Skip to content

Commit 383bc98

Browse files
author
Ram Rachum
committed
-
1 parent 87419a8 commit 383bc98

File tree

2 files changed

+17
-16
lines changed
  • garlicsim

2 files changed

+17
-16
lines changed

garlicsim/garlicsim/general_misc/caching/cache.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,15 @@ def f(a, b=2):
5757
'used `@cache`, while you should have used '
5858
'`@cache()`' % max_size)
5959

60-
if max_size == infinity:
60+
def decorator(function):
6161

62-
def decorator(function):
63-
# In case we're being given a function that is already cached:
64-
if getattr(function, 'is_cached', False): return function
62+
# In case we're being given a function that is already cached:
63+
if getattr(function, 'is_cached', False): return function
64+
65+
if max_size == infinity:
6566

6667
cache_dict = {}
67-
68+
6869
def cached(function, *args, **kwargs):
6970
sleek_call_args = \
7071
SleekCallArgs(cache_dict, function, *args, **kwargs)
@@ -74,12 +75,8 @@ def cached(function, *args, **kwargs):
7475
cached._cache[sleek_call_args] = value = \
7576
function(*args, **kwargs)
7677
return value
77-
78-
else: # max_size < infinity
79-
80-
def decorator(function):
81-
# In case we're being given a function that is already cached:
82-
if getattr(function, 'is_cached', False): return function
78+
79+
else: # max_size < infinity
8380

8481
cache_dict = OrderedDict()
8582

@@ -100,12 +97,13 @@ def cached(function, *args, **kwargs):
10097
cached._cache = cache_dict
10198
cached.is_cached = True
10299

100+
result = decorator_tools.decorator(cached, function)
101+
103102
def cache_clear():
104103
'''Clear the cache, deleting all saved results.'''
105-
cache.clear()
106-
107-
result = decorator_tools.decorator(cached, function)
108-
result.cache_clear = cached._cache.clear
104+
cached._cache.clear()
105+
result.cache_clear = cache_clear
106+
109107
return result
110108

111109
return decorator

garlicsim/test_garlicsim/test_general_misc/test_caching/test_cache.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,7 @@ def test_api():
179179
result_2 = cached_function(1)
180180

181181
assert cached_function(1) == result_2 == cached_function(1)
182-
assert result_1 != result_2 == cached_function(1) != result_1
182+
assert result_1 != result_2 == cached_function(1) != result_1
183+
184+
# Asserting we're not using `dict.clear` or something:
185+
assert cached_function.cache_clear.__name__ == 'cache_clear'

0 commit comments

Comments
 (0)