System.Web.Caching.Cache c = HttpRuntime.Cache;
c.Insert("XXOO", "牙买袋"", null, "2010-5-1", TimeSpan.Zero);假如我要频繁修改XXOO这个缓存的值,比如我5分钟修改一次,请问应该如果修改?是可以直接进行修改还是直接添加一个覆盖还是删除以后再添加一个?谢谢

解决方案 »

  1.   

    难道是
    Cache("XXOO")="www";
    Cache("XXOO")="111";
    Cache("XXOO")="222";
    Cache("XXOO")="333";
    这样?如果是这样的话,那么他其他的参数,比如过期时间等还是跟初始化的时候【c.Insert("XXOO", "牙买袋"", null, "2010-5-1", TimeSpan.Zero);】这样一样吗?
      

  2.   

    if(Cache("key"))
    {
       // T = Cache("key") as T;
    }
    else
    {
        //access db , get T
        //Cache.Insert(T) 
    }
      

  3.   

    看方法重载,,,设置为5分钟即可HttpContext.Current.Cache.Insert(
                key,
                o,
                null,
                DateTime.Now.AddMinutes(5),
                System.Web.Caching.Cache.NoSlidingExpiration);
      

  4.   


    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;
        }
    }
      

  5.   

    HttpContext.Current.Cache.Insert(CacheName, key, null,
                DateTime.Now.AddMinutes(5),              System.Web.Caching.Cache.NoSlidingExpiration);
      

  6.   

     "牙买袋"", null, "2010-5-1" 这些你可以封装一下啊
    装到一个数组里或者 list里
    之后再添加,修改
      

  7.   

    public static object GetCache(string CacheKey)
    {    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)
    {
    }
      

  8.   

    我的意思是我要实时修改某个cache的值,也就是说
    System.Web.Caching.Cache c = HttpRuntime.Cache;
    c.Insert("XXOO", "牙买袋"", null, "2010-5-1", TimeSpan.Zero);
    设置了XXOO这个cache以后,或许下一分钟我让想让XXOO=111 或许过会我想让XXOO=222
    我的意思就是如何修改这个值?
      

  9.   


    如果是根据过期来判断,那么只需要GET 是否为NULL。如果只是想修改这个值  那直接赋值就好了
      

  10.   

    直接赋值Cache("XXOO")="111";是这样吗?那么他的其他元素,比如过期时间等是否仍然是初始化时候的设置呢?
      

  11.   

    Cache["XXOO"]=100
    请问是这样修改吗?
      

  12.   

    你可以使用 Cache.Insert Insert 与Add 不同之处 在于Insert 对于已经存在的项 会进行替换而 Add 则不会  则是失败
      

  13.   


    关于时间过期 ,有两个参数 你需要了解absoluteExpiration 和 slidingExpiration 具体可看msdn http://msdn.microsoft.com/zh-cn/library/4y13wyk9%28v=VS.80%29.aspx
      

  14.   

    DTCache.SetCache("WW", Convert.ToInt32(DTCache.GetCache("WW")) + 1, DTCache.GetCache("EndTime"), TimeSpan.Zero);Cache["WW"] = Convert.ToInt32(DTCache.GetCache("WW")) + 1;请问这2种修改的方法,哪种性能高?谢谢,因为操作太频繁了
      

  15.   

    DTCache.SetCache应该是封装好的 Cache.Insert  应该使用它 DTCache.SetCache