Skip to main content

A fast Cache module written in Python

Project description

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!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

ijcache-0.0.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file ijcache-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: ijcache-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for ijcache-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b2a63433dc2326fac290defde0236621057723881aeb460bc54513c5248b69cf
MD5 8daa0798e2b392a6e0a064a6bee58166
BLAKE2b-256 1b55ffa52bebf258ef394176ca2cf409ddc4257af0b3b2d59caf3d81eaf55156

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page