4

I am just trying to implement caching in my Web API. As I have already done the caching with the help of MemoryCache which uses System.Runtime.Caching.

Now I came to know that we can do caching with WebCache class too which uses System.Web.Helpers.

Is there any difference between them?

4
  • Not saying this is a duplicate question but check out these previously asked questions. stackoverflow.com/a/14811900/1260204 and stackoverflow.com/a/11547814/1260204. They both refer to pretty good articles on how you can implement custom output caching in web api. Commented Mar 23, 2016 at 14:20
  • @Igor Thanks for the information, Now we have so many options for caching in Web API. Like CacheCow and using System.Runtime.caching. But my question was different. Commented Mar 25, 2016 at 10:35
  • There differences and similarities between these. If it gets reopened I'll answer and explain. Commented Apr 25, 2018 at 8:26
  • @Luke Thanks in advance, I am also waiting for that. Commented Apr 25, 2018 at 9:13

2 Answers 2

1

I would always go with System.Runtime.Caching. Could be (didn't check) that System.Web.Helper.WebCache points to same global object internally. However, it is best to use whatever you want over interface, so make a simple caching intrerface and later you can always switch easily.

Sign up to request clarification or add additional context in comments.

1 Comment

Agreed. Stop coding to concretes. My answer elaborates on this tip. I'm upvoting because of the inclusion of "use an interface" on this answer.
1

While not the exact answer, I would abstract it, so you can pick which one you want to implement later.

I prefer ObjectCache/Memory cache because of more options. However....don't paint yourself into a corner.

public interface IServerSideMyInformationCache
{
    void SetMyObject(string key, MyObject myobj);

    MyObject GetMyObject(string key);

    void RemoveMyObject(string key);
}

public class ServerSideMyInformationMemoryCache : IServerSideMyInformationCache
{
    public const string CacheKeyPrefix = "ServerSideMyInformationMemoryCachePrefixKey";

    public void SetMyObject(string key, MyObject myobj)
    {
        /* not shown...custom configuration to house the setting */
        CachingSettingsConfigurationSection settings = CachingSettingsConfigurationRetriever.GetCachingSettings();

        ObjectCache cache = MemoryCache.Default;
        CacheItemPolicy policy = new CacheItemPolicy { SlidingExpiration = new TimeSpan(0, settings.MyObjectCacheMinutes, 0), Priority = CacheItemPriority.NotRemovable };
        cache.Set(this.GetFullCacheKey(key), myobj, policy);
    }

    public MyObject GetMyObject(string key)
    {
        MyObject returnItem = null;
        ObjectCache cache = MemoryCache.Default;
        object value = cache.Get(this.GetFullCacheKey(key));
        if (null != value)
        {
            returnItem = value as MyObject;
        }

        return returnItem;
    }

    public void RemoveMyObject(string key)
    {
        string cacheKey = this.GetFullCacheKey(key);
        ObjectCache cache = MemoryCache.Default;
        if (null != cache)
        {
            if (cache.Contains(cacheKey))
            {
                cache.Remove(cacheKey);
            }
        }
    }

    private string GetFullCacheKey(string key)
    {
        string returnValue = CacheKeyPrefix + key;
        return returnValue;
    }
}   


public class ServerSideMyInformationSystemWebCachingCache : IServerSideMyInformationCache
{
    public const string CacheKeyPrefix = "ServerSideMyInformationSystemWebCachingCachePrefixKey";

    public void SetMyObject(string key, MyObject myobj)
    {
        string cacheKey = this.GetFullCacheKey(key);

        if (null != myobj)
        {
            if (null == System.Web.HttpRuntime.Cache[cacheKey])
            {
                /* not shown...custom configuration to house the setting */
                CachingSettingsConfigurationSection settings = CachingSettingsConfigurationRetriever.GetCachingSettings();

                System.Web.HttpRuntime.Cache.Insert(
                    cacheKey, 
                    myobj,
                    null, 
                    System.Web.Caching.Cache.NoAbsoluteExpiration,
                    new TimeSpan(0, settings.MyObjectCacheMinutes, 0),
                    System.Web.Caching.CacheItemPriority.NotRemovable, 
                    null);
            }
            else
            {
                System.Web.HttpRuntime.Cache[cacheKey] = myobj;
            }
        }
    }

    public MyObject GetMyObject(string key)
    {
        MyObject returnItem = null;
        string cacheKey = this.GetFullCacheKey(key);
        if (null != System.Web.HttpRuntime.Cache[cacheKey])
        {
            returnItem = System.Web.HttpRuntime.Cache[cacheKey] as MyObject;
        }

        return returnItem;
    }

    public void RemoveMyObject(string key)
    {
        string cacheKey = this.GetFullCacheKey(key);
        if (null != System.Web.HttpRuntime.Cache[cacheKey])
        {
            System.Web.HttpRuntime.Cache.Remove(cacheKey);
        }
    }

    private string GetFullCacheKey(string key)
    {
        string returnValue = CacheKeyPrefix + key;
        return returnValue;
    }
}   

/* the crappiest of factories, but shows the point */
public static class ServerSideMyInformationCacheFactory
{
    public static IServerSideMyInformationCache GetAIServerSideMyInformationCache()
    {
        return new ServerSideMyInformationMemoryCache();
        ////return new ServerSideMyInformationSystemWebCachingCache();
    }
}   

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.