Skip to content

Commit 60bdb28

Browse files
committed
Redis: improvements
1 parent 4ecc181 commit 60bdb28

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

conceptual/Caching/Backends/caching-redis.md

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ LoggingServices.DefaultBackend.DefaultVerbosity.SetMinimalLevel( LogLevel.Warnin
127127
128128
In-memory cache must be enabled globally for the whole back-end. It is not possible to enable it at the level of individual classes or methods.
129129

130-
## Exception handling
130+
### Exception handling
131131

132132
Failures are highly likely if your system is overloaded. Here is how they are handled:
133133

@@ -136,7 +136,7 @@ Failures are highly likely if your system is overloaded. Here is how they are ha
136136

137137
The exception handling policy described above is abstracted by the <xref:PostSharp.Patterns.Caching.Resilience.IExceptionHandlingPolicy> interface and configured by the <xref:PostSharp.Patterns.Caching.Backends.Redis.RedisCachingBackendConfiguration.ExceptionHandlingPolicy> property.
138138

139-
## Performance tuning
139+
### Performance tuning
140140

141141
With any level of load, it is recommended to enable cache key compression by assigning the <xref:PostSharp.Patterns.Caching.CachingServices.DefaultKeyBuilder?CachingServices.DefaultKeyBuilder> property to a <xref:PostSharp.Patterns.Caching.Implementation.CacheKeyBuilder> instance with a non-default <xref:PostSharp.Patterns.Caching.Implementation.CacheKeyHashingAlgorithm>.
142142

@@ -152,31 +152,40 @@ The following <xref:PostSharp.Patterns.Caching.Backends.Redis.RedisCacheDependen
152152

153153
Note that you can run a manual cleanup by calling the <xref:PostSharp.Patterns.Caching.Implementation.CachingBackend.CleanUpAsync*?CachingBackend.CleanUpAsync> method. Do not run this method with the default options on a production database; these options are optimized for the cleanup operation's performance and may overload your server.
154154

155+
### Monitoring
156+
157+
The following metrics are relevant to assess the health of your caching set up:
158+
159+
- Number of cache evictions per second. A high number might mean either insufficient caching memory, or ineffective caching strategy - caching things that are not worth it (too many cache misses)
160+
- Number of cache expirations per second. A high number might mean too small expiration delays
161+
- Number of warnings per minute in the caching component. A high number means that your system is overloaded.
162+
163+
If you want to gather statistics about cache hits and misses, you can do so by implementing a <xref:PostSharp.Patterns.Caching.Implementation.CachingBackendEnhancer> that overrides the <xref:PostSharp.Patterns.Caching.Implementation.CachingBackend.GetItemCore*> and <xref:PostSharp.Patterns.Caching.Implementation.CachingBackend.GetItemAsyncCore*> methods (a `null` return value means a cache miss).
164+
155165
## Data schema
156166

157167
When dependencies are enabled, <xref:PostSharp.Patterns.Caching.Backends.Redis.RedisCachingBackend> relies on these keys:
158168

159-
1. Item version: `<prefix>:<schema-version>:val:<hash>:<item-version>:<item-key>` — string.
160-
2. Item value: `<prefix>:<schema-version>:ver:<hash>:<item-key>` — a list with values: `[<item-value>, <item-sliding-expiration>, <tag0>, <tag1> ... <tagn>]`.
161-
3. Backward dependencies: `<prefix>:<schema-version>:bdep:<hash>:<dependency-key>` — hash set of `<item-version>:<item-key>`.
162-
4. Forward dependencies: `<prefix>:<schema-version>:fdep:<hash>:<item-version>:<item-key>` — list of `<item-key>`.
169+
1. Item version: `<prefix>:<schema-version>:{<item-key>}:ver`unique string identifier.
170+
2. Item value: `<prefix>:<schema-version>:{<item-key>}:val:<item-version>` — a list with values: `[<item-value>, <item-sliding-expiration>, <tag0>, <tag1> ... <tagn>]`.
171+
3. Backward dependencies: `<prefix>:<schema-version>:{<dependency-key>}:bdep` — hash set of `<item-version>:<item-key>`.
172+
4. Forward dependencies: `<prefix>:<schema-version>:{<item-key>}:fdep:<item-version>` — list of `<dependency-key>`.
163173

164174
When dependencies are disabled, only the item value record is used.
165175

166176
In this description, elements between angle brackets are placeholders and mean the following:
167177

168-
- `<item-key>` is the cache key (as generated by <xref:PostSharp.Patterns.Caching.Implementation.CacheKeyBuilder>), where `{` and `}` are escaped.
178+
- `<item-key>` is the cache key (as generated by <xref:PostSharp.Patterns.Caching.Implementation.CacheKeyBuilder>), where `{`, `}` and `:` have been escaped.
169179
- `<item-value>` is the serialized cache value.
170180
- `<prefix>` is the value of the <xref:PostSharp.Patterns.Caching.Backends.Redis.RedisCachingBackendConfiguration.Prefix> property.
171181
- `<schema-version>` is the version of the data schema internal to <xref:PostSharp.Patterns.Caching.Backends.Redis.RedisCachingBackend>.
172-
- `<hash>` is a 16-bit hash of `<item-key>` surrounded with curly brackets, e.g. `{Aj4}`, unless the <xref:PostSharp.Patterns.Caching.Backends.Redis.RedisCachingBackendConfiguration.Prefix> property already contains a sharding hash (which is not recommended). This hash ensures that all keys corresponding to the same cache item reside in the same Redis node.
173182
- `<item-version>` is a randomly generated item version.
174-
- `<dependency-key>` is either a dependency key or a cache item key, when the cache item is itself a dependency (recursive dependencies), where `{` and `}` are escaped.
183+
- `<dependency-key>` is either a dependency key or a cache item key, when the cache item is itself a dependency (recursive dependencies), where `{`, `}` and `:` have been escaped.
175184

176185
## Clearing the cache
177186

178187
To remove all cache keys, you can:
179188

180189
* Use the `FLUSHDB` Redis command to delete all keys in the selected database, even those that don’t match the prefix.
181190
* Use the `SCAN <prefix>:*` command to identify all keys, then use `DEL` for each key.
182-
* Use the <xref:PostSharp.Patterns.Caching.Implementation.CachingBackend.ClearAsync*> method, which does a `SCAN <prefix>:<schema-version>:*` command, then `DEL` for each key.
191+
* Use the <xref:PostSharp.Patterns.Caching.Implementation.CachingBackend.ClearAsync*> method, which does a `SCAN <prefix>:<schema-version>:*` command, then `UNLINK` for each key.

0 commit comments

Comments
 (0)