From 0e225277dbb57bdaf850a0c67b4fc988051af800 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Sat, 16 Jul 2022 15:44:01 -0400 Subject: move get_stitch_plan_cache() to utils.cache --- lib/utils/cache.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'lib/utils/cache.py') diff --git a/lib/utils/cache.py b/lib/utils/cache.py index c0313ebe..46d8ec59 100644 --- a/lib/utils/cache.py +++ b/lib/utils/cache.py @@ -2,14 +2,32 @@ # # Copyright (c) 2010 Authors # Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. +import os +import atexit + +import appdirs +import diskcache try: from functools import lru_cache except ImportError: from backports.functools_lru_cache import lru_cache -# simplify use of lru_cache decorator - +# simplify use of lru_cache decorator def cache(*args, **kwargs): return lru_cache(maxsize=None)(*args, **kwargs) + + +__stitch_plan_cache = None + + +def get_stitch_plan_cache(): + global __stitch_plan_cache + + if __stitch_plan_cache is None: + cache_dir = os.path.join(appdirs.user_config_dir('inkstitch'), 'cache', 'stitch_plan') + __stitch_plan_cache = diskcache.Cache(cache_dir, size=1024 * 1024 * 100) + atexit.register(__stitch_plan_cache.close) + + return __stitch_plan_cache -- cgit v1.2.3 From d51feec98d7b4e4b224c34c013da6df059b78005 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Thu, 21 Jul 2022 23:16:56 -0400 Subject: cache key generation using params, path, color, and style --- lib/utils/cache.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'lib/utils/cache.py') diff --git a/lib/utils/cache.py b/lib/utils/cache.py index 46d8ec59..767978ca 100644 --- a/lib/utils/cache.py +++ b/lib/utils/cache.py @@ -4,6 +4,8 @@ # Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. import os import atexit +import hashlib +import pickle import appdirs import diskcache @@ -31,3 +33,38 @@ def get_stitch_plan_cache(): atexit.register(__stitch_plan_cache.close) return __stitch_plan_cache + + +class CacheKeyGenerator(object): + """Generate cache keys given arbitrary data. + + Given arbitrary data, generate short cache key that is extremely likely + to be unique. + + Use example: + + >>> generator = CacheKeyGenerator() + >>> generator.update(b'12345') + >>> generator.update([1, 2, 3, {4, 5, 6}]) + >>> generator.get_cache_key() + """ + + def __init__(self): + # SHA1 is chosen for speed. We don't need cryptography-grade hashing + # for this use case. + self._hasher = hashlib.sha1() + + def update(self, data): + """Provide data to be hashed into a cache key. + + Arguments: + data -- a bytes object or any object that can be pickled + """ + + if not isinstance(data, bytes): + data = pickle.dumps(data) + + self._hasher.update(data) + + def get_cache_key(self): + return self._hasher.hexdigest() -- cgit v1.2.3 From 98bc2e2ff9c843a64c3db355290ed541e6708312 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Fri, 29 Jul 2022 18:17:50 -0400 Subject: add preferences UI including cache settings --- lib/utils/cache.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'lib/utils/cache.py') diff --git a/lib/utils/cache.py b/lib/utils/cache.py index 767978ca..1ede8ce1 100644 --- a/lib/utils/cache.py +++ b/lib/utils/cache.py @@ -10,6 +10,8 @@ import pickle import appdirs import diskcache +from lib.utils.settings import global_settings + try: from functools import lru_cache except ImportError: @@ -29,7 +31,9 @@ def get_stitch_plan_cache(): if __stitch_plan_cache is None: cache_dir = os.path.join(appdirs.user_config_dir('inkstitch'), 'cache', 'stitch_plan') - __stitch_plan_cache = diskcache.Cache(cache_dir, size=1024 * 1024 * 100) + size_limit = global_settings['cache_size'] * 1024 * 1024 + __stitch_plan_cache = diskcache.Cache(cache_dir, size=size_limit) + __stitch_plan_cache.size_limit = size_limit atexit.register(__stitch_plan_cache.close) return __stitch_plan_cache -- cgit v1.2.3