Skip to content

Commit 01f7d49

Browse files
committed
Fix logic wrong current_redis_cache and aio version
1 parent aa8bbac commit 01f7d49

File tree

4 files changed

+11
-41
lines changed

4 files changed

+11
-41
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
__pycache__
22
*.pyc
3-
test*.py
3+
test*.py
4+
dist
5+
*.egg-info

examples/basic.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
from py_redis_cache import RedisCache
55
from py_redis_cache.functional import cache
66

7+
78
# init redis_cache instance and connection
89
redis_cache = RedisCache(
910
host="127.0.0.1",
1011
port=6379,
1112
verbose=1 # Turn on logging for demonstration, set to 0 for silent caching
1213
)
1314

14-
@redis_cache.cache(ttl=10) # Expire after 10 seconds
15+
@cache(ttl=10) # Expire after 10 seconds
1516
def heavy_compute(a: list, b: list):
1617
length = max(len(a), len(b))
1718
c = [[]] * length
@@ -26,5 +27,6 @@ def heavy_compute(a: list, b: list):
2627

2728
result = heavy_compute([1, 2, 3], [4, 5, 6])
2829
print(result)
30+
2931
# Cache hit, key=redis_cache::11=__main__.heavy_compute(a=[1, 2, 3].b=[4, 5, 6])
3032
# [4, 10, 18]

py_redis_cache/__init__.py

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ def __call__(cls, *args, **kwargs):
2424
is_aio_redis = isinstance(cls, AsyncRedisCache)
2525

2626
if cls not in cls._instances:
27-
2827
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
29-
if is_aio_redis:
28+
if not is_aio_redis:
3029
current_redis_cache = cls._instances[cls]
3130
print("SET", current_redis_cache)
3231
else:
@@ -254,38 +253,3 @@ def wrapper(*args, **kwargs):
254253
return data
255254
return wrapper
256255
return decorator
257-
258-
259-
def cached(
260-
cache_instance: RedisCache,
261-
tags: Union[list, str] = None,
262-
ttl=30
263-
) -> Callable:
264-
"""
265-
Return a decorator to cache the output of a function.
266-
The cache will be invalidated after amount of time defined by
267-
`ttl` argument (30 seconds by default)
268-
269-
Args:
270-
tags(list|str, optional): tag of the cache, the tag
271-
will be added to the key for storing data.
272-
ttl(int): Time to live of the cache, that mean the cache
273-
will be deleted after `ttl` seconds.
274-
275-
Returns:
276-
Decorator function
277-
"""
278-
def decorator(func):
279-
def wrapper(*args, **kwargs):
280-
key = cache_instance._build_key(tags, func, *args, **kwargs)
281-
data = cache_instance.get(key)
282-
if data is None:
283-
# Cache miss, call function and set result in redis
284-
data = func(*args, **kwargs)
285-
cache_instance.setex(key, data, ttl=ttl)
286-
else:
287-
# Cache hit
288-
cache_instance.log_info(f"Cache hit, key={key}")
289-
return data
290-
return wrapper
291-
return decorator

py_redis_cache/functional.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11

2+
23
def cache(tags=None, ttl=30):
34
from . import current_redis_cache
5+
print("Import")
46
return current_redis_cache.cache(tags, ttl)
57

68

79
def aio_cache(tags=None, ttl=30):
8-
from . import current_redis_cache
9-
return current_redis_cache.cache(tags, ttl)
10+
from . import current_aio_redis_cache
11+
return current_aio_redis_cache.cache(tags, ttl)

0 commit comments

Comments
 (0)