6 static void remove_entry(flxCache *c, flxCacheEntry *e) {
12 /* g_message("removing from cache: %p %p", c, e); */
14 /* Remove from hash table */
15 t = g_hash_table_lookup(c->hash_table, e->record->key);
16 FLX_LLIST_REMOVE(flxCacheEntry, by_key, t, e);
18 g_hash_table_replace(c->hash_table, t->record->key, t);
20 g_hash_table_remove(c->hash_table, e->record->key);
22 /* Remove from linked list */
23 FLX_LLIST_REMOVE(flxCacheEntry, entry, c->entries, e);
26 flx_time_event_queue_remove(c->server->time_event_queue, e->time_event);
28 flx_subscription_notify(c->server, c->interface, e->record, FLX_SUBSCRIPTION_REMOVE);
30 flx_record_unref(e->record);
35 flxCache *flx_cache_new(flxServer *server, flxInterface *iface) {
39 c = g_new(flxCache, 1);
42 c->hash_table = g_hash_table_new((GHashFunc) flx_key_hash, (GEqualFunc) flx_key_equal);
44 FLX_LLIST_HEAD_INIT(flxCacheEntry, c->entries);
49 void flx_cache_free(flxCache *c) {
53 remove_entry(c, c->entries);
55 g_hash_table_destroy(c->hash_table);
60 flxCacheEntry *flx_cache_lookup_key(flxCache *c, flxKey *k) {
64 g_assert(!flx_key_is_pattern(k));
66 return g_hash_table_lookup(c->hash_table, k);
69 gpointer flx_cache_walk(flxCache *c, flxKey *pattern, flxCacheWalkCallback cb, gpointer userdata) {
76 if (flx_key_is_pattern(pattern)) {
79 for (e = c->entries; e; e = n) {
82 if (flx_key_pattern_match(pattern, e->record->key))
83 if ((ret = cb(c, pattern, e, userdata)))
90 for (e = flx_cache_lookup_key(c, pattern); e; e = n) {
93 if ((ret = cb(c, pattern, e, userdata)))
101 static gpointer lookup_record_callback(flxCache *c, flxKey *pattern, flxCacheEntry *e, void *userdata) {
106 if (flx_record_equal_no_ttl(e->record, userdata))
112 flxCacheEntry *flx_cache_lookup_record(flxCache *c, flxRecord *r) {
118 return flx_cache_walk(c, r->key, lookup_record_callback, r);
121 static void next_expiry(flxCache *c, flxCacheEntry *e, guint percent);
123 static void elapse_func(flxTimeEvent *t, void *userdata) {
124 flxCacheEntry *e = userdata;
129 if (e->state == FLX_CACHE_FINAL) {
130 remove_entry(e->cache, e);
131 g_message("Removing entry from cache due to expiration");
136 case FLX_CACHE_VALID:
137 e->state = FLX_CACHE_EXPIRY1;
141 case FLX_CACHE_EXPIRY1:
142 e->state = FLX_CACHE_EXPIRY2;
145 case FLX_CACHE_EXPIRY2:
146 e->state = FLX_CACHE_EXPIRY3;
150 case FLX_CACHE_EXPIRY3:
151 e->state = FLX_CACHE_FINAL;
159 g_assert(percent > 0);
161 g_message("Requesting cache entry update at %i%%.", percent);
163 /* Request a cache update, if we are subscribed to this entry */
164 if (flx_is_subscribed(e->cache->server, e->record->key))
165 flx_interface_post_query(e->cache->interface, e->record->key, TRUE);
167 /* Check again later */
168 next_expiry(e->cache, e, percent);
172 static void update_time_event(flxCache *c, flxCacheEntry *e) {
177 flx_time_event_queue_update(c->server->time_event_queue, e->time_event, &e->expiry);
179 e->time_event = flx_time_event_queue_add(c->server->time_event_queue, &e->expiry, elapse_func, e);
182 static void next_expiry(flxCache *c, flxCacheEntry *e, guint percent) {
187 g_assert(percent > 0 && percent <= 100);
189 e->expiry = e->timestamp;
191 usec = e->record->ttl * 10000;
194 usec = g_random_int_range(usec*percent, usec*(percent+2));
196 g_time_val_add(&e->expiry, usec);
197 update_time_event(c, e);
200 void flx_cache_update(flxCache *c, flxRecord *r, gboolean unique, const flxAddress *a) {
201 flxCacheEntry *e, *t;
205 g_assert(r && r->ref >= 1);
207 g_message("cache update: %s", (txt = flx_record_to_string(r)));
212 /* This is a goodbye request */
214 if ((e = flx_cache_lookup_record(c, r))) {
216 e->state = FLX_CACHE_FINAL;
217 g_get_current_time(&e->timestamp);
218 e->expiry = e->timestamp;
219 g_time_val_add(&e->expiry, 1000000); /* 1s */
220 update_time_event(c, e);
225 /* This is an update request */
227 if ((t = e = flx_cache_lookup_key(c, r->key))) {
231 /* For unique records, remove all entries but one */
232 while (e->by_key_next)
233 remove_entry(c, e->by_key_next);
237 /* For non-unique record, look for exactly the same entry */
238 for (; e; e = e->by_key_next)
239 if (flx_record_equal_no_ttl(e->record, r))
246 /* g_message("found matching cache entry"); */
248 /* We are the first in the linked list so let's replace the hash table key with the new one */
249 if (e->by_key_prev == NULL)
250 g_hash_table_replace(c->hash_table, r->key, e);
252 /* Notify subscribers */
253 if (!flx_record_equal_no_ttl(e->record, r))
254 flx_subscription_notify(c->server, c->interface, r, FLX_SUBSCRIPTION_CHANGE);
256 /* Update the record */
257 flx_record_unref(e->record);
258 e->record = flx_record_ref(r);
261 /* No entry found, therefore we create a new one */
263 /* g_message("couldn't find matching cache entry"); */
265 e = g_new(flxCacheEntry, 1);
267 e->time_event = NULL;
268 e->record = flx_record_ref(r);
270 /* Append to hash table */
271 FLX_LLIST_PREPEND(flxCacheEntry, by_key, t, e);
272 g_hash_table_replace(c->hash_table, e->record->key, t);
274 /* Append to linked list */
275 FLX_LLIST_PREPEND(flxCacheEntry, entry, c->entries, e);
277 /* Notify subscribers */
278 flx_subscription_notify(c->server, c->interface, e->record, FLX_SUBSCRIPTION_NEW);
282 g_get_current_time(&e->timestamp);
283 next_expiry(c, e, 80);
284 e->state = FLX_CACHE_VALID;
288 static gpointer drop_key_callback(flxCache *c, flxKey *pattern, flxCacheEntry *e, gpointer userdata) {
297 void flx_cache_drop_key(flxCache *c, flxKey *k) {
301 flx_cache_walk(c, k, drop_key_callback, NULL);
304 void flx_cache_drop_record(flxCache *c, flxRecord *r) {
310 if ((e = flx_cache_lookup_record(c, r)))
314 static void func(gpointer key, gpointer data, gpointer userdata) {
315 flxCacheEntry *e = data;
319 t = flx_record_to_string(e->record);
320 fprintf((FILE*) userdata, "%s\n", t);
324 void flx_cache_dump(flxCache *c, FILE *f) {
328 fprintf(f, ";;; CACHE DUMP FOLLOWS ;;;\n");
329 g_hash_table_foreach(c->hash_table, func, f);
332 gboolean flx_cache_entry_half_ttl(flxCache *c, flxCacheEntry *e) {
339 g_get_current_time(&now);
341 age = flx_timeval_diff(&now, &e->timestamp)/1000000;
343 g_message("age: %u, ttl/2: %u", age, e->record->ttl);
345 return age >= e->record->ttl/2;