框架开发手册(4)

2019-01-19 18:38

SOA Framework 开发使用手册

2.6.1.3 配置文件写法

配置文件需注意以下几点:

? ? ? ?

scanvageInterval defaultQueueLength typeName queueLength

CacheManager的扫描间隔时间。

所有容器的默认长度,如果单个容器没有定义,则使用。 缓存容器类型全名称。 单个容器的长度

2.6.1.4 Web页面调用代码

给定长缓存容器添加CacheItem时,可以添加缓存依赖,那么在访问缓存项时,会判断依赖项的HasChanged属性,如果该属性为true,那么此缓存项过期,无法获取。如果缓存依赖项为空的话,那么此缓存项只有在添加缓存项时检测到缓存项的位置超出缓存容器设置长度时才会被清理,请注意。

?《SOA Framework》 16

SOA Framework 开发使用手册

Web客户端1 protected void btsubmit_Click(object sender, EventArgs e) { string index=txtindex.Text; string name=txtname.Text; if(string.IsNullOrEmpty(index)) { return; } string file1 = Server.MapPath(\); string file2 = Server.MapPath(\); //创建一个缓存项,以TestCacheItem1的Index属性作为此缓存项的Key TestCacheItem1 item=new TestCacheItem1(Convert.ToInt32(index),name); //创建一个缓存依赖项,此处为文件依赖类型,可以依赖多个文件 //将文件物理路径作为初始化文件依赖的参数。 FileCacheDependency files = new FileCacheDependency(file1,file2); //在TestCacheQueue1中加入一个缓存项,该缓存项,依赖于files //当文件发生更改时,缓存过期。如果依赖项为空, //那么此缓存项只有在添加缓存项时检测到此缓存项的位置超出缓存容器设置长度时才会被清理 TestCacheQueue1.Instance.Add(item.Index, item, files); } protected void btsearch_Click(object sender, EventArgs e) { string index = txtindex.Text; if (string.IsNullOrEmpty(index)) { return; } TestCacheItem1 item = null; //通过Key来获取缓存项。如果缓存中无此项,则返回false //如果依赖项为过期也返回false if (TestCacheQueue1.Instance.TryGetValue(Convert.ToInt32(index),out item)) { this.txtname.Text = \+item.Name; } else { this.txtname.Text = \缓存1依赖改变,缓存清空\; } } ?《SOA Framework》 17

SOA Framework 开发使用手册

Web客户端2 protected void btsubmit2_Click(object sender, EventArgs e) { string name = txtcachename.Text; string age = txtage.Text; if (string.IsNullOrEmpty(age)) { return; } //创建一个缓存项,以TestCacheItem2的name属性作为此缓存项的Key TestCacheItem2 item = new TestCacheItem2(name,Convert.ToInt32(age) ); //创建一个绝对时间依赖项,在此时间之后,缓存过期 AbsoluteTimeDependency time = new AbsoluteTimeDependency(Convert.ToDateTime(\)); //在TestCacheQueue2中加入一个缓存项,该缓存项,依赖于time //当时间到达时,缓存过期 TestCacheQueue2.Instance.Add(item.Name, item,time); } protected void btsearch2_Click(object sender, EventArgs e) { string name = txtcachename.Text; TestCacheItem2 item = null; //通过Key来获取缓存项。如果缓存中无此项,则返回false //如果依赖项为过期也返回false TestCacheQueue2.Instance.TryGetValue(name, out item); if (!Equals(item, null)) { this.txtname.Text = \+item.Age.ToString(); } else { this.txtage.Text = \缓存2依赖改变,缓存清空\; } } ?《SOA Framework》 18

SOA Framework 开发使用手册

2.6.2 无长度限制的缓存(PortableCacheQueue)

无长度限制的缓存容器,内部使用Dictionary容器,来储存CacheItem,与CacheQueue类相比,此容器不可设置大小,但同样可以设置缓存过期依赖,并且由CacheManager统一扫描,删除缓存过期项。这种容器的生命周期与应用程序相同,只有在应用程序被卸载的时候,才释放内存。

2.6.2.1 自定义缓存项

与CacheQueue一样,PortableCacheQueue可以存储任意自定义类型。

//自定义缓存项 public class TestCacheItem2 { private string name; private int age; public string Name { get { return name; } set { name = value; } } public int Age { get { return age; } set { age = value; } } public TestCacheItem2( string name,int age) { this.name = name; this.age = age; } } 2.6.2.2 自定义缓存容器

定义一个装载项为上述类的缓存容器,在定义时需注意以下几点: ?

?

必须继承自MCS.Library.Caching.ProtableCacheQueue. 必须向CacheManager注册,否则无法实现定期清除过期缓存项。

由于CacheManager 中使用类型作为Dictionary的Key,所以必须使用单件,即保证类实例的单一性。否则在添加缓存项时,会覆盖或无法添加。

?

?《SOA Framework》 19

SOA Framework 开发使用手册

//定义一个缓存容器,此处必须为泛型封闭类,且必须继承PortableCacheQueue类,装载项为TestCacheItem2 public class TestProtableCacheQueue:PortableCacheQueue { //向框架中的注册一个Cache容器,必须使用CacheManager.GetInstance来注册,否则无法实现定期清除过期缓存项 //使用static readonly 关键字,将Instance定义为只读静态变量,初始化后不在改变。初始化交给CLR来处理,保证类实例的单一性 public static readonly TestProtableCacheQueue Instance = CacheManager.GetInstance(); private TestProtableCacheQueue() { } } 2.6.2.3 Web页面调用代码

给无长度限制缓存容器添加CacheItem时,可以添加缓存依赖,那么在访问缓存项时,会判断依赖项的HasChanged属性,如果该属性为true,那么此缓存项过期,无法获取。如果不添加缓存依赖项,那么此缓存项只有在应用程序卸载时才会被清理,请注意。

protected void btsubmit3_Click(object sender, EventArgs e) { string name = txtcachename.Text; string age = txtage.Text; if (string.IsNullOrEmpty(age)) { return; } //创建一个缓存项,以TestCacheItem2的Index属性作为此缓存项的Key TestCacheItem2 item = new TestCacheItem2(name, Convert.ToInt32(age)); //创建一个相对时间依赖项,如果两次访问之间超过设定的30秒,则缓存过期 SlidingTimeDependency time = new SlidingTimeDependency(TimeSpan.FromSeconds(30)); //在TestCacheQueue2中加入一个缓存项,该缓存项,依赖于time //当两次访问的时间间隔超过TimeSpan,缓存过期 TestProtableCacheQueue.Instance.Add(item.Name, item, time); } ?《SOA Framework》 20


框架开发手册(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2014吉林公务员考试申论文章分论点之传统文化 - 图文

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: