MemoryCache.Add返回true,但不会增加项目缓存 [英] MemoryCache.Add returns true but does not add item to cache

查看:667
本文介绍了MemoryCache.Add返回true,但不会增加项目缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用添加如下Add方法项目到MemoryCache.Default实例:

 布尔结果= MemoryCache.Default.Add(cacheKey,dataToCache,cacheItemPolicy)
 

结果的值是true,表示该项目已被添加到缓存然而,当我试图找回它随即缓存为空。我也曾尝试使用设置方法的空缓存相同的结果要添加项目。

缓存已经默认99MB内存的限制,因此它不会出现,如果没有空间来增加新的项目。

任何想法?


 私有静态无效InsertCachedData(字符串cacheKey,反对dataToCache,字符串[] dependantCacheKeys)
    {
        CacheItemPolicy cacheItemPolicy =新CacheItemPolicy();

        cacheItemPolicy.AbsoluteExpiration =新的DateTimeOffset(DateTime.Now,新的时间跨度(小时:0分:0秒:3600));

        如果(dependantCacheKeys =空&安培;!&安培; dependantCacheKeys.Length大于0)
        {
            cacheItemPolicy.ChangeMonitors.Add(MemoryCache.Default.CreateCacheEntryChangeMonitor(dependantCacheKeys));
        }

        MemoryCache.Default.Add(cacheKey,dataToCache,cacheItemPolicy);

        logger.DebugFormat(高速缓存未命中与键{0} VehiclesProvider通话,cacheKey);
    }
 

解决方案

你没有设置 AbsoluteExpiration 属性正确。

这传递给时间跨度参数的DateTimeOffset 构造应在从UTC传递的DateTime 值的偏移,,你需要一些任意的时间跨度添加到您的生成抵销。你传递3600秒 - 也就是1小时 - 这是巧合,因为,presumably,你总部设在英国的工作纯粹是哪里BST目前UTC提前一小时

您正在传递 DateTime.Now 的DateTime 参数,所以你在有效做的是设置缓存的项目立即失效。

如果您希望您的缓存项一小时生活那么这样设置过期时间:

  cacheItemPolicy.AbsoluteExpiration =新的DateTimeOffset(DateTime.Now.AddHours(1));
 

I'm trying to add items to the MemoryCache.Default instance using the Add method as below:

bool result= MemoryCache.Default.Add(cacheKey, dataToCache, cacheItemPolicy)

The value of result is true, indicating that the item has been added to the cache yet when I attempt to retrieve it immediately afterwards the cache is empty. I have also tried to add the item using the Set method with the same result of an empty cache.

The cache has the default 99Mb memory limit so it doesn't appear as if there is no space to add new items.

Any ideas?


private static void InsertCachedData(string cacheKey, object dataToCache, string[] dependantCacheKeys)
    {
        CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();

        cacheItemPolicy.AbsoluteExpiration = new DateTimeOffset(DateTime.Now, new TimeSpan(hours: 0, minutes: 0, seconds: 3600));

        if (dependantCacheKeys != null && dependantCacheKeys.Length > 0)
        {
            cacheItemPolicy.ChangeMonitors.Add(MemoryCache.Default.CreateCacheEntryChangeMonitor(dependantCacheKeys));
        }

        MemoryCache.Default.Add(cacheKey, dataToCache, cacheItemPolicy);

        logger.DebugFormat("Cache miss for VehiclesProvider call with key {0}", cacheKey);
    }

解决方案

You're not setting the AbsoluteExpiration property correctly.

The TimeSpan argument that you pass to the DateTimeOffset constructor should be the offset from UTC of the passed DateTime value, not some arbitrary timespan that you want to add to generate your offset. You're passing in 3600 seconds -- ie, one hour -- which is working purely by coincidence because, presumably, you're based in the UK where BST is currently one hour ahead of UTC.

You're passing DateTime.Now as the DateTime argument, so what you're effectively doing is setting the cached item to expire immediately.

If you want your cached item to live for an hour then set the expiration like this:

cacheItemPolicy.AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddHours(1));

这篇关于MemoryCache.Add返回true,但不会增加项目缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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