Skip to content

Commit 9c66b52

Browse files
committed
refactor(cache): rename scoped_mode to mode_context
- Updated context manager name from `scoped_mode` to `mode_context` for clarity - Updated related documentation in README and CHANGELOG - Renamed function calls and references to use new `mode_context` name
1 parent a0ff354 commit 9c66b52

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
-**New Features:**
88
- Added arguments excluding support for the `RedisFuncCache` class, which makes it possible to cache functions with arguments that cannot be serialized.
99
- Added support for controlling cache TTL update behavior with `update_ttl` parameter.
10-
- Enhanced cache mode control with bit-flag based `Mode` enum and new context managers:
11-
- `mask_mode()` for applying mode masks using bitwise AND operations
10+
- Enhanced cache mode control with context managers:
11+
- `mode_context()` for applying mode contextually
1212
- `disable_rw()` as an alias for completely disabling cache read and write operations
1313
- `read_only()` for read-only cache mode
1414
- `write_only()` for write-only cache mode

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ The library guarantees thread safety and concurrency security through the follow
179179

180180
1. Contextual State Isolation
181181

182-
The [ContextVar](https://docs.python.org/3/library/contextvars.html#contextvars.ContextVar) based `scoped_mode()` context manager and other cache control context managers ensure thread and coroutine isolation. Each thread or async task maintains its own independent state, preventing cross-context interference.
182+
The [ContextVar](https://docs.python.org/3/library/contextvars.html#contextvars.ContextVar) based `mode_context()` context manager and other cache control context managers ensure thread and coroutine isolation. Each thread or async task maintains its own independent state, preventing cross-context interference.
183183
184184
Atomicity is a key feature of this library. All cache operations (both read and write) are implemented using Redis Lua scripts, which are executed atomically by the Redis server. This means that each script runs in its entirety without being interrupted by other operations, ensuring data consistency even under high concurrent load.
185185
@@ -592,13 +592,13 @@ The `update_ttl` parameter controls the behavior of the cache data structures (s
592592
593593
### Cache Mode Control
594594
595-
The library provides fine-grained control over cache behavior through the `scoped_mode()` context manager and convenience methods. You can control whether the cache reads from or writes to Redis using the following flags:
595+
The library provides fine-grained control over cache behavior through the `mode_context()` context manager and convenience methods. You can control whether the cache reads from or writes to Redis using the following flags:
596596
597597
- `read` (`bool`): allow read from cache
598598
- `write` (`bool`): allow write to cache
599599
- `exec` (`bool`): allow execute function
600600
601-
You can use the `scoped_mode()` context manager to explicitly set any mode:
601+
You can use the `mode_context()` context manager to explicitly set any mode:
602602
603603
```python
604604
from redis_func_cache import RedisFuncCache
@@ -614,20 +614,20 @@ data = get_user_data(123)
614614
# Bypass cache reading, but still write to cache
615615
mode = cache.get_mode()
616616
mode.read = False
617-
with cache.scoped_mode(mode):
617+
with cache.mode_context(mode):
618618
data = get_user_data(123) # Function executed, result stored in cache
619619
620620
# Only read from cache, don't execute function or write to cache
621621
mode = cache.get_mode()
622622
mode.write = False
623-
with cache.scoped_mode(mode):
623+
with cache.mode_context(mode):
624624
data = get_user_data(123) # Only attempts to read from cache
625625
626626
# Disable cache read and write
627627
mode = cache.get_mode()
628628
mode.read = False
629629
mode.write = False
630-
with cache.scoped_mode(mode):
630+
with cache.mode_context(mode):
631631
data = get_user_data(123) # Function executed, no cache interaction
632632
```
633633

src/redis_func_cache/cache.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ def reset_mode(self, token: Token[RedisFuncCache.Mode]):
870870
self._mode.reset(token)
871871

872872
@contextmanager
873-
def scoped_mode(self, mode: RedisFuncCache.Mode) -> Generator[None, None, None]:
873+
def mode_context(self, mode: RedisFuncCache.Mode) -> Generator[None, None, None]:
874874
"""A context manager to control cache behavior.
875875
876876
This context manager allows you to temporarily change cache mode flags.
@@ -881,7 +881,6 @@ def scoped_mode(self, mode: RedisFuncCache.Mode) -> Generator[None, None, None]:
881881
Args:
882882
mode: The cache mode to use within the context. Can be a combination of Mode bitwise flags.
883883
884-
885884
Example:
886885
887886
::
@@ -924,7 +923,7 @@ def func(): ...
924923
.. versionadded:: 0.5
925924
"""
926925
mode = replace(self._mode.get(), read=False, write=False)
927-
with self.scoped_mode(mode):
926+
with self.mode_context(mode):
928927
yield
929928

930929
@contextmanager
@@ -949,7 +948,7 @@ def func(): ...
949948
.. versionadded:: 0.5
950949
"""
951950
mode = replace(self._mode.get(), read=True, write=False)
952-
with self.scoped_mode(mode):
951+
with self.mode_context(mode):
953952
yield
954953

955954
@contextmanager
@@ -970,5 +969,5 @@ def func(): ...
970969
.. versionadded:: 0.5
971970
"""
972971
mode = replace(self._mode.get(), read=False, write=True)
973-
with self.scoped_mode(mode):
972+
with self.mode_context(mode):
974973
yield

0 commit comments

Comments
 (0)