Enterprise Library 4.1 Caching Block 图文笔记

image

image

一,下载并安装好Enterprise Library 4.1

二,新建一个Web应用程序

 

三,右键点击Web.Config 文件 使用 Edit Enterprise Library Configuration 可以编辑Web.Config,建立一个缓存程序块

image

1.可以在Cache Managers 中建立多个缓存管理器,然后设置默认使用哪个管理器

 image

2.可以右键为管理器添加一种存储方式,如果不加就是默认的内存存储。

image

3.管理器的名字用来做Key,获取存储区对象

4.保存后,打开Web.config 会看到

<cachingConfiguration defaultCacheManager="Cache Manager">
  <cacheManagers>
    <add expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="1000"
      numberToRemoveWhenScavenging="10" backingStoreName="Null Storage"
      type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      name="Cache Manager" />
    <add expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="1000"
      numberToRemoveWhenScavenging="10" backingStoreName="Isolated Storage"
      type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      name="Cache Manager1" />
  </cacheManagers>
  <backingStores>
    <add partitionName="IsolatedCache" encryptionProviderName=""
      type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.IsolatedStorageBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      name="Isolated Storage" />
    <add encryptionProviderName="" type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      name="Null Storage" />
  </backingStores>
</cachingConfiguration>

四,添加引用

image

using Microsoft.Practices.EnterpriseLibrary.Caching;

using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;

五.写代码(例子我是复制菩提树下的杨过的代码)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;

namespace CachingBlock
{
  

    public class MyData
    {
        public string Name { set; get; }
        public int Age { set; get; }
        public string Color { set; get; }
    }

    public partial class WebForm1 : System.Web.UI.Page
    {
        const string KEYNAME = "myDateCache";//缓存的键值

        ICacheManager cacheManager;

        protected void Page_Load(object sender, EventArgs e)
        {
            cacheManager = CacheFactory.GetCacheManager();//实例化ICachemanager
        }

        protected void btnWrite_Click(object sender, EventArgs e)
        {
            //生成要缓存的数据(实际开发中可以是从数据库查询出来的数据)
            List<MyData> _list = new List<MyData>{ 
                new MyData(){ Age=1, Color="Yellow", Name="China"},
                new MyData{ Age=2,Color="Black",Name="USA"}
            };

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

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


            Response.Write("Cache写入完成," + DateTime.Now.ToString());
        }

        protected void btnRead_Click(object sender, EventArgs e)
        {

            this.R1.DataSource = GetCacheData();
            this.R1.DataBind();
            Response.Write("Cache加载完成," + DateTime.Now.ToString());

        }

        /// <summary>
        /// 获取缓存数据
        /// </summary>
        /// <returns></returns>
        public List<MyData> GetCacheData()
        {
            List<MyData> _cacheData = cacheManager.GetData(KEYNAME) as List<MyData>;

            if (null == _cacheData)//记得一定要加此判断(因为缓存可能过期)
            {
                //如果缓存数据为空,则重新生成数据,并加入缓存(为检测效果,特地把Color与Name前加了一个"New")
                _cacheData = new List<MyData>
                { 
                new MyData(){ Age=1, Color="New Yellow", Name="New China"},
                new MyData{ Age=2,Color="New Black",Name="New USA"}
                };

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

                cacheManager.Add(KEYNAME, _cacheData, CacheItemPriority.Normal, null, _ExpireTime);
            }

            return _cacheData;
        }

        protected void btnRemove_Click(object sender, EventArgs e)
        {
            cacheManager.Remove(KEYNAME);
            Response.Write("Cache清空完成," + DateTime.Now.ToString());
        }

    }


}

示例源码下载:EL41Sample.rar
Enterprise Library 4.1 目录:Enterprise Library 4.1 快速使用图文笔记 目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值