Skip to content

Commit

Permalink
improve caching helper
Browse files Browse the repository at this point in the history
  • Loading branch information
trisongz committed Feb 28, 2024
1 parent 64e87c5 commit 2166407
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
28 changes: 22 additions & 6 deletions lazyops/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,10 @@ def timed_cache(
secs: typing.Optional[int] = 60 * 60,
maxsize: int = 1024,
invalidate_cache_key: typing.Optional[str] = '_invalidate_cache',
**kwargs,
cache_if_result: typing.Optional[typing.Any] = None,
cache_if_type: typing.Optional[typing.Type] = None,
exclude_none: typing.Optional[bool] = False,
**_kwargs,
):
"""
Wrapper for creating a expiring cached function
Expand All @@ -310,7 +313,7 @@ def timed_cache(
maxsize: maxsize of the cache
invalidate_cache_key: key to invalidate the cache
"""
if 'ttl' in kwargs: secs = kwargs.pop('ttl')
if 'ttl' in _kwargs: secs = _kwargs.pop('ttl')
if secs is None: secs = 60
def wrapper_cache(func):
if is_coro_func(func):
Expand All @@ -323,10 +326,16 @@ async def _wrapped(*args, **kwargs):
async def wrapped_func(*args, **kwargs):
_invalidate = kwargs.pop(invalidate_cache_key, None) if invalidate_cache_key else None
args, kwargs = freeze_args_and_kwargs(*args, **kwargs)
if _invalidate is True:
if _invalidate is True: _wrapped.cache_invalidate(*args, **kwargs)
result = await _wrapped(*args, **kwargs)
if exclude_none is True and result is None:
_wrapped.cache_invalidate(*args, **kwargs)
elif cache_if_result is not None and result != cache_if_result:
_wrapped.cache_invalidate(*args, **kwargs)
elif cache_if_type is not None and not isinstance(result, cache_if_type):
_wrapped.cache_invalidate(*args, **kwargs)
# print(f'wrapped: {_wrapped.cache_info()}')
return await _wrapped(*args, **kwargs)
# print(f'Invalidating cache for {func.__name__}')
return result

return wrapped_func

Expand All @@ -343,7 +352,14 @@ def _check_cache(func, invalidate: typing.Optional[bool] = None):
def wrapped_func(*args, **kwargs):
_check_cache(func, invalidate = kwargs.pop(invalidate_cache_key, None) if invalidate_cache_key else None)
args, kwargs = freeze_args_and_kwargs(*args, **kwargs)
return func(*args, **kwargs)
result = func(*args, **kwargs)
if exclude_none is True and result is None:
func.cache_clear()
elif cache_if_result is not None and result != cache_if_result:
func.cache_clear()
elif cache_if_type is not None and not isinstance(result, cache_if_type):
func.cache_clear()
return result

return wrapped_func

Expand Down
2 changes: 1 addition & 1 deletion lazyops/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = '0.2.75'
VERSION = '0.2.76'
35 changes: 35 additions & 0 deletions tests/libs/test_cacheing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import time
import asyncio
import random
from lazyops.utils.helpers import timed_cache

"""
Test of the new behavior for only caching if the result is True
"""

_values = [
True,
False,
False,
False,
False,
]

@timed_cache(5, cache_if_result=True)
def test_timed_cache() -> bool:
return random.choice(_values)

for _ in range(10):
print(test_timed_cache())

@timed_cache(2, cache_if_result=True)
async def test_timed_cache_async() -> bool:
return random.choice(_values)


async def run_test():
for _ in range(10):
print(await test_timed_cache_async())
await asyncio.sleep(1)

asyncio.run(run_test())

0 comments on commit 2166407

Please sign in to comment.