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?

Luke
  • 22,826
  • 31
  • 110
  • 193
Sibeesh Venu
  • 18,755
  • 12
  • 103
  • 140
  • Not saying this is a duplicate question but check out these previously asked questions. http://stackoverflow.com/a/14811900/1260204 and http://stackoverflow.com/a/11547814/1260204. They both refer to pretty good articles on how you can implement custom output caching in web api. – Igor Mar 23 '16 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. – Sibeesh Venu Mar 25 '16 at 10:35
  • There differences and similarities between these. If it gets reopened I'll answer and explain. – Luke Apr 25 '18 at 08:26
  • @Luke Thanks in advance, I am also waiting for that. – Sibeesh Venu Apr 25 '18 at 09:13

2 Answers2

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.

vbilopav
  • 156
  • 9
  • 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. – granadaCoder Mar 23 '16 at 14:27
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();
    }
}   
granadaCoder
  • 26,328
  • 10
  • 113
  • 146