cachify 0.1.0__py3-none-any.whl → 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cachify/__init__.py +24 -22
- cachify/cache.py +116 -116
- cachify/config/__init__.py +4 -4
- cachify/features/never_die.py +219 -219
- cachify/memory_cache.py +37 -37
- cachify/redis/__init__.py +19 -19
- cachify/redis/config.py +115 -115
- cachify/redis/lock.py +232 -232
- cachify/redis_cache.py +27 -27
- cachify/storage/__init__.py +9 -9
- cachify/storage/memory_storage.py +52 -52
- cachify/storage/redis_storage.py +138 -138
- cachify/types/__init__.py +95 -95
- cachify/utils/arguments.py +65 -65
- cachify/utils/decorator_factory.py +44 -44
- cachify/utils/functions.py +10 -10
- cachify/utils/locks.py +6 -6
- {cachify-0.1.0.dist-info → cachify-0.2.0.dist-info}/METADATA +4 -3
- cachify-0.2.0.dist-info/RECORD +24 -0
- {cachify-0.1.0.dist-info → cachify-0.2.0.dist-info}/WHEEL +1 -1
- {cachify-0.1.0.dist-info → cachify-0.2.0.dist-info/licenses}/LICENSE +21 -21
- cachify-0.1.0.dist-info/RECORD +0 -24
- {cachify-0.1.0.dist-info → cachify-0.2.0.dist-info}/entry_points.txt +0 -0
cachify/redis/config.py
CHANGED
|
@@ -1,115 +1,115 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
from dataclasses import dataclass
|
|
4
|
-
from typing import TYPE_CHECKING, Literal, get_args, overload
|
|
5
|
-
|
|
6
|
-
if TYPE_CHECKING:
|
|
7
|
-
from redis import Redis
|
|
8
|
-
from redis.asyncio import Redis as AsyncRedis
|
|
9
|
-
|
|
10
|
-
OnErrorType = Literal["silent", "raise"]
|
|
11
|
-
|
|
12
|
-
DEFAULT_KEY_PREFIX = "cachify"
|
|
13
|
-
DEFAULT_LOCK_TIMEOUT = 10
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
@dataclass
|
|
17
|
-
class RedisConfig:
|
|
18
|
-
"""Configuration for Redis cache backend."""
|
|
19
|
-
|
|
20
|
-
sync_client: Redis | None
|
|
21
|
-
async_client: AsyncRedis | None
|
|
22
|
-
key_prefix: str
|
|
23
|
-
lock_timeout: int
|
|
24
|
-
on_error: OnErrorType
|
|
25
|
-
|
|
26
|
-
@overload
|
|
27
|
-
def get_client(self, is_async: Literal[True]) -> AsyncRedis: ...
|
|
28
|
-
|
|
29
|
-
@overload
|
|
30
|
-
def get_client(self, is_async: Literal[False]) -> Redis: ...
|
|
31
|
-
|
|
32
|
-
def get_client(self, is_async: bool) -> Redis | AsyncRedis:
|
|
33
|
-
"""Get the appropriate Redis client based on is_async flag."""
|
|
34
|
-
if is_async:
|
|
35
|
-
client = self.async_client
|
|
36
|
-
else:
|
|
37
|
-
client = self.sync_client
|
|
38
|
-
|
|
39
|
-
if not client:
|
|
40
|
-
mode = "async" if is_async else "sync"
|
|
41
|
-
raise RuntimeError(
|
|
42
|
-
f"Redis {mode} client not configured."
|
|
43
|
-
f"Provide {mode}_client in setup_redis_config() to use @redis_cache on {mode} functions."
|
|
44
|
-
)
|
|
45
|
-
return client
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
_redis_config: RedisConfig | None = None
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
def setup_redis_config(
|
|
52
|
-
sync_client: Redis | None = None,
|
|
53
|
-
async_client: AsyncRedis | None = None,
|
|
54
|
-
key_prefix: str = DEFAULT_KEY_PREFIX,
|
|
55
|
-
lock_timeout: int = DEFAULT_LOCK_TIMEOUT,
|
|
56
|
-
on_error: OnErrorType = "silent",
|
|
57
|
-
):
|
|
58
|
-
"""
|
|
59
|
-
Configure the Redis cache backend.
|
|
60
|
-
|
|
61
|
-
Must be called before using @redis_cache decorator.
|
|
62
|
-
Can only be called once. Use reset_redis_config() first if reconfiguration is needed.
|
|
63
|
-
|
|
64
|
-
Args:
|
|
65
|
-
sync_client: Redis sync client instance (redis.Redis)
|
|
66
|
-
async_client: Redis async client instance (redis.asyncio.Redis)
|
|
67
|
-
key_prefix: Prefix for all cache keys in Redis (default: "cache")
|
|
68
|
-
lock_timeout: Timeout in seconds for distributed locks (default: 10)
|
|
69
|
-
on_error: Error handling mode - "silent" treats errors as cache miss,
|
|
70
|
-
"raise" propagates exceptions (default: "silent")
|
|
71
|
-
|
|
72
|
-
Raises:
|
|
73
|
-
ValueError: If neither sync_client nor async_client is provided
|
|
74
|
-
RuntimeError: If called twice without reset_redis_config() first
|
|
75
|
-
"""
|
|
76
|
-
global _redis_config
|
|
77
|
-
|
|
78
|
-
if _redis_config is not None:
|
|
79
|
-
raise RuntimeError("Redis config already set. Call reset_redis_config() first if you need to reconfigure.")
|
|
80
|
-
|
|
81
|
-
if sync_client is None and async_client is None:
|
|
82
|
-
raise ValueError("At least one of sync_client or async_client must be provided")
|
|
83
|
-
|
|
84
|
-
if on_error not in get_args(OnErrorType):
|
|
85
|
-
raise ValueError(f"on_error must be one of {get_args(OnErrorType)}")
|
|
86
|
-
|
|
87
|
-
_redis_config = RedisConfig(
|
|
88
|
-
sync_client=sync_client,
|
|
89
|
-
async_client=async_client,
|
|
90
|
-
key_prefix=key_prefix,
|
|
91
|
-
lock_timeout=lock_timeout,
|
|
92
|
-
on_error=on_error,
|
|
93
|
-
)
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
def get_redis_config() -> RedisConfig:
|
|
97
|
-
"""
|
|
98
|
-
Get the current Redis configuration.
|
|
99
|
-
|
|
100
|
-
Raises:
|
|
101
|
-
RuntimeError: If setup_redis_config() has not been called
|
|
102
|
-
"""
|
|
103
|
-
if _redis_config is None:
|
|
104
|
-
raise RuntimeError("Redis not configured. Call setup_redis_config() before using @redis_cache")
|
|
105
|
-
return _redis_config
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
def reset_redis_config():
|
|
109
|
-
"""
|
|
110
|
-
Reset the Redis configuration to allow reconfiguration.
|
|
111
|
-
|
|
112
|
-
Useful for testing or when you need to change Redis connection settings.
|
|
113
|
-
"""
|
|
114
|
-
global _redis_config
|
|
115
|
-
_redis_config = None
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import TYPE_CHECKING, Literal, get_args, overload
|
|
5
|
+
|
|
6
|
+
if TYPE_CHECKING:
|
|
7
|
+
from redis import Redis
|
|
8
|
+
from redis.asyncio import Redis as AsyncRedis
|
|
9
|
+
|
|
10
|
+
OnErrorType = Literal["silent", "raise"]
|
|
11
|
+
|
|
12
|
+
DEFAULT_KEY_PREFIX = "cachify"
|
|
13
|
+
DEFAULT_LOCK_TIMEOUT = 10
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass
|
|
17
|
+
class RedisConfig:
|
|
18
|
+
"""Configuration for Redis cache backend."""
|
|
19
|
+
|
|
20
|
+
sync_client: Redis | None
|
|
21
|
+
async_client: AsyncRedis | None
|
|
22
|
+
key_prefix: str
|
|
23
|
+
lock_timeout: int
|
|
24
|
+
on_error: OnErrorType
|
|
25
|
+
|
|
26
|
+
@overload
|
|
27
|
+
def get_client(self, is_async: Literal[True]) -> AsyncRedis: ...
|
|
28
|
+
|
|
29
|
+
@overload
|
|
30
|
+
def get_client(self, is_async: Literal[False]) -> Redis: ...
|
|
31
|
+
|
|
32
|
+
def get_client(self, is_async: bool) -> Redis | AsyncRedis:
|
|
33
|
+
"""Get the appropriate Redis client based on is_async flag."""
|
|
34
|
+
if is_async:
|
|
35
|
+
client = self.async_client
|
|
36
|
+
else:
|
|
37
|
+
client = self.sync_client
|
|
38
|
+
|
|
39
|
+
if not client:
|
|
40
|
+
mode = "async" if is_async else "sync"
|
|
41
|
+
raise RuntimeError(
|
|
42
|
+
f"Redis {mode} client not configured."
|
|
43
|
+
f"Provide {mode}_client in setup_redis_config() to use @redis_cache on {mode} functions."
|
|
44
|
+
)
|
|
45
|
+
return client
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
_redis_config: RedisConfig | None = None
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def setup_redis_config(
|
|
52
|
+
sync_client: Redis | None = None,
|
|
53
|
+
async_client: AsyncRedis | None = None,
|
|
54
|
+
key_prefix: str = DEFAULT_KEY_PREFIX,
|
|
55
|
+
lock_timeout: int = DEFAULT_LOCK_TIMEOUT,
|
|
56
|
+
on_error: OnErrorType = "silent",
|
|
57
|
+
):
|
|
58
|
+
"""
|
|
59
|
+
Configure the Redis cache backend.
|
|
60
|
+
|
|
61
|
+
Must be called before using @redis_cache decorator.
|
|
62
|
+
Can only be called once. Use reset_redis_config() first if reconfiguration is needed.
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
sync_client: Redis sync client instance (redis.Redis)
|
|
66
|
+
async_client: Redis async client instance (redis.asyncio.Redis)
|
|
67
|
+
key_prefix: Prefix for all cache keys in Redis (default: "cache")
|
|
68
|
+
lock_timeout: Timeout in seconds for distributed locks (default: 10)
|
|
69
|
+
on_error: Error handling mode - "silent" treats errors as cache miss,
|
|
70
|
+
"raise" propagates exceptions (default: "silent")
|
|
71
|
+
|
|
72
|
+
Raises:
|
|
73
|
+
ValueError: If neither sync_client nor async_client is provided
|
|
74
|
+
RuntimeError: If called twice without reset_redis_config() first
|
|
75
|
+
"""
|
|
76
|
+
global _redis_config
|
|
77
|
+
|
|
78
|
+
if _redis_config is not None:
|
|
79
|
+
raise RuntimeError("Redis config already set. Call reset_redis_config() first if you need to reconfigure.")
|
|
80
|
+
|
|
81
|
+
if sync_client is None and async_client is None:
|
|
82
|
+
raise ValueError("At least one of sync_client or async_client must be provided")
|
|
83
|
+
|
|
84
|
+
if on_error not in get_args(OnErrorType):
|
|
85
|
+
raise ValueError(f"on_error must be one of {get_args(OnErrorType)}")
|
|
86
|
+
|
|
87
|
+
_redis_config = RedisConfig(
|
|
88
|
+
sync_client=sync_client,
|
|
89
|
+
async_client=async_client,
|
|
90
|
+
key_prefix=key_prefix,
|
|
91
|
+
lock_timeout=lock_timeout,
|
|
92
|
+
on_error=on_error,
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def get_redis_config() -> RedisConfig:
|
|
97
|
+
"""
|
|
98
|
+
Get the current Redis configuration.
|
|
99
|
+
|
|
100
|
+
Raises:
|
|
101
|
+
RuntimeError: If setup_redis_config() has not been called
|
|
102
|
+
"""
|
|
103
|
+
if _redis_config is None:
|
|
104
|
+
raise RuntimeError("Redis not configured. Call setup_redis_config() before using @redis_cache")
|
|
105
|
+
return _redis_config
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def reset_redis_config():
|
|
109
|
+
"""
|
|
110
|
+
Reset the Redis configuration to allow reconfiguration.
|
|
111
|
+
|
|
112
|
+
Useful for testing or when you need to change Redis connection settings.
|
|
113
|
+
"""
|
|
114
|
+
global _redis_config
|
|
115
|
+
_redis_config = None
|