MemoryCache.AddOrGetExisting的延迟加载不起作用 [英] Lazy loading for MemoryCache.AddOrGetExisting doesn't work

查看:50
本文介绍了MemoryCache.AddOrGetExisting的延迟加载不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含另一个MemoryCache的MemoryCache。顶级内存缓存具有键作为字符串,其值作为内存缓存。第二级内存缓存的键为字符串,其值很短。这是我的代码:

 var newCache = new Lazy< MemoryCache>(()=> 
{
MemoryCache缓存;

//将数据加载到缓存中的工厂代码

返回缓存;
});
var oldCache = MemoryCache.Default.AddOrGetExisting(strKey,newCache,new CacheItemPolicy())as Lazy< MemoryCache> ;;

尝试
{
oCache =(oldCache ?? newCache).Value;
}
catch
{
MemoryCache.Default.Remove(strKey);
throw;
}

var newId = new Lazy< short>(()=>
{
short id = 0;
//工厂代码将一个数据加载到缓存中

返回id;
});
var oldId = oCache.AddOrGetExisting(strCode,newId,new CacheItemPolicy())as Lazy< short> ;
尝试
{
data =(oldId ?? newId).Value;
}
catch
{
oCache.Remove(strCode);
throw;
}

第一个  MemoryCache.Default.AddOrGetExisting as Lazy< T>工作得很好。当指定的键具有相应的内存缓存值时。未调用工厂函数。


但是,第二个AddOrGetExisting为< Lazy< short>>不起作用! (oCache.AddOrGetExisting(strCode,newId,new CacheItemPolicy())as Lazy< short>)


从第一步获取oCache后,我使用oCache.Get(strCode)获取数据并返回数据。但是当调用 


oldId =  oCache.AddOrGetExisting(strCode,newId,new CacheItemPolicy())为Lazy< short> ;;


It始终返回null,并始终调用我的第二个工厂函数。为什么?


提前致谢!!!


解决方案

嗨kallan2,


- >它总是返回null,我的第二个工厂函数总是被调用。为什么?

 //返回:
//如果匹配的缓存条目已经存在,则为缓存条目;否则,null。

根据该文档,如果密钥存在于MemoryCache中,该方法将返回缓存值,否则返回null。请检查MemoryCache中是否存在密钥。


最好的问候,

李旺



I created a MemoryCache that contains another MemoryCache. The top level memory cache has key as string and its value as memory cache. The 2nd level memory cache has key as string and its value is short. Here is my code:

            var newCache = new Lazy<MemoryCache>(() =>
                {
                    MemoryCache cache; 

                    // factory code that loads data into the cache

                    return cache;
                });
            var oldCache = MemoryCache.Default.AddOrGetExisting(strKey, newCache, new CacheItemPolicy()) as Lazy<MemoryCache>;

            try
            {
                oCache = (oldCache ?? newCache).Value;
            }
            catch
            {
                MemoryCache.Default.Remove(strKey);
                throw;
            }

            var newId = new Lazy<short>(() =>
                {
                    short id = 0;
                    // factory code that loads one data into the cache

                    return id;
                });
            var oldId = oCache.AddOrGetExisting(strCode, newId, new CacheItemPolicy()) as Lazy<short>;
            try
            {
                data = (oldId ?? newId).Value;
            }
            catch
            {
                oCache.Remove(strCode);
                throw;
            }

The first MemoryCache.Default.AddOrGetExisting as Lazy<T> worked fine. When the specified key has the corresponding memory cache value. The factory function was not called.

However, the second AddOrGetExisting as <Lazy<short>> doesn't work! (oCache.AddOrGetExisting(strCode, newId, new CacheItemPolicy()) as Lazy<short>)

After getting oCache from the first step, I used oCache.Get(strCode) to get data and the data returned. But when calling 

oldId = oCache.AddOrGetExisting(strCode, newId, new CacheItemPolicy()) as Lazy<short>;

It always returns null and my second factory function always be called. Why?

Thanks in advance!!!

解决方案

Hi kallan2,

-> It always returns null and my second factory function always be called. Why?

// Returns:
//     If a matching cache entry already exists, a cache entry; otherwise, null.

According to the document, if the key exists in MemoryCache, the method will return a cache value, otherwise, it will return null. Please check whether the key exists in the MemoryCache.

Best Regards,
Li Wang


这篇关于MemoryCache.AddOrGetExisting的延迟加载不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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