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 * * *")。

EnterpriseLibrary之Caching应用的更多相关文章

  1. nuget packages batch install

    d:\nuget\nuget.exe install EnterpriseLibrary.Common -NoCache -Verbosity detailed -OutputDirectory D: ...

  2. csc.rsp Nuget MVC/WebAPI、SignalR、Rx、Json、EntityFramework、OAuth、Spatial

    # This file contains command-line options that the C# # command line compiler (CSC) will process as ...

  3. Enterprise Library 6.0 参考源码索引

    http://www.projky.com/entlib/6.0/Diagnostics/Tracing/DiaLib.cs.htmlhttp://www.projky.com/entlib/6.0/ ...

  4. 缓存篇~第六回 Microsoft.Practices.EnterpriseLibrary.Caching实现基于方法签名的数据集缓存

    返回目录 这一讲中主要是说EnterpriseLibrary企业级架构里的caching组件,它主要实现了项目缓存功能,它支持四种持久化方式,内存,文件,数据库和自定义,对于持久化不是今天讨论的重要, ...

  5. 错误:创建 cachingConfiguration 的配置节处理程序时出错: 未能加载文件或程序集“Microsoft.Practices.EnterpriseLibrary.Caching,

    问题: 错误:创建 cachingConfiguration 的配置节处理程序时出错: 未能加载文件或程序集“Microsoft.Practices.EnterpriseLibrary.Caching ...

  6. Lind.DDD.Caching分布式数据集缓存介绍

    回到目录 戏说当年 大叔原创的分布式数据集缓存在之前的企业级框架里介绍过,大家可以关注<我心中的核心组件(可插拔的AOP)~第二回 缓存拦截器>,而今天主要对Lind.DDD.Cachin ...

  7. 采用EntLib5.0(Unity+Interception+Caching)实现项目中可用的Caching机制

    看了园子里很多介绍Caching的文章,多数都只介绍基本机制,对于Cache更新和依赖部分,更是只简单的实现ICacheItemRefreshAction接口,这在实际项目中是远远不够的.实际项目中, ...

  8. 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级) Caching Application Bl ...

  9. 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级) 本篇文章具体官方解释请参照以下链接: h ...

随机推荐

  1. Objective-C对象初始化 、 实例方法和参数 、 类方法 、 工厂方法 、 单例模式

    1 重构Point2类 1.1 问题 本案例使用初始化方法重构Point2类,类中有横坐标x.纵坐标y两个属性,并且有一个能显示位置show方法.在主程序中创建两个Point2类的对象,设置其横纵坐标 ...

  2. 即使连网了ping也会失败

    /*************************************************************************** * 即使连网了ping也会失败 * 说明: * ...

  3. Qt4升级到Qt5

    QtWidgets作为一个独立的模块 例如编译时错误 error: QMainWindow: No such file or directory error: QToolButton: No such ...

  4. 有趣的数-回文数(Palindrome number)

    文章转自http://blog.163.com/hljmdjlln@126/blog/static/5473620620120412525181/ 做LC上的题"Palindrome num ...

  5. bind 方法实现

    [要求]:实现 bind 方法 [实现]: // 简单方法 Function.prototype.bind = Function.prototpe.bind || function(context) ...

  6. C语言Makefile文件使用

    C语言中代码Makefile文件的写法 单文件,例: #定义变量 CFLAGS=gcc #具体命令都需要一个入口,all: 这个就相当于入口,默认情况,执行第一次入口, #后面执行其他入口进行依赖,如 ...

  7. UIImage加载图片的两种方法区别

    Apple官方的文档为生成一个UIImage对象提供了两种方法加载图片: 1. imageNamed,其参数为图片的名字: 2. imageWithContentsOfFile,其参数也是图片文件的路 ...

  8. 光流算法:Brox算法(转载)

    参考论文:1. High Accuracy Optical Flow Estimation Based on a Theory for Warping, Thomas Box, ECCV20042. ...

  9. LVS+keepalived配置

    一.系统环境准备: 1.keepalive主服务器 主机名称:dir 系统环境:CentOS release 6.5 (Final) 外网ip:192.168.1.203(网络模式桥接) vip:19 ...

  10. Linux驱动设计—— 中断与时钟@request_irq参数详解

    request_irq函数定义 /*include <linux/interrupt.h>*/ int request_irq(unsigned int irq, irq_handler_ ...