System.Web.Caching.Cache c = HttpRuntime.Cache;
c.Insert("XXOO", "牙买袋"", null, "2010-5-1", TimeSpan.Zero);假如我要频繁修改XXOO这个缓存的值,比如我5分钟修改一次,请问应该如果修改?是可以直接进行修改还是直接添加一个覆盖还是删除以后再添加一个?谢谢
c.Insert("XXOO", "牙买袋"", null, "2010-5-1", TimeSpan.Zero);假如我要频繁修改XXOO这个缓存的值,比如我5分钟修改一次,请问应该如果修改?是可以直接进行修改还是直接添加一个覆盖还是删除以后再添加一个?谢谢
Cache("XXOO")="www";
Cache("XXOO")="111";
Cache("XXOO")="222";
Cache("XXOO")="333";
这样?如果是这样的话,那么他其他的参数,比如过期时间等还是跟初始化的时候【c.Insert("XXOO", "牙买袋"", null, "2010-5-1", TimeSpan.Zero);】这样一样吗?
{
// T = Cache("key") as T;
}
else
{
//access db , get T
//Cache.Insert(T)
}
key,
o,
null,
DateTime.Now.AddMinutes(5),
System.Web.Caching.Cache.NoSlidingExpiration);
using System;
using System.Web;
public static class CacheHelper
{
/// <summary>
/// Insert value into the cache using
/// appropriate name/value pairs
/// </summary>
/// <typeparam name="T">Type of cached item</typeparam>
/// <param name="o">Item to be cached</param>
/// <param name="key">Name of item</param>
public static void Add<T>(T o, string key)
{
// NOTE: Apply expiration parameters as you see fit.
// I typically pull from configuration file. // In this example, I want an absolute
// timeout so changes will always be reflected
// at that time. Hence, the NoSlidingExpiration.
HttpContext.Current.Cache.Insert(
key,
o,
null,
DateTime.Now.AddMinutes(1440),
System.Web.Caching.Cache.NoSlidingExpiration);
} /// <summary>
/// Remove item from cache
/// </summary>
/// <param name="key">Name of cached item</param>
public static void Clear(string key)
{
HttpContext.Current.Cache.Remove(key);
} /// <summary>
/// Check for item in cache
/// </summary>
/// <param name="key">Name of cached item</param>
/// <returns></returns>
public static bool Exists(string key)
{
return HttpContext.Current.Cache[key] != null;
} /// <summary>
/// Retrieve cached item
/// </summary>
/// <typeparam name="T">Type of cached item</typeparam>
/// <param name="key">Name of cached item</param>
/// <param name="value">Cached value. Default(T) if item doesn't exist.</param>
/// <returns>Cached item as type</returns>
public static bool Get<T>(string key, out T value)
{
try
{
if (!Exists(key))
{
value = default(T);
return false;
} value = (T) HttpContext.Current.Cache[key];
}
catch
{
value = default(T);
return false;
} return true;
}
}
DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration);
装到一个数组里或者 list里
之后再添加,修改
{ System.Web.Caching.Cache objCache = HttpRuntime.Cache;
return objCache[CacheKey];
}
public static void SetCache(string CacheKey, object objObject)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject);
}string CacheKey = "test";
object objModel = GetCache(CacheKey);//从缓存中获取
if (objModel == null)
{
}
System.Web.Caching.Cache c = HttpRuntime.Cache;
c.Insert("XXOO", "牙买袋"", null, "2010-5-1", TimeSpan.Zero);
设置了XXOO这个cache以后,或许下一分钟我让想让XXOO=111 或许过会我想让XXOO=222
我的意思就是如何修改这个值?
如果是根据过期来判断,那么只需要GET 是否为NULL。如果只是想修改这个值 那直接赋值就好了
请问是这样修改吗?
关于时间过期 ,有两个参数 你需要了解absoluteExpiration 和 slidingExpiration 具体可看msdn http://msdn.microsoft.com/zh-cn/library/4y13wyk9%28v=VS.80%29.aspx