微软企业库5.0的下载地址:http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=15104
微软企业库5.0文档地址:http://entlib.codeplex.com/releases/view/43135
缓存的参考链接:http://msdn.microsoft.com/en-us/library/ff664753%28v=PandP.50%29.aspx
1、安装微软企业库5.0
2、运行EntLibConfig.exe
3、增加缓存配置
4、查看缓存配置
5、另存为App.config
6、新建一个winform程序
7、添加Microsoft.Practices.EnterpriseLibrary.Caching 引用,dll的位置是:C:\Program Files\Microsoft Enterprise Library 5.0\Bin
8、把前面生成的App.config拷贝覆盖项目中的App.config
9、demo代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Caching;
namespace CachingDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 设置缓存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSetCache_Click(object sender, EventArgs e)
{
CacheManager cachemanager = (CacheManager)CacheFactory.GetCacheManager();
cachemanager.Add("username", "liuyu");
}
/// <summary>
/// 获取缓存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnGetCaching_Click(object sender, EventArgs e)
{
CacheManager cachemanager = (CacheManager)CacheFactory.GetCacheManager();
this.label1.Text = cachemanager.GetData("username") == null ? String.Empty : cachemanager.GetData("username").ToString();
}
/// <summary>
/// 清除缓存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRemoveCaching_Click(object sender, EventArgs e)
{
CacheManager cachemanager = (CacheManager)CacheFactory.GetCacheManager();
cachemanager.Remove("username");
}
}
}