|
1 | | -import functools |
2 | | -import pprint |
3 | 1 | import random |
4 | 2 | import string |
5 | | -import time |
6 | 3 | import typing as t |
7 | 4 |
|
| 5 | +import cachetools |
8 | 6 | from viur import toolkit |
9 | 7 | from viur.core import current, db, tasks |
10 | 8 | from viur.core.prototypes import List |
11 | 9 | from viur.core.skeleton import SkeletonInstance |
| 10 | + |
12 | 11 | from .abstract import ShopModuleAbstract |
13 | 12 | from ..globals import SHOP_INSTANCE, SHOP_LOGGER |
14 | 13 | from ..services import Event, on_event |
|
24 | 23 | SUFFIX_LENGTH = 6 |
25 | 24 |
|
26 | 25 |
|
27 | | -def get_ttl_hash(seconds: int = 3600) -> int: |
28 | | - """Return the same value withing `seconds` time period""" |
29 | | - return round(time.time() / seconds) |
30 | | - |
31 | | - |
32 | 26 | class DiscountCondition(ShopModuleAbstract, List): |
33 | 27 | moduleName = "discount_condition" |
34 | 28 | kindName = "{{viur_shop_modulename}}_discount_condition" |
@@ -156,13 +150,9 @@ def generate_subcodes(self, parent_key: db.Key, prefix: str, amount: int): |
156 | 150 | # --- Helpers ------------------------------------------------------------ |
157 | 151 |
|
158 | 152 | @classmethod |
159 | | - def get_skel(cls, key: db.Key, expires: int = 3600) -> SkeletonInstance_T["DiscountConditionSkel"] | None: |
160 | | - return cls._get_skel(key, ttl_hash=get_ttl_hash(expires)) |
161 | | - |
162 | | - @staticmethod |
163 | | - @functools.lru_cache() |
164 | | - def _get_skel(key: db.Key, ttl_hash: int | None = None) -> SkeletonInstance_T["DiscountConditionSkel"] | None: |
165 | | - # logger.debug(f"_get_skel({key=}, {ttl_hash=})") |
| 153 | + @cachetools.cached(cache=cachetools.TTLCache(maxsize=1024, ttl=3600)) |
| 154 | + def get_skel(cls, key: db.Key) -> SkeletonInstance_T["DiscountConditionSkel"] | None: |
| 155 | + # logger.debug(f"get_skel({key=})") |
166 | 156 | skel = SHOP_INSTANCE.get().discount_condition.viewSkel() |
167 | 157 | if not skel.read(key): |
168 | 158 | return None |
|
0 commit comments