Public RedisResponseCache(string redisConnectionMultiplexerConfiguration) internal class RedisResponseCache : IResponseCache It is important to set expiration for both hashes. With the conversion in place the entry in cache can be set (I will show only async version). HeadersHashEntries = new HashEntry(header.Key, (string)header.Value) HashEntry headersHashEntries = new HashEntry įoreach (KeyValuePair header in headerDictionary) Private HashEntry HeaderDictionaryToHashEntryArray(IHeaderDictionary headerDictionary) New HashEntry(nameof(cachedResponse.Body), bodyStream.ToArray()) New HashEntry(nameof(cachedResponse.StatusCode), cachedResponse.StatusCode), New HashEntry(nameof(cachedResponse.Created), ()), New HashEntry("Type", nameof(CachedResponse)), MemoryStream bodyStream = new MemoryStream() Private HashEntry CachedResponseToHashEntryArray(CachedResponse cachedResponse) First, some helper methods which will take care of conversion (I will be using StackExchange.Redis). What can be done is represent it by two separated hashes, which share a key pattern. I'm going to focus only on CachedResponse as CachedVaryByRules is simpler and can be done by replicating same approach.Īn instance of CachedResponse can't be represented by single hash because it contains headers collection. They can be stored in Redis by using Hashes. That might not be a good idea, so it might be better to focus on its implementations: CachedResponse and CachedVaryByRules. The IResponseCacheEntry doesn't make any assumptions about the shape of an entry (it's an empty interface) so the only thing that can be done with an instance of it is to blindly attempt binary serialization. It needs to be able to set or get IResponseCacheEntry by a string key. The component, which needs to be implemented in order to switch caching from in-memory to Redis, is IResponseCache as it represent the storage for entries. It orchestrates the entire process and makes other components like IResponseCachingPolicyProvider, IResponseCachingKeyProvider and IResponseCache talk to each other. The heart of server side response caching in ASP.NET Core is ResponseCachingMiddleware. This way the nodes are no longer responsible for holding the cache, the memory usage is lower and when an instance recycles it doesn't have to warm up again. One of approaches can be using a distributed cache like Redis. None of those limitations may be a problem for you, but if it is, you might want to trade some cache latency to solve them. Last but not least, it provides no support for load balancing scenarios - every node has to keep its own full cache. If the process goes down for any reason the cache needs to be repopulated. This also means that cache is not durable. In high load scenarios this can lead to memory pressure and memory pressure can lead to entries being evicted prior to its expiration. Under the hood it utilizes in-memory caching which means that the cache has low latency at price of increased memory usage. It's easy to use and (when configured properly) can give you nice performance boost. ASP.NET Core comes with out-of-the-box support for server side response caching.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |