]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: lru: new function to delete <nb> least recently used keys
authorBaptiste Assmann <bedis9@gmail.com>
Thu, 7 Jan 2016 01:28:50 +0000 (02:28 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 11 Jan 2016 06:31:35 +0000 (07:31 +0100)
Introduction of a new function in the LRU cache source file.
Purpose of this function is to be used to delete a number of entries in
the cache. 'number' is defined by the caller and the key removed are
taken at the tail of the tree

include/import/lru.h
src/lru.c

index 220cb17f653f816a7c3acd0548d7e3b6bc011b63..7427fd627ba8ef4cbd772a72402c3474945a1ea7 100644 (file)
@@ -72,3 +72,4 @@ struct lru64 *lru64_get(unsigned long long key, struct lru64_head *lru, void *do
 void lru64_commit(struct lru64 *elem, void *data, void *domain, unsigned long long revision, void (*free)(void *));
 struct lru64_head *lru64_new(int size);
 int lru64_destroy(struct lru64_head *lru);
+void lru64_kill_oldest(struct lru64_head *lru, unsigned long int nb);
index 719fe075e68c2ba75ec5b82d094ef806814f9eb5..84442a47f2d8ec8ef36eaf74c41d9b270b5058e5 100644 (file)
--- a/src/lru.c
+++ b/src/lru.c
@@ -199,6 +199,31 @@ int lru64_destroy(struct lru64_head *lru)
        return 0;
 }
 
+/* kill the <nb> least used entries from the <lru> cache */
+void lru64_kill_oldest(struct lru64_head *lru, unsigned long int nb)
+{
+       struct lru64 *elem, *next;
+
+       for (elem = container_of(lru->list.p, typeof(*elem), lru);
+            nb && (&elem->lru != &lru->list);
+            elem = next) {
+               next = container_of(elem->lru.p, typeof(*next), lru);
+               if (!elem->domain)
+                       continue; /* locked entry */
+
+               LIST_DEL(&elem->lru);
+               eb64_delete(&elem->node);
+               if (elem->data && elem->free)
+                       elem->free(elem->data);
+               if (!lru->spare)
+                       lru->spare = elem;
+               else
+                       free(elem);
+               lru->cache_usage--;
+               nb--;
+       }
+}
+
 /* The code below is just for validation and performance testing. It's an
  * example of a function taking some time to return results that could be
  * cached.