webconfig配置:
<configSections>
<section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<connectionStrings>
</connectionStrings>
<cachingConfiguration defaultCacheManager="CacheManager">
<cacheManagers>
<add expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="1000"
numberToRemoveWhenScavenging="10" backingStoreName="Null Storage"
name="CacheManager" />
</cacheManagers>
cs:
protected void Page_Load(object sender, EventArgs e)
{
cacheMgr = CacheFactory.GetCacheManager();
if (!Page.IsPostBack)
{
cacheBlodTest();
}
else
{
readCache();
}
}
public void cacheBlodTest()
{
Database DB = DatabaseFactory.CreateDatabase("localDB");
DbCommand cmd = DB.GetSqlStringCommand("select top 6 newsid,newsline,newsday,imgpath,news_color,url from dbo.VIEW_NEWS_TITLE_INDEX ORDER BY news_sort DESC, index_id DESC");
dt = DB.ExecuteDataSet(cmd).Tables[0];
//cacheMgr["key1"]
//约对过期策略
cacheMgr.Add("dtkey", dt, CacheItemPriority.Normal,
null, new ICacheItemExpiration[] { new AbsoluteTime(TimeSpan.FromSeconds(10)) });
//相对过期策略
// cacheMgr.Add("key2", "value2",
// CacheItemPriority.Normal, null,
// new SlidingTime(TimeSpan.FromMinutes(1)));
readCache();
}
public void readCache()
{
DataTable toBeDisplay = (DataTable)cacheMgr.GetData("dtkey");
//判断是否为空
if (this.ListBox1.Items.Count > 0)
{
this.ListBox1.Items.Clear();
}
if(toBeDisplay.Rows.Count >0)
{
foreach (DataRow dr in toBeDisplay.Rows)
{
this.ListBox1.Items.Add(dr[0].ToString());
}
}
}