【问题标题】:Using memorycache in asp.net mvc application it gets created everytime在 asp.net mvc 应用程序中使用 memorycache 每次都会创建它
【发布时间】:2014-08-12 07:31:19
【问题描述】:
Environment:
      asp.net mvc
      iis

我围绕 memorycache 创建了一个包装器和具体类,我在它的 constrcutor 中设置了 memoryCache

  public class AbijMemoryCache : IAbijMemoryCache
{
    private readonly MemoryCache _cache;

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

            _cache = MemoryCache.Default;

        }
    }
}

然后在 unity 中只获取一个我使用过的内存缓存实例 ContainerControlledLifetimeManager

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

我使用以下代码在我的 homeController 中设置缓存

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

但在上传到 iis 缓存后有时会变空。

检查

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

为什么它会为空? 我该怎么办?

【问题讨论】:

    标签: c# asp.net-mvc iis memorycache


    【解决方案1】:

    我发现 MemoryCache 是通过引用工作的,所以如果你将一个对象分配给 memoryCache 它会 参考一下

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

    你可以在缓存值之前使用序列化,这样就可以完成

     var cloneValue = SerializationCloner.DeepFieldClone(value);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-20
      • 2013-01-31
      • 1970-01-01
      • 2020-07-30
      • 2011-03-22
      • 1970-01-01
      • 2018-03-22
      • 1970-01-01
      相关资源
      最近更新 更多