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.

add(key, obj)[source]

Add the object to cache

clearCache()[source]

Clear the cache

consistency_check()[source]
size()[source]
cachesize()[source]
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.

showBuffer(file=<_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>)[source]

Show the buffer information

Parameters

file – A file object to dump. Deafult is sys.stderr.

show_statistics(file=<_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>)[source]
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.

class UnfilledRingCache(max_cache)[source]

Bases: object

clearCache()[source]
add(key, obj)[source]
size()[source]
cachesize()[source]
consistency_check()[source]
hasKey(key)[source]
get(key)[source]
showBuffer(file=<_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>)[source]
class FilledRingCache(unfilledRingCache)[source]

Bases: object

size()[source]
cachesize()[source]
hasKey(key)[source]
get(key)[source]
add(key, obj)[source]
consistency_check()[source]
showBuffer(file=<_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>)[source]
add(key, obj)[source]
clearCache()[source]

Clear the cache

consistency_check()[source]
size()[source]
cachesize()[source]
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.

showBuffer(file=<_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>)[source]

Show the buffer information

Parameters

file – A file object to dump. Deafult is sys.stderr.