EnterpriseLibrary之Caching应用
http://blog.csdn.net/linux7985/article/details/6239433
******
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应用的更多相关文章
- nuget packages batch install
d:\nuget\nuget.exe install EnterpriseLibrary.Common -NoCache -Verbosity detailed -OutputDirectory D: ...
- 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 ...
- Enterprise Library 6.0 参考源码索引
http://www.projky.com/entlib/6.0/Diagnostics/Tracing/DiaLib.cs.htmlhttp://www.projky.com/entlib/6.0/ ...
- 缓存篇~第六回 Microsoft.Practices.EnterpriseLibrary.Caching实现基于方法签名的数据集缓存
返回目录 这一讲中主要是说EnterpriseLibrary企业级架构里的caching组件,它主要实现了项目缓存功能,它支持四种持久化方式,内存,文件,数据库和自定义,对于持久化不是今天讨论的重要, ...
- 错误:创建 cachingConfiguration 的配置节处理程序时出错: 未能加载文件或程序集“Microsoft.Practices.EnterpriseLibrary.Caching,
问题: 错误:创建 cachingConfiguration 的配置节处理程序时出错: 未能加载文件或程序集“Microsoft.Practices.EnterpriseLibrary.Caching ...
- Lind.DDD.Caching分布式数据集缓存介绍
回到目录 戏说当年 大叔原创的分布式数据集缓存在之前的企业级框架里介绍过,大家可以关注<我心中的核心组件(可插拔的AOP)~第二回 缓存拦截器>,而今天主要对Lind.DDD.Cachin ...
- 采用EntLib5.0(Unity+Interception+Caching)实现项目中可用的Caching机制
看了园子里很多介绍Caching的文章,多数都只介绍基本机制,对于Cache更新和依赖部分,更是只简单的实现ICacheItemRefreshAction接口,这在实际项目中是远远不够的.实际项目中, ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级) Caching Application Bl ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级) 本篇文章具体官方解释请参照以下链接: h ...
随机推荐
- Python 读书系列
1. 原文<A byte of Python> 翻译版:<<简明Python教程>> 2. Python:核心编程
- Howto add permanent static routes in Ubuntu
Static routing is the term used to refer to the manual method used to set up routing. An administrat ...
- Core Java Volume I — 4.10. Class Design Hints
4.10. Class Design HintsWithout trying to be comprehensive or tedious, we want to end this chapter w ...
- PAT (Basic Level) Practise:1027. 打印沙漏
[题目链接] 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打印 ***** *** * *** ***** 所谓“沙漏形状”,是指每行输出奇数个符号:各行符号中 ...
- Linux启动Apache支持.htaccess伪静态文件方法
第一.编辑httpd.conf文件 A - 在etc/httpd/conf/目录下的httpd.conf 文件,找到: LoadModule rewrite_module modules/mod_re ...
- Matlab神经网络工具箱学习之一
1.神经网络设计的流程 2.神经网络设计四个层次 3.神经网络模型 4.神经网络结构 5.创建神经网络对象 6.配置神经网络的输入输出 7.理解神经网络工具箱的数据结构 8.神经网络训练 1.神经网络 ...
- 如何优雅的写C++代码(一)
// get the greatest power of two that is a divisor of n: return n&-n; // swap two integers a and ...
- TextView中的图文混排
ImageSpan imageSpanMenu1 = new ImageSpan(activity,menuResId1); SpannableString contentMenu1 = new Sp ...
- tyvj1022 - 进制转换 ——进制为负数
题目链接:https://www.tyvj.cn/Problem_Show.aspx?id=1022 #include <cstdio> #include <cstdlib> ...
- 如何读懂 Intel HEX 文件
什么是 Intel HEX 文件格式 转自:http://www.cnblogs.com/imapla/archive/2013/03/16/2926133.htmlIntel HEX 文件是遵循 ...