def test_memoized(): @memoized(cache=sql_archive()) def add(x,y): return x+y add(1,2) add(1,2) add(1,3) #print ("sql_cache = %s" % add.__cache__()) _key4 = '((), '+str({'y':3, 'x':1})+')' _key3 = '((), '+str({'y':2, 'x':1})+')' key4_ = '((), '+str({'x':1, 'y':3})+')' key3_ = '((), '+str({'x':1, 'y':2})+')' assert add.__cache__() == {_key4: 4, _key3: 3} or {key4_: 4, key3_: 3} @memoized(cache=dict_archive(cached=False)) # use archive backend 'direcly' def add(x,y): return x+y add(1,2) add(1,2) add(1,3) #print ("dict_cache = %s" % add.__cache__()) assert add.__cache__() == {_key4: 4, _key3: 3} or {key4_: 4, key3_: 3} @memoized(cache=dict()) def add(x,y): return x+y add(1,2) add(1,2) add(1,3) #print ("dict_cache = %s" % add.__cache__()) assert add.__cache__() == {_key4: 4, _key3: 3} or {key4_: 4, key3_: 3} @memoized(cache=add.__cache__()) def add(x,y): return x+y add(1,2) add(2,2) #print ("re_dict_cache = %s" % add.__cache__()) _key2 = '((), '+str({'y':2, 'x':2})+')' key2_ = '((), '+str({'x':2, 'y':2})+')' assert add.__cache__() == {_key4: 4, _key3: 3, _key2: 4} or {key4_: 4, key3_: 3, key2_: 4} @memoized(keymap=dumps) def add(x,y): return x+y add(1,2) add(1,2) add(1,3) #print ("pickle_dict_cache = %s" % add.__cache__()) _pkey4 = dill.dumps(eval(_key4)) _pkey3 = dill.dumps(eval(_key3)) pkey4_ = dill.dumps(eval(key4_)) pkey3_ = dill.dumps(eval(key3_)) assert add.__cache__() == {_pkey4: 4, _pkey3: 3} or {pkey4_: 4, pkey3_: 3}
def test_lru(): @lru_cache(maxsize=3, cache=dict_archive('test'), purge=True) def identity(x): return x identity(1) identity(2) identity(3) ic = identity.__cache__() assert len(ic.keys()) == 3 assert len(ic.archive.keys()) == 0 identity(4) assert len(ic.keys()) == 0 assert len(ic.archive.keys()) == 4 identity(5) assert len(ic.keys()) == 1 assert len(ic.archive.keys()) == 4 @lru_cache(maxsize=3, cache=dict_archive('test'), purge=False) def inverse(x): return -x inverse(1) inverse(2) inverse(3) ic = inverse.__cache__() assert len(ic.keys()) == 3 assert len(ic.archive.keys()) == 0 inverse(4) assert len(ic.keys()) == 3 assert len(ic.archive.keys()) == 1 inverse(5) assert len(ic.keys()) == 3 assert len(ic.archive.keys()) == 2 @lru_cache(maxsize=3, cache=dict_archive('test', cached=False)) def foo(x): return x foo(1) foo(2) foo(3) ic = foo.__cache__() assert len(ic.keys()) == 3 assert len(ic.archive.keys()) == 3 foo(4) assert len(ic.keys()) == 3 assert len(ic.archive.keys()) == 3 foo(5) assert len(ic.keys()) == 3 assert len(ic.archive.keys()) == 3 #XXX: should it be 'correct' expected behavior to ignore purge? @lru_cache(maxsize=3, cache=None, purge=True) def bar(x): return -x bar(1) bar(2) bar(3) ic = bar.__cache__() assert len(ic.keys()) == 3 assert len(ic.archive.keys()) == 0 bar(4) assert len(ic.keys()) == 3 assert len(ic.archive.keys()) == 0 bar(5) assert len(ic.keys()) == 3 assert len(ic.archive.keys()) == 0
bar(2) bar(3) ic = bar.__cache__() assert len(ic.keys()) == 3 assert len(ic.archive.keys()) == 0 bar(4) assert len(ic.keys()) == 3 assert len(ic.archive.keys()) == 0 bar(5) assert len(ic.keys()) == 3 assert len(ic.archive.keys()) == 0 from klepto._cache import no_cache from klepto import archives arch = archives.dict_archive('cache_class') @no_cache(cache=arch) class Parent2: def __init__(self, val): self.val = int(1000 * random()) def test_class(): try: p2 = Parent2(1) except RecursionError: print(321) pass else:
import sys import dill from klepto.archives import cache, sql_archive, dict_archive @memoized(cache=sql_archive()) def add(x,y): return x+y add(1,2) add(1,2) add(1,3) #print ("sql_cache = %s" % add.__cache__()) _key4 = '((), '+str({'y':3, 'x':1})+')' _key3 = '((), '+str({'y':2, 'x':1})+')' assert add.__cache__() == {_key4: 4, _key3: 3} @memoized(cache=dict_archive(cached=False)) # use archive backend 'direcly' def add(x,y): return x+y add(1,2) add(1,2) add(1,3) #print ("dict_cache = %s" % add.__cache__()) _key4 = '((), '+str({'y':3, 'x':1})+')' _key3 = '((), '+str({'y':2, 'x':1})+')' assert add.__cache__() == {_key4: 4, _key3: 3} @memoized(cache=dict()) def add(x,y): return x+y add(1,2) add(1,2)
@memoized(cache=sql_archive()) def add(x, y): return x + y add(1, 2) add(1, 2) add(1, 3) #print ("sql_cache = %s" % add.__cache__()) _key4 = '((), ' + str({'y': 3, 'x': 1}) + ')' _key3 = '((), ' + str({'y': 2, 'x': 1}) + ')' assert add.__cache__() == {_key4: 4, _key3: 3} @memoized(cache=dict_archive(cached=False)) # use archive backend 'direcly' def add(x, y): return x + y add(1, 2) add(1, 2) add(1, 3) #print ("dict_cache = %s" % add.__cache__()) _key4 = '((), ' + str({'y': 3, 'x': 1}) + ')' _key3 = '((), ' + str({'y': 2, 'x': 1}) + ')' assert add.__cache__() == {_key4: 4, _key3: 3} @memoized(cache=dict()) def add(x, y):