0
# Redis Cache Operations
1
2
Redis-backed global cache implementation for Google Cloud NDB operations. Provides efficient caching with Redis as the backend storage for NDB entities and query results.
3
4
## Capabilities
5
6
### Cache Initialization
7
8
Create and configure Redis cache instances for NDB global caching.
9
10
```python { .api }
11
class RedisCache(GlobalCache):
12
def __init__(self, redis, strict_read: bool = False, strict_write: bool = False) -> None:
13
"""
14
Initialize Redis cache with a Redis client instance.
15
16
Parameters:
17
- redis: Redis client instance (from redis-py library)
18
- strict_read: If True, raises exceptions on read errors instead of treating as cache miss
19
- strict_write: If True, raises exceptions on write errors instead of ignoring
20
"""
21
22
@classmethod
23
def from_environment(cls, strict_read: bool = False, strict_write: bool = False) -> Self:
24
"""
25
Create Redis cache instance using environment variables for configuration.
26
27
Parameters:
28
- strict_read: If True, raises exceptions on read errors
29
- strict_write: If True, raises exceptions on write errors
30
31
Returns:
32
RedisCache instance configured from environment
33
"""
34
```
35
36
### Cache Operations
37
38
Core caching operations for storing, retrieving, and managing cached data.
39
40
```python { .api }
41
def get(self, keys):
42
"""
43
Retrieve cached values for the given keys.
44
45
Parameters:
46
- keys: List of cache keys to retrieve
47
48
Returns:
49
Dictionary mapping keys to cached values, or None for missing keys
50
"""
51
52
def set(self, items, expires: Incomplete | None = None) -> None:
53
"""
54
Store items in the cache with optional expiration.
55
56
Parameters:
57
- items: Dictionary of key-value pairs to cache
58
- expires: Optional expiration time for cached items
59
"""
60
61
def delete(self, keys) -> None:
62
"""
63
Remove items from the cache.
64
65
Parameters:
66
- keys: List of cache keys to delete
67
"""
68
69
def clear(self) -> None:
70
"""
71
Clear all items from the cache.
72
"""
73
```
74
75
### Watch and Compare-and-Swap Operations
76
77
Advanced cache operations for optimistic concurrency control.
78
79
```python { .api }
80
def watch(self, items) -> None:
81
"""
82
Watch cache keys for changes to support compare-and-swap operations.
83
84
Parameters:
85
- items: Dictionary of keys and expected values to watch
86
"""
87
88
def unwatch(self, keys) -> None:
89
"""
90
Stop watching the specified cache keys.
91
92
Parameters:
93
- keys: List of keys to stop watching
94
"""
95
96
def compare_and_swap(self, items, expires: Incomplete | None = None):
97
"""
98
Atomically update cache items only if they haven't changed since being watched.
99
100
Parameters:
101
- items: Dictionary of key-value pairs to update
102
- expires: Optional expiration time for updated items
103
104
Returns:
105
Success status of the compare-and-swap operation
106
"""
107
```
108
109
### Pipeline Support
110
111
Access to Redis pipeline functionality for batch operations.
112
113
```python { .api }
114
@property
115
def pipes(self):
116
"""
117
Access to Redis pipeline objects for efficient batch operations.
118
119
Returns:
120
Redis pipeline objects for batching commands
121
"""
122
```
123
124
## Usage Examples
125
126
### Basic Redis Cache Setup
127
128
```python
129
from google.cloud.ndb import RedisCache
130
import redis
131
132
# Create Redis client
133
redis_client = redis.Redis(
134
host='localhost',
135
port=6379,
136
db=0,
137
decode_responses=True
138
)
139
140
# Create Redis cache with strict error handling
141
cache = RedisCache(
142
redis_client,
143
strict_read=True,
144
strict_write=True
145
)
146
```
147
148
### Using with NDB Context
149
150
```python
151
from google.cloud import ndb
152
from google.cloud.ndb import RedisCache
153
import redis
154
155
# Setup Redis cache
156
redis_client = redis.Redis(host='localhost', port=6379, db=0)
157
cache = RedisCache(redis_client)
158
159
# Use with NDB client
160
client = ndb.Client()
161
with client.context(global_cache=cache):
162
# All NDB operations will use Redis caching
163
class MyModel(ndb.Model):
164
name = ndb.StringProperty()
165
166
# These operations will be cached in Redis
167
entity = MyModel(name="example")
168
key = entity.put()
169
retrieved = key.get()
170
```
171
172
### Environment-based Configuration
173
174
```python
175
from google.cloud.ndb import RedisCache
176
177
# Create Redis cache from environment variables
178
# Expects REDIS_HOST, REDIS_PORT, etc. to be configured
179
cache = RedisCache.from_environment(
180
strict_read=False,
181
strict_write=True
182
)
183
```