0

I am using system.Runtime.Caching.dll for caching some values. But when i implement region i am getting error. Exception is says : "The parameter regionName must be null." Do you have any idea about this problem.. Whay i am getting this. Or how can i add region inside .net caching method...

my sample source code is :

            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();
user1479273
  • 205
  • 1
  • 2
  • 9

1 Answers1

2

This question is a long time since asked. And I searched on internet about this issue. After that I figure out that: .Net framework 4.0 don't support multilanguage for memorycache. actually implementing this is very easy but why framework dont support multi language..! This is a mystery.. So, i found this class in msdn. you can use this. it will work..

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;
        }
    }
}
Mahmut EFE
  • 5,137
  • 5
  • 46
  • 56
  • I know this is marked as the answer, but where do you see evidence the question is .NET 4.0? What do you mean by 4.0 not supporting "multilanguage"? Perhaps you mean that the `MemoryCache` class does not support regions? That's the problem solved by the code you've posted. – Andrew Barber Aug 27 '14 at 18:21