0

I have problem with cache in my asp.net mvc3 application.

My code

using System.Web.Caching;
...
class RegularCacheProvider : ICacheProvider
{
    Cache cache ;

    public object Get(string name)
    {
        return cache[name];
    }

    public void Set(string name, object value)
    {
        cache.Insert(name, value);
    }

    public void Unset(string name)
    {
        cache.Remove(name);
    }
}

And I use singleton for give value for it :

School schoolSettings = (School)CacheProviderFactory.Cache.Get("SchoolSettings");
            if (schoolSettings == null)
            {
                CacheProviderFactory.Cache.Set("SchoolSettings", someObject);
            }

So in first use it does not work and give me an error cache[name] is null.

What I'm doing wrong?

Any help would be appreciated.

5
  • Did you check out output caching for MVC3? BTW Siema! Commented Aug 7, 2012 at 12:53
  • Witam :) I need use Cache this way. Commented Aug 7, 2012 at 12:54
  • Were you planning on assigning a value to cache at some point? 'cos you haven't. Commented Aug 7, 2012 at 12:58
  • Also, you claim cache is a singleton and that there is a CacheProviderFactory.Cache - neither of these things are shown in the code. You are asking us to guess what you have removed; that will not lead to a good answer... Commented Aug 7, 2012 at 13:00
  • Slightly off topic but you might want to look at using dependency injection to provide the cache instance. That way you can change it to another caching provider if needed and you can also unit test easier. Commented Aug 7, 2012 at 13:02

4 Answers 4

2

At no point have you given cache a value... and note that the regular web cache probably isn't your best bet if you want it separate; perhaps

MemoryCache cache = new MemoryCache(); 
Sign up to request clarification or add additional context in comments.

1 Comment

I have had the same problem - problem with null reference. But I have change it for HttpRuntime.Cache
1

What about using the HttpRuntime.Cache, this example would cache for an hour?

HttpRuntime.Cache.Add("SchoolSettings", someObject, null, DateTime.Now.AddHours(1),
                       System.Web.Caching.Cache.NoSlidingExpiration,
                       System.Web.Caching.CacheItemPriority.Normal, null);

3 Comments

I need to use System.Web.Caching
@MateuszRogulski why is that? this is a private implementation detail; frankly it shouldn't matter how it is implemented
Well, I have change it with your method. Thanks :)
1

Try the following code. it works fine for my project

 using System.Runtime.Caching;

    public class RegularCacheProvider : ICacheProvider
        {
            private ObjectCache Cache { get { return MemoryCache.Default; } }

            object ICacheProvider.Get(string key)
            {
                return Cache[key];
            }

            void ICacheProvider.Set(string key, object data, int cacheTime = 30)
            {
                var policy = new CacheItemPolicy {AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime)};
                Cache.Add(new CacheItem(key, data), policy);
            }

            void ICacheProvider.Unset(string key)
            {
                Cache.Remove(key);
            }
        }

Comments

0

Change the code where you check for the value as follow:

School schoolSettings = CacheProviderFactory.Cache.Get("SchoolSettings") as (School);

Notice that I am using "as" rather than casting the object. Cast will blow up if the value is null while "as" will just give you a null value which is what you expect.

1 Comment

Like Marc Gravel mentioned, Are you sure cache is not null by the time the Get method is called?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.