# 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!
Raw data
{
"_id": null,
"home_page": "https://github.com/urain39/ijcache",
"name": "ijcache",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "cache,lru,plru,pseudo-lru",
"author": "urain39",
"author_email": "urain39@qq.com",
"download_url": "",
"platform": "all",
"description": "# IJCache\n\nA fast Cache module written in Python.\n\n\nUsage\n=====\n\n```py\nimport ijcache\n\n# Create a Bit-PLRU Cache object with 32 items\nca = ijcache.BPLRUCache(32)\n\n# Create a 2-Random Choices Cache object with 1024 items\nca = ijcache.TRCCache(1024)\n\n# Add one item\nca.add('k1', 1)\n\n# Lookup an item\nca.lookup('k1')\n\n# Lookup and add in one method\nval = ca.ensure('k2', lambda : tuple(range(0, 256)))\n\n# Remove one item\nca.remove('k1')\n\n# With decorator\n@ca.cache\ndef fib(x):\n if x == 1: return 1\n if x == 0: return 0\n return fib(x - 1) + fib(x - 2)\n\n# Custom event handlers\nclass MyItem(ijcache.Item):\n def on_hit(self):\n print(f'Hit on {self.value}')\n def on_evict(self):\n print(f'Evict {self.value}')\n\nca.add('k2', MyItem(2))\nv2 = ca.lookup('k2').value\n\n# Want more? Just see `help(ca)`\n```\n\n\nBenchmarks\n==========\n\nView [source](benchmark.py)\n\nbm1.txt\n```\n1. bplru 1.2433981895446777, 14930352\n2. lru 1.26200270652771, 14930352\n3. functools 1.2794532775878906, 14930352\n4. trc 1.292776107788086, 14930352\n5. none 1.3914763927459717, 14930352\n```\n\nbm2.txt\n```\n1. trc 1.2627553939819336, 14930352\n2. bplru 1.2631657123565674, 14930352\n3. functools 1.2682974338531494, 14930352\n4. lru 1.2897653579711914, 14930352\n5. none 1.3929316997528076, 14930352\n```\n\nbm3.txt\n```\n1. bplru 1.2370176315307617, 14930352\n2. functools 1.2376103401184082, 14930352\n3. trc 1.2679831981658936, 14930352\n4. lru 1.2752890586853027, 14930352\n5. none 1.399376630783081, 14930352\n```\n\nI can't direct say which one is best, but in 32 items, bplru is a not bad\nchoice. And in other cases, functools may better than my implementations,\nbecause it with less wrappings.\n\n\nNotes\n=====\n\nSome codes refer to [karlmcguire/plru](https://github.com/karlmcguire/plru),\nthank Karl!\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A fast Cache module written in Python",
"version": "0.0.1",
"split_keywords": [
"cache",
"lru",
"plru",
"pseudo-lru"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "8daa0798e2b392a6e0a064a6bee58166",
"sha256": "b2a63433dc2326fac290defde0236621057723881aeb460bc54513c5248b69cf"
},
"downloads": -1,
"filename": "ijcache-0.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8daa0798e2b392a6e0a064a6bee58166",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 5615,
"upload_time": "2022-12-09T02:12:31",
"upload_time_iso_8601": "2022-12-09T02:12:31.644233Z",
"url": "https://files.pythonhosted.org/packages/1b/55/ffa52bebf258ef394176ca2cf409ddc4257af0b3b2d59caf3d81eaf55156/ijcache-0.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-12-09 02:12:31",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "urain39",
"github_project": "ijcache",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "ijcache"
}