简体   繁体   English

在asp.net MVC应用程序中使用memorycache每次都会创建它

[英]Using memorycache in asp.net mvc application it gets created everytime

Environment:
      asp.net mvc
      iis

I have create a wrapper and concrete class around memorycache which in its constrcutor i set memoryCache 我创建一个围绕的MemoryCache的包装和具体的类,它在其constrcutor我设置的MemoryCache

  public class AbijMemoryCache : IAbijMemoryCache
{
    private readonly MemoryCache _cache;

    public AbijMemoryCache()
    {
        if (_cache == null)
        {

            _cache = MemoryCache.Default;

        }
    }
}

then in unity for getting only one instance of memory cache I have used ContainerControlledLifetimeManager 然后为了统一只获取一个内存缓存实例,我使用了ContainerControlledLifetimeManager

  container.RegisterType<IAbijMemoryCache, AbijMemoryCache>(new ContainerControlledLifetimeManager());

i set cache in my homeController using following code 我使用以下代码在homeController中设置缓存

  var policy = new CacheItemPolicy();
  _cache.Set(key, value, policy);

but after uploading it to iis cache get empty sometime. 但将其上传到iis缓存后,有时会清空。

checking 检查

        _cache.Get(key) **`it gets null`**

why its getting null? 为什么它为空? what should i do? 我该怎么办?

I find that MemoryCache is working by reference so if you do assign an object to memoryCache it will be by refremce 我发现MemoryCache通过引用工作,因此如果您确实将一个对象分配给memoryCache,则将通过刷新

  _cache.Set(key, value, policy); //value should not be an object.

you can use a serialize before caching the value so it can be done like 您可以在缓存值之前使用序列化,以便像

 var cloneValue = SerializationCloner.DeepFieldClone(value);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM