LibRingCache
LibRingCache is a fast implementation of a cache that selectively lets the garbage collector collect old items and tries to keep the newest X (amount can be freely chosen via the size parameter in LibRingCache:create(size))
Old items are discarded by number, if size is N and the N+1th item was just inserted, then the 1st item is free to collect from the GC, IF the item is not in use anywhere else. This way any cached data can be safely used, but you should be aware of global variables holding cached data and so preventing the GC from collecting them.
Note that the Cache will only work correctly if either the key or the value (or both) is a table.
Creating a cache
LibRingCache:create(size)
Creates a new ring cache of size 'size' and returns a reference.
- size
- the size
Example
do local LibRingCache = LibStub:GetLibrary("LibRingCache-1.0"); local cache = LibRingCache:create(10); cache:insert("foo", {"bar"}); print(cache:get("foo")); end
Inserting into a Cache
Cache:insert(key, data)
insert 'data' with key 'key' into this cache.
- key
- a (unique) key by wich the data can be identified
- data
- the data
Example
see LibRingCache:create(size)
Getting data from a Cache
Cache:get(key)
retrieve a cached value by key from the ringcache.
- key
- a (unique) key by wich the value can be identified
Example
see LibRingCache:create(size)
Removing data from a Cache
Cache:remove(key)
remove a cached value by key from the ringcache.
- key
- a (unique) key by wich the value can be identified
- return
- returns the removed data/value
Note
Items removed by :remove will be removed instantly!
Clearing the cache
Cache:clear()
instantly removes all values from the cache
Facts
- Date created
- 14 May 2009
- Last updated
- 14 May 2009