//infoModel是实体类
List<infoModel> myList = null;
//读取各分类下面资讯
if (Cache["exhiCategroyInfo"] == null)
{
myList = new info().getInfoIndexTopInfo();
if (myList != null)
{
//缓存委托
CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onCategroyInfoRemove); //缓存时间(秒)
new dbCache().insertCache("exhiCategroyInfo", myList, 3000, callBack);
}
}
else
{
myList = (List<infoModel>)Cache["exhiCategroyInfo"];
}
private void onCategroyInfoRemove(string key, object value, CacheItemRemovedReason reason)
{
//create an instance of the callback delegate
CacheItemRemovedCallback callBack =
new CacheItemRemovedCallback(onCategroyInfoRemove);
new dbCache().insertCache(key, value, 3000, callBack);
}
//insertCache
public void insertCache(string cacheKey, object cacheValue, int cacheTime, CacheItemRemovedCallback callBack)
{
//判断缓存是否存在
if (!checkCacheKeyIsExist(cacheKey))
{
Cache _cache = HttpRuntime.Cache; _cache.Insert(cacheKey, cacheValue, null, System.DateTime.Now.AddSeconds((double)cacheTime), Cache.NoSlidingExpiration, CacheItemPriority.Default, callBack);
}
else
HttpContext.Current.Response.Write("抱歉,该名称的缓存已存在!");
} //我调试了,确认数据已经被缓存;当我重新刷新页面,缓存竟然为空,为什么呢??
List<infoModel> myList = null;
//读取各分类下面资讯
if (Cache["exhiCategroyInfo"] == null)
{
myList = new info().getInfoIndexTopInfo();
if (myList != null)
{
//缓存委托
CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onCategroyInfoRemove); //缓存时间(秒)
new dbCache().insertCache("exhiCategroyInfo", myList, 3000, callBack);
}
}
else
{
myList = (List<infoModel>)Cache["exhiCategroyInfo"];
}
private void onCategroyInfoRemove(string key, object value, CacheItemRemovedReason reason)
{
//create an instance of the callback delegate
CacheItemRemovedCallback callBack =
new CacheItemRemovedCallback(onCategroyInfoRemove);
new dbCache().insertCache(key, value, 3000, callBack);
}
//insertCache
public void insertCache(string cacheKey, object cacheValue, int cacheTime, CacheItemRemovedCallback callBack)
{
//判断缓存是否存在
if (!checkCacheKeyIsExist(cacheKey))
{
Cache _cache = HttpRuntime.Cache; _cache.Insert(cacheKey, cacheValue, null, System.DateTime.Now.AddSeconds((double)cacheTime), Cache.NoSlidingExpiration, CacheItemPriority.Default, callBack);
}
else
HttpContext.Current.Response.Write("抱歉,该名称的缓存已存在!");
} //我调试了,确认数据已经被缓存;当我重新刷新页面,缓存竟然为空,为什么呢??
public virtual void AddObjectWith(string objId, object o)
{
CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);
webCache.Insert(objId, o, null, System.DateTime.Now.AddSeconds(TimeOut), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, callBack);
}
public void onRemove(string key, object val, CacheItemRemovedReason reason)
{}
==============================
改成System.Web.Caching.CacheItemPriority.High,也不行啊!!
http://blog.csdn.net/merced1976/archive/2004/05/26/20752.aspx
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace WebApplication1
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<A> o = null;//new List<A>(); if (HttpRuntime.Cache["MyList"] == null)
{
o = new List<A>();
o.Add(new A() { obj = new object() });
HttpRuntime.Cache.Insert("MyList", o, null, DateTime.Now.AddSeconds(100), System.Web.Caching.Cache.NoSlidingExpiration,);
}
else
{
o = (List<A>)HttpRuntime.Cache["MyList"] ;
}
}
} public class A
{
public object obj { get; set; }
}
}2.缓存并不是说你设定了绝对过期时间,在这个过期时间之前就绝对不会回收.在系统内存紧张缓存会被回收失效的.3._cache.Insert(cacheKey, cacheValue, null, System.DateTime.Now.AddSeconds((double)cacheTime), Cache.NoSlidingExpiration, CacheItemPriority.Default, callBack);CacheItemPriority:指定 Cache 对象中存储的项的相对优先级。
Default:缓存项优先级的默认值为 Normal。
Normal:在服务器释放系统内存时,具有该优先级级别的缓存项很有可能被从缓存删除,其被删除的可能性仅次于具有 Low 或 BelowNormal 优先级的那些项。这是默认选项。
关于缓存中储存项的相对优先级见MSDN:http://msdn.microsoft.com/zh-cn/library/system.web.caching.cacheitempriority(VS.80).aspx
===============================================================
我本地调试了,插入缓存成功,然后我立即取出缓存,就取出不了,里面的值为空