cache paramters
来源:互联网 发布:拆分盘系统源码 编辑:程序博客网 时间:2024/06/05 02:33
aching Expiration
Microsoft Caching Application Block provides great use of the caching expiration functionality. For that, we need to add the namespace below:
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
Adding the namespace will provide us the below classes to implement the expiration of caching using the Application Block.
AbsoluteTime
ExtendedFormat
ExtendedFormatTime
FileDependency
NeverExpired
SlidingTime
AbsoluteTime
Absolute time defines the lifetime of a cache item by specifying the absolute time for an item to expire from thecache item collection. Absolute expirations are specified in full-time format (hh:mm:ss). The object will expire from the cache at the specified time:
For example:
AbsoluteTime _AbsoulteTime = new AbsoluteTime(TimeSpan.FromHours(1));_objCacheManager.Add("Student", _objStud, CacheItemPriority.Normal, null, _AbsoulteTime);
ExtendedFormat
and ExtendFormatTime
are quite similar with AbsoluteTime
configuration. I am not going into details on these two. Let us start with FileDependency
.
FileDependency
File-based dependency invalidates a particular cache item when a file(s) on the disk changes. This is quite helpful when we want to update cache data if there are some changes in files or database.
FileDependency _objFileDependency=new FileDependency("MyFile.XML");_objCacheManager.Add("Student", _objStud, CacheItemPriority.Normal, null, _objFileDependency);
As per the given example, if there is any change in "MyFile.xml", then _objStud
will be removed from the cachecollection. Sometimes it is very useful when we want to update cache data on file changes.
NeverExpired
If we set the caching expiring policy as never expire, then data will be stored in the cache collection unless and until we use CacheManager.Remove()
to remove the cache item. When we use the CacheManager.Add()
method with only a key and value argument, the cache object is set to never expire mode.
Sliding
Sliding resets the time for the item in the cache to expire on each request. This is useful when an item in thecache is to be kept alive as long as requests for that item are coming in from various clients. This means the item expires after the specified time has elapsed from when the item was last accessed. The default time is 2 minutes.
SlidingTime _objSlidingTime = new SlidingTime(TimeSpan.FromMinutes(5));_objCacheManager.Add("Student", _objStud, CacheItemPriority.Normal, null, _objSlidingTime);
Use Multiple Expariation Policy
I have already discussed the CacheManager.Add()
method which has one more overloaded method that takes five arguments. One of the arguments is an ICacheItemExpiration
. If you look at the details, the type of argument is params
, so using that, we can set the multiple expiration policy for a cache collection. Below is a sample example:
//Creating Absolute Time ExpirationAbsoluteTime _AbsoulteTime = new AbsoluteTime(TimeSpan.FromHours(1));//Creating FileDependecy ObjectFileDependency _objFileDependency=new FileDependency("MyFile.XML");// Using ICacheItemExpiration To Set multiple Cache Expiration policy_objCacheManager.Add("Student", _objStud, CacheItemPriority.Normal, null, new ICacheItemExpiration[] { _AbsoulteTime, _objFileDependency });
So _objStud
will remove from the cache collection based on the two expiration policies.
Implementing ICacheItemRefreshAction
ICacheItemRefreshAction
is the part of the RefreshAction
parameter of the CacheManager.Add()
method. The refreshAction
parameter allows for passing in an object that will perform some actions to update the cached object when it has expired. This is quite helpful if we want to perform some operations during cacheexpiration.
public class CacheLogPolicy : ICacheItemRefreshAction{ // Implement Referesh Method of interface ICacheItemRefreshAction public void Refresh(string removedKey, object expiredValue, CacheItemRemovedReason removalReason) { // Log The Information Or We can do some other stuff also }}
Now set the parameter of the refresh action in the CacheManager.Add()
method as follows:
_objCacheManager.Add("Student", _objStud, CacheItemPriority.Normal, new CacheLogPolicy(),null);
- cache paramters
- cache
- cache
- Cache
- cache
- Cache
- Cache
- Cache
- Cache
- cache
- cache
- cache
- Cache
- cache
- cache
- cache
- Cache
- Cache
- ORACLE时区
- 敏捷实践——“钱掌柜”分流发布模式
- 培养了用户通过微信支付及使用的习惯
- SEU寒假训练题解二 H Codeforces 435D
- C++考试——指针
- cache paramters
- C++开发者都应该使用的10个C++11特性
- 除掉脸上的一个肉粒突起新方法
- 兼容不同的屏幕大小(推荐,最官方的适应屏幕大小的经验)
- 基于消息与.Net Remoting的分布式处理架构
- AVAudioSessionPortOverrideSpeaker 和 AVAudioSessionCategoryOptionDefaultToSpeaker区别
- C#版万年历源码、农历、节日、节气齐全
- 2015年工作中遇到的问题:71-80,Tomcat-Redis-浮点数-HTTPS
- ios8UIWebView中嵌入百度地图黑屏解决方案