dotnet System.Web.Caching.Cache vs System.Runtime.

2020-06-01 01:03发布

I've got a class that needs to store data in a cache. Originally I used it in an asp.net application so I used System.Web.Caching.Cache.

Now I need to use it in a Windows Service. Now, as I understand it, I should not use the asp.net cache in a not asp.net application, so I was looking into MemoryCache.

The problem is that they are not compatible, so either I change to use MemoryCache in the asp.net application, or I will need to create an adapter so ensure that the two cache implementations have the same interface (maybe derive from ObjectCache and use the asp.net cache internally?)

What are the implications of using MemoryCache in an asp.net?

Nadav

2条回答
够拽才男人
2楼-- · 2020-06-01 01:21

I would go with your second option and refactor things a little bit. I would create an Interface and two Providers (which are your adapters):

public interface ICachingProvider
{
    void AddItem(string key, object value);
    object GetItem(string key);
}

public AspNetCacheProvider : ICachingProvider
{
    // Adapt System.web.Caching.Cache to match Interface
}

public MemoryCacheProvider : ICachingProvider
{
    // Adapt System.Runtime.Caching.MemoryCache to match Interface
}
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2020-06-01 01:31

This morning I came across a library that could help with this situation. It's a CacheAdapter that allows you to code against a common interface and swap out the caching mechanism (System.Web.Caching, System.Runtime.Cache, or AppFabric) via configuration.

https://bitbucket.org/glav/cacheadapter

查看更多
登录 后发表回答