cache paramters

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 Expiration
AbsoluteTime _AbsoulteTime = new AbsoluteTime(TimeSpan.FromHours(1));
//Creating FileDependecy Object
FileDependency _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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值