Skip to main content

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!

Metadata

Release files for ijcache 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for ijcache 0.0.1
File Interpreter ABI Platform
ijcache-0.0.1-py3-none-any.whl Python 3 none any Details

Release files / ijcache-0.0.1-py3-none-any.whl

Download URL ijcache-0.0.1-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b2a63433dc2326fac290defde0236621057723881aeb460bc54513c5248b69cf
BLAKE2b-256 checksum
How to use checksums
1b55ffa52bebf258ef394176ca2cf409ddc4257af0b3b2d59caf3d81eaf55156
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.0

Release history Release notifications | RSS feed

This release

0.0.1 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page