GRPCCache
A caching utility for gRPC methods using Redis as a backend.
This class provides decorators to cache gRPC responses based on specific message fields.
Example
from grpc_cache import grpc_cache, setup_grpc_cache
from grpc_cache.backends.redis import RedisSyncBackend
from redis import Redis
from my_proto.messages_pb2 import MyRequest, MyResponse, MyServicer
redis = Redis.from_url("redis://localhost:6379/0")
setup_grpc_cache(backend=RedisSyncBackend(redis=redis), ex=timedelta(minutes=1))
class MyService(MyServicer):
@grpc_cache(fields_for_key=["field1", "field2"], ex=timedelta(minutes=5))
def MyMethod(self, request: MyRequest, context) -> MyResponse:
# Perform some logic
return MyResponse(result="Hello World")
Source code in grpc_cache/cache.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
__call__(fields_for_key=None, ex=None, protobuf=None, prefix=None)
Decorator to enable caching for a gRPC method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fields_for_key
|
list[str] | None
|
List of field names to construct the cache key. |
None
|
ex
|
int | timedelta | None
|
Expiration time for the cache, in seconds or as a timedelta. |
None
|
protobuf
|
Message
|
Protobuf message type for serialization (optional if return type is annotated). |
None
|
prefix
|
str
|
Optional prefix for cache keys. |
None
|
Returns:
| Type | Description |
|---|---|
Callable[[gRPCSyncMethod], gRPCSyncMethod]
|
Decorator function. |
Example
from grpc_cache import grpc_cache
from my_proto.messages_pb2 import MyRequest, MyResponse, MyServicer
class MyService(MyServicer):
@grpc_cache(fields_for_key=["field1", "field2"], ex=timedelta(minutes=5))
def MyMethod(self, request: MyRequest, context) -> MyResponse:
# Perform some logic
return MyResponse(result="Hello World")
def AnotherMethod(self, request: MyRequest, context) -> MyResponse:
self.MyMethod.clear(request.field1, request.field2) # clear cache for MyMethod
return MyResponse(result="Hello World")
Source code in grpc_cache/cache.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
setup(backend, ex)
Configures the gRPC cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ex
|
int | timedelta
|
Default expiration time for the cache, in seconds or as a timedelta. |
required |
backend
|
SyncBackend
|
Backend storage. |
required |