Skip to content

urain39/IJCache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IJCache

A fast Cache module written in Python.

Usage

import ijcache

# Create a Bit-PLRU Cache object with 32 items
ca = ijcache.BPLRUCache(32)

# Create a 2-Random Choices Cache object with 1024 items
ca = ijcache.TRCCache(1024)

# Add one item
ca.add('k1', 1)

# Lookup an item
ca.lookup('k1')

# Lookup and add in one method
val = ca.ensure('k2', lambda : tuple(range(0, 256)))

# Remove one item
ca.remove('k1')

# With decorator
@ca.cache
def fib(x):
  if x == 1: return 1
  if x == 0: return 0
  return fib(x - 1) + fib(x - 2)

# Custom event handlers
class MyItem(ijcache.Item):
  def on_hit(self):
    print(f'Hit on {self.value}')
  def on_evict(self):
    print(f'Evict {self.value}')

ca.add('k2', MyItem(2))
v2 = ca.lookup('k2').value

# Want more? Just see `help(ca)`

Benchmarks

View source

bm1.txt

1. bplru 1.2433981895446777, 14930352
2. lru 1.26200270652771, 14930352
3. functools 1.2794532775878906, 14930352
4. trc 1.292776107788086, 14930352
5. none 1.3914763927459717, 14930352

bm2.txt

1. trc 1.2627553939819336, 14930352
2. bplru 1.2631657123565674, 14930352
3. functools 1.2682974338531494, 14930352
4. lru 1.2897653579711914, 14930352
5. none 1.3929316997528076, 14930352

bm3.txt

1. bplru 1.2370176315307617, 14930352
2. functools 1.2376103401184082, 14930352
3. trc 1.2679831981658936, 14930352
4. lru 1.2752890586853027, 14930352
5. none 1.399376630783081, 14930352

I can't direct say which one is best, but in 32 items, bplru is a not bad choice. And in other cases, functools may better than my implementations, because it with less wrappings.

Notes

Some codes refer to karlmcguire/plru, thank Karl!