EnterpriseLibrary之Caching应用

http://blog.csdn.net/linux7985/article/details/6239433

http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece76310508037434380146791d31175c3933fc239045c3e3abefe7975465184b52f2656b23a1cacab672c60423db286cd9549c0be972b2ad92465231f834014d649f89f1625c121c60defac1ae6bfa76299a881c4df23098c0c113fddb0dd55575e8834b04777b8fbc55f142f52e7b36e72fe292369c86007e11ba8ab256f77daaa9c0b5bc25a813c4dc1ee22b14e05c464b36c683345d05bc07b465630f74e57e8304c7885ea2df05b785753c65fb2c9d6c0e95fffd9&p=c26ac54ad3c70afc57efd52b6147&newp=8662c40f85cc43ff57e890275a4292695803ed603dddd1&user=baidu&fm=sc&query=expirationPollFrequencyInSeconds%9E%E9%CA%B2%FCN%9B%5D%D0%A7&qid=fbe573ab000061cf&p1=7

******

CacheManager.Add(key, value)方法,将数据项放入缓存,这是Add方法一个比较简单的重载。该Add方法添加的数据项不会过期,并且设置CacheItemPriority属性Normal

******

此外,CacheManager.Add方法还有如下重载:

public void Add(

   string key,

   object value,

   CacheItemPriority scavengingPriority,

   ICacheItemRefreshAction refreshAction,

   params ICacheItemExpiration[] expirations

);

scavengingPriority 指定新数据项的scavenging优先级。

refreshAction:该对象允许更新缓存中过期数据项。

expirations:Param数组指定该数据项的有期策略,可以null或忽略。

(2)将数据项移出缓存

// Request that the item be removed from the cache.

this.primitivesCache.Remove(product.ProductID);

如果该item不在缓存中,该Remove方法么都不做。

(3)从缓存中检索数据

// Read the item from the cache. If the item is not found in the cache, the

// return value will be null.

Product product = (Product) this.primitivesCache.GetData(product.ProductID);
如果缓存中不存在该item,则返回null值。 

(4)清除所有缓存数据
this.primitivesCache.Flush();
清除缓存中所有数据。


============================================================================================

微软发布的EnterparseLibrary提供了许多功能,为我们的应用程序提供了许多方便,有缓存、配置、异常、数据访问、加密、日志等组件。项目中需要用到的Cache功能,便采用了EnterpriseLibrary的Cache组件。下面浅谈一下Cache的实用范围、用法及注意事项。

        应用系统为了提升效率,可以将一些配置信息等不常改变的数据进行缓存以减少对数据源的读取频率。通常的做法是在程序中使用静态变量来存储,再设置一个Timer,每隔一段时间对数据进行更新等操作。EnterpriseLibrary的Cache提供了非常强大的支持,可以设置绝对时间、间隔时间、自定义格式以及文件过期时间来进行相应的更新操作。

        1. 绝对时间过期的缓存:AbsoluteTime

     AbsoluteTime _ExpireTime = new AbsoluteTime(DateTime.Now.AddSeconds(30));//指定30秒后过期

            cacheManager.Add(KEYNAME, _list, CacheItemPriority.Normal, null, _ExpireTime);//加入缓存

        2. 相对时间过期的缓存:SlidingTime

        3. 自定义格式过期的缓存:ExtendedFormatTime

           自定义格式为:<Minute> <Hour> <Day of month> <Month> <Day of week>

           Minute            0-59
           Hour              0-23
           Day of month     1-31
           Month             1-12
           Day of week       0-6 (Sunday is 0)
           如:
           * * * * *    - expires every minute
           5 * * * *    - expire 5th minute of every hour
           * 21 * * *   - expire every minute of the 21st hour of every day
           31 15 * * *  - expire 3:31 PM every day
           7 4 * * 6    - expire Saturday 4:07 AM

        4. 文件的过期缓存:FileDependency

        简单的程序代码如下:

        CacheManager cacheManager = CacheFactory.GetCacheManager();

        ExtendedFormatTime expireTime = new ExtendedFormatTime("41 11 * * *");
        cacheManager.Add("key", value, CacheItemPriority.Normal, new ProductCacheRefreshAction(), expireTime);

        上述代码即将value放入到以key为键值的默认换成块中,且在每天的11点41分缓存中的值失效,需要重新读取数据源。

        Cache以配置文件的方式供用户进行缓存的轮询过期数据的频率、缓存中数据项的多少、清除数据项的多少以及缓存备份的位置。

 1.    expirationPollFrequencyInSeconds: 设置控制后台调度程序多久检查过期条目的定时器。此属性必须是正整数,且是必要的。
 2.    maximumElementsInCacheBeforeScavenging: 设置在清除开始前可以在缓存中的条目的最大数量。此属性必须是正整数,且是必要的。
 3.    numberToRemoveWhenScavenging: 设置在清除开始时移除的条目的数量,此属性必须是正整数,且是必要的。
 4.    backingStoreName: 缓存备份的位置

        值得一提的是,expirationPollFrequencyInSeconds属性是控制后台调度程序多久检查过期条目的配置,单位为秒,如果系统经常需要更新数据则可以将此值设置的小一点;ICacheItemExpiration的时间是以UTC的时间来作为标准时间来比较的,北京时间比UTC早8个小时,比如你需要在每天的十二点半让缓存过期,则必须这样设置ExtendedFormatTime("30 4 * * *")。

原文地址:https://www.cnblogs.com/guyuehuanhuan/p/4318221.html