6. Caching for Faster Responses and Better Performance Flashcards
What type of resilience strategy is caching?
It’s a proactive resilience strategy.
What does caching improve and how?
The performance of both the local and the remote services by reducing the load — the local service receives a response almost immediately and the remote service doesn’t even get hit with a request.
What are the two concerns to be mindful of when caching and why?
You need to be mindful of how long something can remain in the cache and the size of the items that you’re putting into the cache.
You worry about the lifetime of the item in the cache because you don’t want to return invalid or stale data, and you worry about the size of the items because you have limited resources.
What kind of policies can caching be used with, generic or non-generic ones?
It can be used with either.
How does a basic caching workflow look like?
When a request is made, the cache is checked to see if the value is already stored. If it is, that value is retrieved from the cache and returned as a response. If the value is not in the cache, we make the HTTP request, store the response in the cache, and then return the response. That value is now available for the next time the cache is hit.
What can be said about the context
when it comes to caching and why?
The context
must be passed when executing the request. The context
execution key is used as a cache key and the cache key identifies the location in the cache to search for a value or to store a new value if that item is not present in the cache.
What can be said about the distributed cache?
Polly also supports the distributed cache. This allows us to share cached responses between multiple applications. Polly supports any implementation of the IDistributedCache
including the Redis and SQL Server distributed caches.
What are Polly Cache delegates?
A number of functions that we can use when using Polly Cache, which helps diagnose the problems with the operation of our cache and/or performing logging related to the use of the cache.
What are the names of the Polly Cache delegates?
OnCacheGet
OnCacheMiss
OnCachePut
OnCacheError
OnCacheGetError
OnCachePutError
How do you define a Cache
policy?
By using either the Cache
or the CacheAsync
method of the static Policy
class and passing it a type, a cache provider object, and a TimeSpan
object.
~~~
Policy.CacheAsync(
new MemoryCacheProvider(memoryCache),
TimeSpan.FromMinutes(5));
````
How can we define our own Cache
policy when we want to do something with our cache that the default Cache
policy doesn’t provide?
By using the IAsyncCacheProvider
and the ICacheItemSerializer
interfaces.
ps. Check Microsoft docs for more information.