diff --git a/cachier/pickle_core.py b/cachier/pickle_core.py index fd40a27..a32d1ac 100644 --- a/cachier/pickle_core.py +++ b/cachier/pickle_core.py @@ -8,6 +8,7 @@ # Copyright (c) 2016, Shay Palachy import os +from zlib import adler32 import pickle # for local caching from datetime import datetime import threading @@ -166,7 +167,7 @@ class _PickleCore(_BaseCore): except ImportError: # pragma: no cover pass if hasattr(value, "tobytes"): # For numpy - return hash(value.tobytes()) + return adler32(value.tobytes()) & 0xffffffff if hasattr(value, "__iter__"): # For iterators if isinstance(value, (list, tuple)): hash_array = [] @@ -179,7 +180,7 @@ class _PickleCore(_BaseCore): hash_array.append(key) hash_array.append(self._hash_args(elem)) return tuple(hash_array) - return hash(value) + return adler32(pickle.dumps(value)) & 0xffffffff def set_entry(self, key, func_res): with self.lock: diff --git a/tests/prepare_text_caching_test.py b/tests/prepare_text_caching_test.py index 3ea30ad..f315b24 100644 --- a/tests/prepare_text_caching_test.py +++ b/tests/prepare_text_caching_test.py @@ -4,7 +4,9 @@ from cachier import cachier from time import sleep from random import random -from pickle import dump +from pickle import dump, dumps +from zlib import adler32 + TEXT_VAL_TO_CHECK = 'foo' TEXT_CACHE_FNAME = 'cachier_text_cache_temp.pkl' @@ -14,11 +16,12 @@ TEXT_CACHE_FNAME = 'cachier_text_cache_temp.pkl' def text_caching(text): sleep(1) print(text) - print(hash(text)) + print(adler32(dumps(text)) & 0xffffffff) return random() if __name__ == '__main__': + text_caching.clear_cache() return_val = text_caching(TEXT_VAL_TO_CHECK) print(return_val) with open(TEXT_CACHE_FNAME, 'wb+') as f: diff --git a/tests/test_pickle_core.py b/tests/test_pickle_core.py index 9b7e84a..44d6eaa 100644 --- a/tests/test_pickle_core.py +++ b/tests/test_pickle_core.py @@ -402,13 +402,14 @@ def test_pickle_core_custom_cache_dir(): def test_text_hashing(): - text = TEXT_VAL_TO_CHECK with open(TEXT_CACHE_FNAME, 'rb') as f: first = load(f) print('\npickled return val found for text cache text:') print(first) start_time = time() - second = text_caching(text) + print('calling with value:') + print(TEXT_VAL_TO_CHECK) + second = text_caching(TEXT_VAL_TO_CHECK) print('second value returned:') print(second) call_time = time() - start_time