Skip to content

Commit

Permalink
chore: lazy redis connection
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Oct 21, 2023
1 parent 4d1969c commit 306f937
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions ckanext/toolbelt/utils/cache.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import logging
import pickle # nosec: B403
from functools import update_wrapper
from functools import cached_property, update_wrapper
from typing import Any, Callable, Generic, Optional, Protocol, TypeVar, Union, cast

from typing_extensions import ParamSpec
Expand Down Expand Up @@ -72,7 +72,6 @@ def strategy(*args: Any, **kwargs: Any):
class Cache(Generic[T]):
duration: DurationStrategy
key: KeyStrategy
conn: "redis.Redis[bytes]"
dumper: Dumper[T]
loader: Loader[T]

Expand All @@ -93,7 +92,9 @@ def __init__(
key = constantly(key)
self.key = key

self.conn = redis.connect_to_redis()
@cached_property
def conn(self):
return cast("redis.Redis[bytes]", redis.connect_to_redis())

@staticmethod
def dont_cache(value: T):
Expand Down

0 comments on commit 306f937

Please sign in to comment.