问题使用HttpRuntime.Cache [英] Problem using HttpRuntime.Cache

查看:445
本文介绍了问题使用HttpRuntime.Cache的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的.NET code将对象添加到缓存:

 公共静态无效的添加< T>(字符串键,T dataToCache)
        {
            尝试
            {
                ApplicationLog.Instance.WriteInfoFormat(插入与关键{0}到高速缓存中的项目......,键);                HttpRuntime.Cache.Insert(
                    键,
                    dataToCache,
                    空值,
                    DateTime.Now.AddDays(7),
                    System.Web.Caching.Cache.NoSlidingExpiration);
            }            赶上(异常前)
            {
                ApplicationLog.Instance.WriteException(除息);
            }
        }

和这里是我的code检索的缓存值:

 公共静态吨得到< T>(字符串键)
        {
            尝试
            {
                如果(存在(键))
                {
                    ApplicationLog.Instance.WriteInfoFormat(从高速缓存{0}检索与重点项目......,键);                    回报(T)HttpRuntime.Cache [关键]
                }
                其他
                {
                    ApplicationLog.Instance.WriteInfoFormat(关键属性{0}在缓存中不存在的。键);
                    返回默认(T);
                }
            }
            赶上(异常前)
            {
                ApplicationLog.Instance.WriteException(除息);
                返回默认(T);
            }
        }公共静态布尔存在(字符串键)
       {
           布尔retVal的= FALSE;
           尝试
           {
               retVal的= HttpRuntime.Cache [关键]!= NULL;
           }
           赶上(异常前)
           {
               ApplicationLog.Instance.WriteException(除息);
           }
           返回retVal的;
       }

但我发现,每2分钟左右后,缓存的对象值是越来越设置为null导致从数据库中重新获取该值。

我缺少的是在这里吗?


解决方案

当你说每两分钟插入设置为空值,这是否意味着只是你有兴趣在缓存或每一个项目的项目?

我问这个,因为仅缓存只要在应用程序运行存在。如果应用程序重新启动时,缓存消失。如果一切顺利离开每2分钟这可以解释的行为。在这种情况下,你有你的手一个不同的问题:为什么应用程序重新启动每2分钟<​​/ P>。

如果这只是一些项目的话那可能是内存的问题。缓存清除本身高达响应低内存。我相信有一种方法来对插入的值设置优先级。不过这应该只是一个问题,当你内存不足。

如果这仍然不能解决你的问题,没有发现为什么一个项目已经被删除的方法。据这里解释

Am using following .net code to add objects to cache:

public static void Add<T>(string key, T dataToCache)
        {
            try
            {
                ApplicationLog.Instance.WriteInfoFormat("Inserting item with key {0} into Cache...", key);

                HttpRuntime.Cache.Insert(
                    key,
                    dataToCache,
                    null,
                    DateTime.Now.AddDays(7),
                    System.Web.Caching.Cache.NoSlidingExpiration);
            }

            catch (Exception ex)
            {
                ApplicationLog.Instance.WriteException(ex);             
            }
        }

and here is my code to retrieve values from cache:

public static T Get<T>(string key) 
        {   
            try
            {                
                if (Exists(key))
                {
                    ApplicationLog.Instance.WriteInfoFormat("Retrieving item with key {0} from Cache...", key);

                    return (T)HttpRuntime.Cache[key];
                }
                else
                {
                    ApplicationLog.Instance.WriteInfoFormat("Item with key {0} does not exist in Cache.", key);
                    return default(T); 
                }
            }
            catch(Exception ex)
            {
                ApplicationLog.Instance.WriteException(ex);
                return default(T); 
            }
        }



public static bool Exists(string key)
       {
           bool retVal = false;
           try
           {
               retVal= HttpRuntime.Cache[key] != null;
           }
           catch (Exception ex)
           {
               ApplicationLog.Instance.WriteException(ex);
           }
           return retVal; 
       }

But i find that after every 2 minutes or so,the cached object value is getting set to null resulting in pulling that value from database again.

What am i missing here?

解决方案

When you say every two minutes the value inserted is set to null, does that mean just the item you're interested in or every single item in the cache?

I ask this because the cache only exists as long as the application is running. If the application is restarted, the cache goes away. This would explain the behavior if everything goes away every 2 minutes. In that case you have a different problem on your hands: why does the application restart every 2 minutes.

If it's only SOME items then it could be a memory issue. The cache cleans itself up in response to low memory. I believe there's a way to set priority on values inserted. But this should only be a problem when you're low on memory.

If this still doesn't solve your problem, there is a way to discover why an item is being removed. It is explained here.

这篇关于问题使用HttpRuntime.Cache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆