-
Notifications
You must be signed in to change notification settings - Fork 9
Flatwhite.OutputCacheAttribute
This attribute implement MethodFilterAttribute, I'm sure you know what it does so below are all the settings you can make on the attribute:
-
Duration: the cache duration, in seconds.
-
StaleWhileRevalidate: This should be used with Duration to indicates that caches MAY serve the cached result in which it appears after it becomes stale, up to the indicated number of seconds. The first call comes to the service and gets a stale cache result will also make the cache system auto refresh once. So if the method is not called many times in a short period, it's better to turn on AutoRefresh to make the cache refresh as soon as it starts to be stale
-
AutoRefresh: If set to true, the cache will be auto refreshed every second(s). It's a trade-off to turn this on as you don't want too many Timers trying to refresh your cache data very small amount of seconds especially when you have the value of Duration too small and there is so many varieties of the cache (because of "VaryByParam").
-
VaryByParam: A semicolon-separated list of strings that correspond to to parameter values
-
VaryByCustom: The idea is making it similar to VaryByCustom in Web environment. You can add custom context value and make it a part of cache key by setting VaryByCustom and create a custom IContextProvider and set it to Global.cs class.
-
CacheStoreType: by default, the cache entry will be stored in memory, you can change the cache store by specify the Type of the cache store or change the default cache store in Global.cs
-
CacheStoreId: Eache cache store should have a unique integer id. Once they are registered to the Global.cs, we can specify the cache store to use by setting this value. Default -1 which means the custom store id is not used
-
RevalidationKey: A key to used to delete the cache when an method with relevant RevalidateAttribute is invoked we can re-validate a cache entry after a certain method is called and change the data. For example, Delete a record should revalidate the cache on GetAllRecord
Simply decorate on the method like below:
public interface IBlogService
{
// For method with too many cache variations because of VaryByParam settings
[OutputCache(Duration = 5, VaryByParam = "tag, from, authorId", StaleWhileRevalidate = 5)]
IEnumerable<object> Search(string tag, DateTime from, Guid authorId);
// For method with not many cache variations and data is likely to changed every 5 seconds
[OutputCache(Duration = 5, VaryByParam = "blogId", StaleWhileRevalidate = 5)]
object GetById(int blogId);
// You can turn on AutoRefresh to keep the cache active if there are limited variations of the cache
[OutputCache(Duration = 5, VaryByParam = "blogId", StaleWhileRevalidate = 5, AutoRefresh = true)]
IEnumerable<string> GetBlogCategory();
}