Metadata-Version: 2.1
Name: ijcache
Version: 0.0.1
Summary: A fast Cache module written in Python
Home-page: https://github.com/urain39/ijcache
Author: urain39
Author-email: urain39@qq.com
License: MIT
Keywords: cache,lru,plru,pseudo-lru
Platform: all
Classifier: Development Status :: 4 - Beta
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: BSD License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: Implementation
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Topic :: Software Development :: Libraries
Description-Content-Type: text/markdown
License-File: LICENSE

# IJCache

A fast Cache module written in Python.


Usage
=====

```py
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](benchmark.py)

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](https://github.com/karlmcguire/plru),
thank Karl!
