irfpy.util.ringcache
¶
The module provide ring cache.
Ring cache provides a cache to any objects, but with limitation of the maximum number of the object.
Using this instead of the usual dictionary
as cache,
developer may control the maximum use of the memory.
Code author: Yoshifumi Futaana
- class irfpy.util.ringcache.RingCache(max_cache, name=None)[source]¶
Bases:
object
A ring cache
>>> rcache = RingCache(5, name="mycache") >>> print(rcache) <Ring cache (mycache): Size=0 / Max=5> >>> rcache.add(30, 'thirty') >>> print(len(rcache)) 1 >>> rcache.add(20, None) >>> rcache.add(10, None) >>> print(len(rcache)) 3 >>> print(rcache.get(10)) None >>> rcache.add(11, 'Eleven') >>> rcache.add(12, 'tolv') >>> rcache.add(13, '13') >>> rcache.add(14, '14') >>> rcache.add(15, '15') >>> print(len(rcache)) 5 >>> print(rcache.get(13)) 13 >>> print(rcache.hasKey(30)) False >>> rcache.get(30) Traceback (most recent call last): ... KeyError: >>> rcache.show_statistics(file=sys.stdout) Name=mycache Max=5 Size=5 Added=8 Popped=3 Get (total)=3 Get (success)=2 Inquired=1
>>> rcache.clearCache() >>> print(rcache) <Ring cache (mycache): Size=0 / Max=5> >>> print(15 in rcache) False >>> rcache.add(15, 'Femton') >>> print(rcache.get(15)) Femton
>>> rcache.show_statistics(file=sys.stdout) Name=mycache Max=5 Size=1 Added=1 Popped=0 Get (total)=1 Get (success)=1 Inquired=1
Instance a ring cache.
- Parameters:
max_cache – The maximum cache to be stored.
- hasKey(key)[source]¶
Return if the
key
is in cache- Parameters:
key – Key of the data
- Returns:
True/False if the key-data pair is/is-not in the cache.
- get(key)[source]¶
Get the value corresponding to the
key
- Parameters:
key – Key of the data
- Returns:
The data
It raises
KeyError
if the key-data pair is not in the cache.
- class irfpy.util.ringcache.SimpleRingCache(max_cache)[source]¶
Bases:
object
A simple ring cache (dictionary-like).
>>> rcache = SimpleRingCache(5) >>> rcache.add(30, 'thirty') >>> print(len(rcache)) 1 >>> rcache.add(20, None) >>> rcache.add(10, None) >>> print(len(rcache)) 3 >>> print(rcache.get(10)) None >>> rcache.add(11, 'Eleven') >>> rcache.add(12, 'tolv') >>> rcache.add(13, '13') >>> rcache.add(14, '14') >>> rcache.add(15, '15') >>> print(len(rcache)) 5 >>> print(rcache.get(13)) 13 >>> print(rcache.hasKey(30)) False
Instance a ring cache.
@param max_cache The maximum cache to be stored.
- hasKey(key)[source]¶
Return if the
key
is in cache- Parameters:
key – Key of the data
- Returns:
True/False if the key-data pair is/is-not in the cache.