【问题标题】:The parameter regionName must be null参数 regionName 必须为空
【发布时间】:2014-08-25 19:18:39
【问题描述】:

我正在使用 system.Runtime.Caching.dll 来缓存一些值。但是当我实施区域时,我得到了错误。例外是:“参数 regionName 必须为空。”你对这个问题有什么想法吗?为什么我得到这个。或者我如何在 .net 缓存方法中添加区域...

我的示例源代码是:

            ObjectCache cache = MemoryCache.Default;

            policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(100000.0);

            cache.Add("xxtr", "turkish", policy, "EN");
            cache.Add("xxtr", "türkçe", policy, "TR");
            cache.Add("xxtr", "ru_turki", policy, "RU");
            cache.Add("xxru", "russia", policy, "EN");
            cache.Add("xxru", "rusça", policy, "TR");
            cache.Add("xxru", "ru_russi", policy, "RU");

            string df = cache.GetValues("TR", "xxtr").ToString();

【问题讨论】:

标签: c# caching


【解决方案1】:

这个问题已经问了很久了。我在互联网上搜索了这个问题。之后我发现:.Net framework 4.0 不支持内存缓存的多语言。实际上实现这很容易,但是为什么框架不支持多语言..!这是个谜。所以,我在msdn 找到了这个课程。你可以用这个。它会工作..

using System;
using System.Web;
using System.Runtime.Caching;

namespace CustomCacheSample
{
    public class CustomCache : MemoryCache
    {
        public CustomCache() : base("defaultCustomCache") { }

        public override void Set(CacheItem item, CacheItemPolicy policy)
        {
            Set(item.Key, item.Value, policy, item.RegionName);
        }

        public override void Set(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
        {
            Set(key, value, new CacheItemPolicy { AbsoluteExpiration = absoluteExpiration }, regionName);
        }

        public override void Set(string key, object value, CacheItemPolicy policy, string regionName = null)
        {
            base.Set(CreateKeyWithRegion(key, regionName), value, policy);
        }

        public override CacheItem GetCacheItem(string key, string regionName = null)
        {
            CacheItem temporary = base.GetCacheItem(CreateKeyWithRegion(key, regionName));
            return new CacheItem(key, temporary.Value, regionName);
        }

        public override object Get(string key, string regionName = null)
        {
            return base.Get(CreateKeyWithRegion(key, regionName));
        }

        public override DefaultCacheCapabilities DefaultCacheCapabilities
        {
            get
            {
                return (base.DefaultCacheCapabilities | System.Runtime.Caching.DefaultCacheCapabilities.CacheRegions);
            }
        }

        private string CreateKeyWithRegion(string key, string region)
        {
            return "region:" + (region == null ? "null_region" : region) + ";key=" + key;
        }
    }
}

【讨论】:

  • 我知道这被标记为答案,但是您从哪里看到问题是 .NET 4.0 的证据? 4.0 不支持“多语言”是什么意思?也许您的意思是 MemoryCache 类不支持区域?这就是您发布的代码解决的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-26
  • 2018-06-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-08
  • 2018-06-03
  • 1970-01-01
相关资源
最近更新 更多