博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Enterprise Library Step By Step系列(十):缓冲应用程序块——进阶篇
阅读量:7230 次
发布时间:2019-06-29

本文共 4979 字,大约阅读时间需要 16 分钟。

一.基于时间的过期策略

基于时间的过期策略,支持两种相对时间和绝对时间。
1 .绝对时间( Absolute ):
允许您定义一个缓冲项的生命周期,我们可以指定一个单一的时间作为过期,或者通过表达式来设置。
指定单一的时间作为过期:
 1
ExpandedBlockStart.gif
///读取数据

 2
None.gif
            Database db 
=
 DatabaseFactory.CreateDatabase(
"
Database Instance
"
);
 3
None.gif            DataSet ds 
=
 db.ExecuteDataSet(CommandType.Text,
"
Select * from Products
"
);
 4
None.gif            
 5
ExpandedBlockStart.gif            
///创建CacheManager

 6
None.gif
            IsolatedCacheManager 
=
 CacheFactory.GetCacheManager(
"
Isolated Cache Manager
"
);
 7
None.gif            
 8
ExpandedBlockStart.gif            
///创建一个单一的时间

 9
None.gif
            DateTime refreshTime 
=
 
new
 DateTime(
2005
11
12
12
51
30
);
10
None.gif
11
ExpandedBlockStart.gif            
///指定为绝对过期时间

12
None.gif
            AbsoluteTime expireTime 
=
 
new
 AbsoluteTime(refreshTime);
13
None.gif            
14
ExpandedBlockStart.gif            
///添加缓冲项,优先级为Normal

15
None.gif
            IsolatedCacheManager.Add(
"
MyDataSet
"
,ds, CacheItemPriority.Normal, 
null
,expireTime);
用表达式来设置:
表达式的格式: <Minute> <Hour> <Day of month> <Month> <Day of week>
例子:
“* * * * *”       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
“15 21 4 7 *”  expire 9:15 PM on 4 July
 1
ExpandedBlockStart.gif
///读取数据

 2
None.gif
            Database db 
=
 DatabaseFactory.CreateDatabase(
"
Database Instance
"
);
 3
None.gif            DataSet ds 
=
 db.ExecuteDataSet(CommandType.Text,
"
Select * from Products
"
);
 4
None.gif            
 5
ExpandedBlockStart.gif            
///创建CacheManager

 6
None.gif
            IsolatedCacheManager 
=
 CacheFactory.GetCacheManager(
"
Isolated Cache Manager
"
);
 7
None.gif            
 8
ExpandedBlockStart.gif            
///创建基于表达式

 9
None.gif
            ExtendedFormatTime expireTime 
=
 
new
 ExtendedFormatTime(
"
0 0 * * 6
"
);
10
None.gif            
11
ExpandedBlockStart.gif            
///添加缓冲项,优先级为Normal,过期时间

12
None.gif
            IsolatedCacheManager.Add(
"
Key1
"
"
Cache Item1
"
, CacheItemPriority.Normal,
null
, expireTime);
2 .变化的时间:
允许您定义针对条目的被调用的两次之间的间隔,定义条目的生命周期
 1
ExpandedBlockStart.gif
///读取数据

 2
None.gif
            Database db 
=
 DatabaseFactory.CreateDatabase(
"
Database Instance
"
);
 3
None.gif            DataSet ds 
=
 db.ExecuteDataSet(CommandType.Text,
"
Select * from Products
"
);
 4
None.gif            
 5
ExpandedBlockStart.gif            
///创建CacheManager

 6
None.gif
            IsolatedCacheManager 
=
 CacheFactory.GetCacheManager(
"
Isolated Cache Manager
"
);
 7
None.gif            
 8
ExpandedBlockStart.gif            
///访问5分钟后过期,变化的时间

 9
None.gif
            TimeSpan refreshTime 
=
 
new
 TimeSpan(
0
5
0
);
10
None.gif            SlidingTime expireTime 
=
 
new
 SlidingTime(refreshTime);
11
None.gif            
12
ExpandedBlockStart.gif            
///添加缓冲项

13
None.gif
            IsolatedCacheManager.Add(
"
Key1
"
"
Cache Item1
"
, CacheItemPriority.Normal,
null
, expireTime);
二.基于提醒机制的过期策略:
下面以文件依赖为例
1
ExpandedBlockStart.gif
///依赖于文件DependencyFile.txt
2ExpandedBlockEnd.gif            ///当文件改变时过期

3
None.gif
            FileDependency expireNotice 
=
 
new
 FileDependency(
"
DependencyFile.txt
"
);
4
None.gif            
5
ExpandedBlockStart.gif            
///添加缓冲项

6
None.gif
            myCacheManager.Add(
"
FileKey
"
"
String: Test Cache Item Dependency
"
, CacheItemPriority.Normal, 
null
, expireNotice);
可以创建自己的过期类,需要实现  ICacheItemExpiration 接口
三.条目移除的提示:
•        Caching Application Block 提供了项目移除的提醒,并在一下情况下被激活
–    条目过期了
–    条目被显式的移除了
–    条目被策略的清楚了
•        需要实现 ICacheItemRefreshAction接口
•        一个类实现了 ICacheItemRefreshAction 接口,同时如果需要后端存储时,还必须被标识为 Serializable (Especially for persistent backing store)
1
None.gif
    [Serializable]
2
None.gif    
public
 
class
 ProductCacheRefreshAction : ICacheItemRefreshAction
3
ExpandedBlockStart.gif    
{
4InBlock.gif        public void Refresh(string key, object expiredValue, CacheItemRemovedReason removalReason)
5ExpandedSubBlockStart.gif        {
6InBlock.gif            //……
7ExpandedSubBlockEnd.gif        }
8ExpandedBlockEnd.gif    }
四.装载缓冲:
1 .缓冲的前期装载( Proactive loading ):应用启动时装载
( 1 )优点
•        全部装载后,应用运行性能提升明显
( 2 )缺点
•        启动时间长
•        可能带来不必要的资源浪费 
•        为了提升启动性能而进行的——基于不同线程的装载,有造成了应用结构的复杂性
( 3 )何时使用主动装载 (Proactive caching)
在一些情况下中,他们自己有更新周期。当装载到缓冲将导致状态过期的出现
此时,您需要清楚的知道被缓冲的对象的生命周期
您还需要提前知道占用资源的程度
使用不稳定的资源时,尽量多使用主动装载缓冲
( 4 )主动装载实例:
 1
None.gif
CacheManager productsCache 
=
 CacheManager.GetCacheManager(); 
 2
ExpandedBlockStart.gif
/// 获取数据

 3
None.gif
ArrayList list 
=
 dataProvider.GetProductList(); 
 4
None.gif
 5
ExpandedBlockStart.gif
/// 添加缓冲项for (int i = 0; i < list.Count; i++) 
 6ExpandedBlockEnd.gif

 7
None.gif
    Product product 
=
 (Product) list[i]; 
 8
None.gif    productsCache.Add( product.ProductID, product ); 
 9
None.gif
10
None.gif
2 .缓冲的被动装载( Reactive loading ):按需装载
( 1 )优点
•        只有在需要的时候才装载,对资源的需求小
( 2 )缺点
•        但是在首次装载的时候,速度慢
( 3 )何时使用被动装载
需要缓冲的对象状态过多或系统资源不足的情况
资源的可靠性和性能良好,此时被动装载的又是更明显
希望利用缓冲机制,但是在应用程序的初始化时,希望不使用缓冲,而是根据用户输入等条件,进行缓冲
( 4 )被动装载实例:
 1
None.gif
CacheManager productsCache 
=
 CacheManager.GetCacheManager(); 
 2
None.gifProduct product 
=
 (Product) productsCache.GetData(productID); 
 3
None.gif
if
 (product 
==
 
null
 4
ExpandedBlockStart.gif
 5ExpandedSubBlockStart.gif    /// 
 6InBlock.gif    product = dataProvider.GetProductByID(productID);
 7InBlock.gif    if (product != null
 8ExpandedSubBlockStart.gif    
 9InBlock.gif       productsCache.Add(productID, product); 
10ExpandedSubBlockEnd.gif    } 
11ExpandedBlockEnd.gif}
 
12
None.gif
五.刷新缓冲( Explicit flushing ):
1 .精确刷新:
使用代码 ——Initiated by application code
全部移除 ——Removes all items from the cache
2 .自我移除( Scavenging ):
使用应用程序块的功能 ——Initiated by application block
基于优先级和最后访问的时间 ——Based upon priority and last access time
控制移除的精确度 ——Configuration settings control size of cache and number removed
自我清除的配置:
MaximumElementsLnCacheBeforeScavenging :缓冲中的最大元素数量。。
NumberToRemoveWhenScavenging :一次移除的数量。
为缓冲建立优先级
 1
ExpandedBlockStart.gif
/// <summary>
 2InBlock.gif        /// 缓冲的优先级有以下几个值:
 3InBlock.gif        /// --Low
 4InBlock.gif        /// --Normal
 5InBlock.gif        ///    --High
 6InBlock.gif        ///--NotRemovable
 7InBlock.gif        /// </summary>
 8InBlock.gif        /// <param name="sender"></param>
 9ExpandedBlockEnd.gif        /// <param name="e"></param>

10
None.gif
        
private
 
void
 button2_Click(
object
 sender, System.EventArgs e)
11
ExpandedBlockStart.gif        
{
12InBlock.gif            for(int intCount=0; intCount<8; intCount++)
13ExpandedSubBlockStart.gif            {
14InBlock.gif                string strKeyName = "Key"+System.Convert.ToString(intCount);
15InBlock.gif                
16InBlock.gif                if (intCount%2 == 0)
17ExpandedSubBlockStart.gif                {
18InBlock.gif                    myCacheManager.Add(strKeyName, "High", CacheItemPriority.High, nullnull);
19ExpandedSubBlockEnd.gif                }
20InBlock.gif                else
21ExpandedSubBlockStart.gif                {
22InBlock.gif                    myCacheManager.Add(strKeyName, "Low", CacheItemPriority.Low, nullnull);
23ExpandedSubBlockEnd.gif                }
24ExpandedSubBlockEnd.gif            }
25ExpandedBlockEnd.gif        }
本文转自lihuijun51CTO博客,原文链接:  http://blog.51cto.com/terrylee/67612
,如需转载请自行联系原作者
 
你可能感兴趣的文章
网络七层协议
查看>>
4种删除Word空白页的小技巧,都是你需要用到的!
查看>>
单服务器MySQL主从复制实践
查看>>
CentOS 7 root口令恢复
查看>>
| 刘知远:让计算机听懂人话
查看>>
苹果收购初创公司Tueo Health,哮喘监测或将应用到Apple Watch
查看>>
CLR存储过程
查看>>
初级运维(一)
查看>>
C语言字符串常用函数学习(一)
查看>>
Lync Server 2010部署与应用(三)---拓扑生成与发布
查看>>
安全摘记1:关于安全与黑客
查看>>
我的友情链接
查看>>
tbox中vector容器的使用
查看>>
一个简单的PHP笔试题
查看>>
firebug重新载入页面获取源码
查看>>
我的友情链接
查看>>
5月末周中国.COM总量净增1.2万个 美国净减2.6万个
查看>>
Elasticsearch数据建模-关联查询
查看>>
我的友情链接
查看>>
CentOS 下安装 Lnmp
查看>>