+static int cache_flush (void)
+{
+ char **keys = NULL;
+ int keys_num = 0;
+
+ char **tmp;
+ int i;
+
+ char *key;
+ time_t *value;
+ avl_iterator_t *iter;
+
+ time_t curtime = time (NULL);
+
+ iter = avl_get_iterator (cache_tree);
+ while (avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
+ {
+ if ((curtime - *value) <= cache_flush_interval)
+ continue;
+ tmp = (char **) realloc (keys,
+ (keys_num + 1) * sizeof (char *));
+ if (tmp == NULL)
+ {
+ sfree (keys);
+ avl_iterator_destroy (iter);
+ ERROR ("network plugin: cache_flush: realloc"
+ " failed.");
+ return (-1);
+ }
+ keys = tmp;
+ keys[keys_num] = key;
+ keys_num++;
+ } /* while (avl_iterator_next) */
+ avl_iterator_destroy (iter);
+
+ for (i = 0; i < keys_num; i++)
+ {
+ if (avl_remove (cache_tree, keys[i], (void *) &key,
+ (void *) &value) != 0)
+ {
+ WARNING ("network plugin: cache_flush: avl_remove"
+ " (%s) failed.", keys[i]);
+ continue;
+ }
+
+ sfree (key);
+ sfree (value);
+ }
+
+ sfree (keys);
+
+ DEBUG ("network plugin: cache_flush: Removed %i %s",
+ keys_num, (keys_num == 1) ? "entry" : "entries");
+ cache_flush_last = curtime;
+ return (0);
+} /* int cache_flush */
+
+static int cache_check (const char *type, const value_list_t *vl)
+{
+ char key[1024];
+ time_t *value = NULL;
+ int retval = -1;
+
+ if (cache_tree == NULL)
+ return (-1);
+
+ if (format_name (key, sizeof (key), vl->host, vl->plugin,
+ vl->plugin_instance, type, vl->type_instance))
+ return (-1);
+
+ pthread_mutex_lock (&cache_lock);
+
+ if (avl_get (cache_tree, key, (void *) &value) == 0)
+ {
+ if (*value < vl->time)
+ {
+ *value = vl->time;
+ retval = 0;
+ }
+ else
+ {
+ DEBUG ("network plugin: cache_check: *value = %i >= vl->time = %i",
+ (int) *value, (int) vl->time);
+ retval = 1;
+ }
+ }
+ else
+ {
+ char *key_copy = strdup (key);
+ value = malloc (sizeof (time_t));
+ if ((key_copy != NULL) && (value != NULL))
+ {
+ *value = vl->time;
+ avl_insert (cache_tree, key_copy, value);
+ retval = 0;
+ }
+ else
+ {
+ sfree (key_copy);
+ sfree (value);
+ }
+ }
+
+ if ((time (NULL) - cache_flush_last) > cache_flush_interval)
+ cache_flush ();
+
+ pthread_mutex_unlock (&cache_lock);
+
+ DEBUG ("network plugin: cache_check: key = %s; time = %i; retval = %i",
+ key, (int) vl->time, retval);
+
+ return (retval);
+} /* int cache_check */
+