summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/elements/element.py21
-rw-r--r--lib/utils/cache.py22
2 files changed, 23 insertions, 20 deletions
diff --git a/lib/elements/element.py b/lib/elements/element.py
index 5df40475..f285258b 100644
--- a/lib/elements/element.py
+++ b/lib/elements/element.py
@@ -2,14 +2,10 @@
#
# Copyright (c) 2010 Authors
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
-import atexit
-import os
import sys
from copy import deepcopy
import numpy as np
-import appdirs
-import diskcache
import inkex
from inkex import bezier
@@ -21,6 +17,7 @@ from ..svg import (PIXELS_PER_MM, apply_transforms, convert_length,
get_node_transform)
from ..svg.tags import INKSCAPE_LABEL, INKSTITCH_ATTRIBS
from ..utils import Point, cache
+from ..utils.cache import get_stitch_plan_cache
class Param(object):
@@ -396,25 +393,13 @@ class EmbroideryElement(object):
def to_stitch_groups(self, last_patch):
raise NotImplementedError("%s must implement to_stitch_groups()" % self.__class__.__name__)
- @classmethod
- def _get_stitch_plan_cache(cls):
- # one cache, shared by all elements, opened once and closed at program exit
- try:
- return cls._stitch_plan_cache
- except AttributeError:
- cache_dir = os.path.join(appdirs.user_config_dir('inkstitch'), 'cache', 'stitch_plan')
- cls._stitch_plan_cache = diskcache.Cache(cache_dir, size=1024 * 1024 * 100)
- atexit.register(cls._stitch_plan_cache.close)
- return cls._stitch_plan_cache
-
@debug.time
def _load_cached_stitch_groups(self):
- stitch_plan_cache = self._get_stitch_plan_cache()
- return stitch_plan_cache.get(self.node.get('id'))
+ return get_stitch_plan_cache().get(self.node.get('id'))
@debug.time
def _save_cached_stitch_groups(self, stitch_groups):
- stitch_plan_cache = self._get_stitch_plan_cache()
+ stitch_plan_cache = get_stitch_plan_cache()
stitch_plan_cache[self.node.get('id')] = stitch_groups
def embroider(self, last_stitch_group):
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