Skip to content

Commit ff03aa1

Browse files
committed
feat: Use cachetools for caching of methods
This is more simple than using hacks with `functools`' `lru_cache`
1 parent e801a62 commit ff03aa1

File tree

3 files changed

+8
-17
lines changed

3 files changed

+8
-17
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ name = "viur-shop"
1010
dynamic = ["version"]
1111
dependencies = [
1212
"viur-toolkit>=0.2.0",
13-
"viur-core>=3.7.0"
13+
"viur-core>=3.7.0",
14+
"cachetools>=5.0"
1415
]
1516
requires-python = ">=3.11"
1617
authors = [

src/viur/shop/modules/discount.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def can_apply(
203203
return dv.is_fulfilled, dv
204204

205205
@property
206-
@functools.cache
206+
@cachetools.cached(cache=cachetools.TTLCache(maxsize=1024, ttl=3600))
207207
def current_automatically_discounts(self) -> list[SkeletonInstance_T[DiscountSkel]]:
208208
query = self.viewSkel().all().filter("activate_automatically =", True)
209209
discounts = []

src/viur/shop/modules/discount_condition.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import functools
2-
import pprint
31
import random
42
import string
5-
import time
63
import typing as t
74

5+
import cachetools
86
from viur import toolkit
97
from viur.core import current, db, tasks
108
from viur.core.prototypes import List
119
from viur.core.skeleton import SkeletonInstance
10+
1211
from .abstract import ShopModuleAbstract
1312
from ..globals import SHOP_INSTANCE, SHOP_LOGGER
1413
from ..services import Event, on_event
@@ -24,11 +23,6 @@
2423
SUFFIX_LENGTH = 6
2524

2625

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-
3226
class DiscountCondition(ShopModuleAbstract, List):
3327
moduleName = "discount_condition"
3428
kindName = "{{viur_shop_modulename}}_discount_condition"
@@ -156,13 +150,9 @@ def generate_subcodes(self, parent_key: db.Key, prefix: str, amount: int):
156150
# --- Helpers ------------------------------------------------------------
157151

158152
@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=})")
166156
skel = SHOP_INSTANCE.get().discount_condition.viewSkel()
167157
if not skel.read(key):
168158
return None

0 commit comments

Comments
 (0)