Skip to content

Ensure flexicache is LRU cached #679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions fastcore/xtras.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,8 +748,9 @@ def is_namedtuple(cls):
def flexicache(*funcs, maxsize=128):
"Like `lru_cache`, but customisable with policy `funcs`"
import asyncio
from collections import OrderedDict
def _f(func):
cache,states = {}, [None]*len(funcs)
cache,states = OrderedDict(), [None]*len(funcs)
def _cache_logic(key, execute_func):
if key in cache:
result,states = cache[key]
Expand All @@ -763,7 +764,7 @@ def _cache_logic(key, execute_func):
cache[key] = cache.pop(key)
return result
cache[key] = (newres, [f(None) for f in funcs])
if len(cache) > maxsize: cache.popitem()
if len(cache) > maxsize: cache.popitem(last=False)
return newres

@wraps(func)
Expand Down
84 changes: 52 additions & 32 deletions nbs/03_xtras.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@
{
"data": {
"text/plain": [
"'pip 23.3.1 from /Users/jhoward/miniconda3/lib/python3.11/site-packages/pip (python 3.11)'"
"'pip 25.0.1 from /Users/drg/.venv/lib/python3.12/site-packages/pip (python 3.12)'"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1194,7 +1194,7 @@
{
"data": {
"text/plain": [
"Path('/Users/daniel.roy.greenfeld/fh/fastcore/fastcore')"
"Path('/Users/drg/git/fastcore/fastcore')"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1261,7 +1261,7 @@
{
"data": {
"text/plain": [
"Path('000_tour.ipynb')"
"Path('llms.txt')"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1440,7 +1440,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/xtras.py#L389){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L391){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"#### ReindexCollection\n",
"\n",
Expand All @@ -1451,7 +1451,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/xtras.py#L389){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L391){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"#### ReindexCollection\n",
"\n",
Expand Down Expand Up @@ -1523,7 +1523,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/xtras.py#L400){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L402){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"###### ReindexCollection.reindex\n",
"\n",
Expand All @@ -1534,7 +1534,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/xtras.py#L400){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L402){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"###### ReindexCollection.reindex\n",
"\n",
Expand Down Expand Up @@ -1623,7 +1623,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/xtras.py#L404){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L406){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"##### ReindexCollection.cache_clear\n",
"\n",
Expand All @@ -1634,7 +1634,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/xtras.py#L404){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L406){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"##### ReindexCollection.cache_clear\n",
"\n",
Expand Down Expand Up @@ -1688,7 +1688,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/xtras.py#L401){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L403){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"##### ReindexCollection.shuffle\n",
"\n",
Expand All @@ -1699,7 +1699,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/xtras.py#L401){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L403){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"##### ReindexCollection.shuffle\n",
"\n",
Expand Down Expand Up @@ -1732,7 +1732,7 @@
{
"data": {
"text/plain": [
"['a', 'd', 'h', 'c', 'e', 'b', 'f', 'g']"
"['a', 'd', 'g', 'f', 'h', 'e', 'b', 'c']"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1988,7 +1988,7 @@
{
"data": {
"text/plain": [
"'https://github.com/fastai/fastcore/tree/master/fastcore/test.py#L35'"
"'https://github.com/fastai/fastcore/tree/master/fastcore/test.py#L37'"
]
},
"execution_count": null,
Expand Down Expand Up @@ -2291,7 +2291,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/xtras.py#L530){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L534){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"#### EventTimer\n",
"\n",
Expand All @@ -2302,7 +2302,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/xtras.py#L530){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L534){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"#### EventTimer\n",
"\n",
Expand Down Expand Up @@ -2336,8 +2336,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Num Events: 3, Freq/sec: 205.6\n",
"Most recent: ▁▁▃▁▇ 254.1 263.2 284.5 259.9 315.7\n"
"Num Events: 1, Freq/sec: 155.1\n",
"Most recent: ▇▂▂▁▁ 386.7 269.9 285.5 249.6 220.0\n"
]
}
],
Expand Down Expand Up @@ -2416,7 +2416,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/xtras.py#L562){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L566){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"#### PartialFormatter\n",
"\n",
Expand All @@ -2427,7 +2427,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/xtras.py#L562){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L566){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"#### PartialFormatter\n",
"\n",
Expand Down Expand Up @@ -2499,7 +2499,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"2000-01-01 12:00:00 UTC is 2000-01-01 22:00:00+10:00 local time\n"
"2000-01-01 12:00:00 UTC is 2000-01-01 20:00:00+08:00 local time\n"
]
}
],
Expand Down Expand Up @@ -2529,7 +2529,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"2000-01-01 12:00:00 local is 2000-01-01 02:00:00+00:00 UTC time\n"
"2000-01-01 12:00:00 local is 2000-01-01 04:00:00+00:00 UTC time\n"
]
}
],
Expand Down Expand Up @@ -2632,7 +2632,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/xtras.py#L619){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L623){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"#### ContextManagers\n",
"\n",
Expand All @@ -2643,7 +2643,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/xtras.py#L619){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L623){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"#### ContextManagers\n",
"\n",
Expand Down Expand Up @@ -3172,8 +3172,9 @@
"def flexicache(*funcs, maxsize=128):\n",
" \"Like `lru_cache`, but customisable with policy `funcs`\"\n",
" import asyncio\n",
" from collections import OrderedDict\n",
" def _f(func):\n",
" cache,states = {}, [None]*len(funcs)\n",
" cache,states = OrderedDict(), [None]*len(funcs)\n",
" def _cache_logic(key, execute_func):\n",
" if key in cache:\n",
" result,states = cache[key]\n",
Expand All @@ -3187,7 +3188,7 @@
" cache[key] = cache.pop(key)\n",
" return result\n",
" cache[key] = (newres, [f(None) for f in funcs])\n",
" if len(cache) > maxsize: cache.popitem()\n",
" if len(cache) > maxsize: cache.popitem(last=False)\n",
" return newres\n",
"\n",
" @wraps(func)\n",
Expand Down Expand Up @@ -3298,6 +3299,32 @@
" return flexicache(time_policy(seconds), maxsize=maxsize)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# demonstrate that flexicache is LRU\n",
"@flexicache(maxsize=2)\n",
"def cached_func(x): return time()\n",
"\n",
"time_1 = cached_func(1)\n",
"test_eq(time_1, cached_func(1))\n",
"\n",
"time_2 = cached_func(2)\n",
"test_eq(time_1, cached_func(1))\n",
"test_eq(time_2, cached_func(2))\n",
"\n",
"time_3 = cached_func(3) # Removes 1\n",
"\n",
"test_eq(time_2, cached_func(2)) # cache remains\n",
"test_eq(time_3, cached_func(3)) # cache remains\n",
"test_ne(time_1, cached_func(1)) # NEQ, removes 2\n",
"test_ne(time_2, cached_func(2)) # NEQ, removes 3\n",
"test_eq(cached_func(1), cached_func(1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -3355,13 +3382,6 @@
"#|hide\n",
"import nbdev; nbdev.nbdev_export()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down