Skip to content

Commit 3ce3fd0

Browse files
committed
make requested changes
1 parent b0f1cd6 commit 3ce3fd0

File tree

3 files changed

+25
-43
lines changed

3 files changed

+25
-43
lines changed

cache_helper/decorators.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ def _cached(func):
2424

2525
@wraps(func)
2626
def wrapper(*args, **kwargs):
27-
cache_key = get_cache_key(*args, **kwargs)
27+
function_cache_key = utils.get_function_cache_key(func_type, func_name, args, kwargs)
28+
cache_key = utils.get_hashed_cache_key(function_cache_key)
2829

2930
try:
3031
value = cache.get(cache_key)
@@ -43,14 +44,5 @@ def wrapper(*args, **kwargs):
4344

4445
return value
4546

46-
def get_cache_key(*args, **kwargs):
47-
"""
48-
Gets the cache key that would be used if the given args and kwargs were supplied to decorated
49-
function. For example, calling foo.get_cache_key('hello', 5) would not call foo - it would just
50-
return the cache key that would be used if you were to call foo with the same args/kwargs.
51-
"""
52-
function_cache_key = utils.get_function_cache_key(func_name, func_type, args, kwargs)
53-
return utils.get_final_cache_key(function_cache_key)
54-
5547
return wrapper
5648
return _cached

cache_helper/utils.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from cache_helper.interfaces import CacheHelperCacheable
77

88

9-
def get_function_cache_key(func_name, func_type, func_args, func_kwargs):
9+
def get_function_cache_key(func_type, func_name, func_args, func_kwargs):
1010
if func_type in ['method', 'function']:
1111
args_string = _sanitize_args(*func_args, **func_kwargs)
1212
elif func_type == 'class_method':
@@ -18,12 +18,12 @@ def get_function_cache_key(func_name, func_type, func_args, func_kwargs):
1818
return key
1919

2020

21-
def get_final_cache_key(key):
21+
def get_hashed_cache_key(key):
2222
"""
2323
Given the intermediate key produced by a function call along with its args + kwargs,
2424
performs a sha256 hash on the utf-8 encoded version of the key, and returns the result
2525
"""
26-
key_hash = sha256(key.encode('utf-8')).hexdigest()
26+
key_hash = sha256(key.encode('utf-8', errors='ignore')).hexdigest()
2727
return key_hash
2828

2929

@@ -42,15 +42,6 @@ def get_function_type(func):
4242
"""
4343
Gets the type of the given function
4444
"""
45-
if inspect.ismethod(func):
46-
# If the self attribute of the function is a class, it must be a class method
47-
# Otherwise, it will be an instance method of some class, so just a regular method
48-
if inspect.isclass(func.__self__):
49-
return 'class_method'
50-
else:
51-
return 'method'
52-
53-
# Covers case when a method is decorated
5445
if 'self' in inspect.getargspec(func).args:
5546
return 'method'
5647
if 'cls' in inspect.getargspec(func).args:

test_project/tests.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from cache_helper import settings
99
from cache_helper.decorators import cached
1010
from cache_helper.interfaces import CacheHelperCacheable
11-
from cache_helper.utils import get_function_type, get_final_cache_key, get_function_cache_key
11+
from cache_helper.utils import get_function_type, get_hashed_cache_key, get_function_cache_key
1212
from cache_helper.exceptions import CacheKeyCreationError, CacheHelperFunctionError
1313

1414

@@ -36,7 +36,6 @@ def fun_math(self, a, b):
3636
def take_then_give_back(self, a):
3737
return a
3838

39-
@cached(60*60)
4039
def instance_method(self):
4140
return self.name
4241

@@ -142,7 +141,7 @@ def assertExpectedKeyInCache(self, key):
142141
"""
143142
Tests given key is in cache, making sure to get the hashed version of key first
144143
"""
145-
finalized_key = get_final_cache_key(key)
144+
finalized_key = get_hashed_cache_key(key)
146145
self.assertTrue(finalized_key in cache)
147146

148147

@@ -202,11 +201,11 @@ def tearDown(self):
202201

203202
def test_unusual_character_key_creation(self):
204203
return_string('āęìøü')
205-
expected_key_unusual_chars = get_function_cache_key('tests.return_string', 'function', ('āęìøü',), {})
204+
expected_key_unusual_chars = get_function_cache_key('function', 'tests.return_string', ('āęìøü',), {})
206205
self.assertExpectedKeyInCache(expected_key_unusual_chars)
207206

208207
return_string('aeiou')
209-
expected_key = get_function_cache_key('tests.return_string', 'function', ('aeiou',), {})
208+
expected_key = get_function_cache_key('function', 'tests.return_string', ('aeiou',), {})
210209
self.assertExpectedKeyInCache(expected_key)
211210

212211
self.assertNotEqual(expected_key_unusual_chars, expected_key)
@@ -216,13 +215,13 @@ def test_same_method_name_different_class(self):
216215
Two different classes with the same method name should have different cache keys
217216
"""
218217
self.apple.take_then_give_back(self.cherry)
219-
apple_take_give_back_cherry_key = get_function_cache_key(
220-
'tests.Fruit.take_then_give_back', 'method', (self.apple, self.cherry), {})
218+
apple_take_give_back_cherry_key = get_function_cache_key('method', 'tests.Fruit.take_then_give_back',
219+
(self.apple, self.cherry), {})
221220
self.assertExpectedKeyInCache(apple_take_give_back_cherry_key)
222221

223222
self.celery.take_then_give_back(self.cherry)
224-
celery_take_give_back_cherry_key = get_function_cache_key(
225-
'tests.Vegetable.take_then_give_back', 'method', (self.celery, self.cherry), {})
223+
celery_take_give_back_cherry_key = get_function_cache_key('method', 'tests.Vegetable.take_then_give_back',
224+
(self.celery, self.cherry), {})
226225
self.assertExpectedKeyInCache(celery_take_give_back_cherry_key)
227226

228227
self.assertNotEqual(apple_take_give_back_cherry_key, celery_take_give_back_cherry_key)
@@ -232,13 +231,13 @@ def test_same_class_method_name_different_class(self):
232231
Two different classes with the same class method name should have different cache keys
233232
"""
234233
self.apple.add_sweet_letter(self.cherry)
235-
apple_add_sweet_cherry_key = get_function_cache_key(
236-
'tests.Fruit.add_sweet_letter', 'class_method', (self.apple, self.cherry), {})
234+
apple_add_sweet_cherry_key = get_function_cache_key('class_method', 'tests.Fruit.add_sweet_letter',
235+
(self.apple, self.cherry), {})
237236
self.assertExpectedKeyInCache(apple_add_sweet_cherry_key)
238237

239238
self.celery.add_sweet_letter(self.cherry)
240-
celery_add_sweet_cherry_key = get_function_cache_key(
241-
'tests.Vegetable.add_sweet_letter', 'class_method', (self.celery, self.cherry), {})
239+
celery_add_sweet_cherry_key = get_function_cache_key('class_method', 'tests.Vegetable.add_sweet_letter',
240+
(self.celery, self.cherry), {})
242241
self.assertExpectedKeyInCache(celery_add_sweet_cherry_key)
243242

244243
self.assertNotEqual(apple_add_sweet_cherry_key, celery_add_sweet_cherry_key)
@@ -248,11 +247,12 @@ def test_same_static_method_name_different_class_instance_reference(self):
248247
Two different classes with the same static method name should have different cache keys
249248
"""
250249
self.apple.static_method(self.cherry)
251-
apple_static_method_key = get_function_cache_key('tests.Fruit.static_method', 'function', (self.cherry,), {})
250+
apple_static_method_key = get_function_cache_key('function', 'tests.Fruit.static_method', (self.cherry,), {})
252251
self.assertExpectedKeyInCache(apple_static_method_key)
253252

254253
self.celery.static_method(self.cherry)
255-
celery_static_method_key = get_function_cache_key('tests.Vegetable.static_method', 'function', (self.cherry,), {})
254+
celery_static_method_key = get_function_cache_key('function', 'tests.Vegetable.static_method', (self.cherry,),
255+
{})
256256
self.assertExpectedKeyInCache(celery_static_method_key)
257257

258258
self.assertNotEqual(apple_static_method_key, celery_static_method_key)
@@ -262,25 +262,24 @@ def test_same_static_method_name_different_class_class_reference(self):
262262
Two different classes with the same static method name should have different cache keys
263263
"""
264264
Fruit.static_method(self.cherry)
265-
fruit_static_method_key = get_function_cache_key('tests.Fruit.static_method', 'function', (self.cherry,), {})
265+
fruit_static_method_key = get_function_cache_key('function', 'tests.Fruit.static_method', (self.cherry,), {})
266266
self.assertExpectedKeyInCache(fruit_static_method_key)
267267

268268
Vegetable.static_method(self.cherry)
269-
vegetable_static_method_key = get_function_cache_key(
270-
'tests.Vegetable.static_method', 'function', (self.cherry,), {})
269+
vegetable_static_method_key = get_function_cache_key('function', 'tests.Vegetable.static_method',
270+
(self.cherry,), {})
271271
self.assertExpectedKeyInCache(vegetable_static_method_key)
272272

273273
self.assertNotEqual(fruit_static_method_key, vegetable_static_method_key)
274274

275275
def test_same_function_name_from_module_level(self):
276276
"""Two different functions with same name should have different cache keys"""
277277
Vegetable.foo(1, 2)
278-
vegetable_static_method_key = get_function_cache_key(
279-
'tests.Vegetable.foo', 'function', (1, 2), {})
278+
vegetable_static_method_key = get_function_cache_key('function', 'tests.Vegetable.foo', (1, 2), {})
280279
self.assertExpectedKeyInCache(vegetable_static_method_key)
281280

282281
foo(1, 2)
283-
module_function_key = get_function_cache_key('tests.foo', 'function', (1, 2), {})
282+
module_function_key = get_function_cache_key('function', 'tests.foo', (1, 2), {})
284283
self.assertExpectedKeyInCache(module_function_key)
285284

286285
self.assertNotEqual(vegetable_static_method_key, module_function_key)

0 commit comments

Comments
 (0)