Alternative HttpContext.Current.Cache

Which alternative HttpContext.Current.Cache?

I deal only with the application tier, not the web tier, and which is better for this?

We obviously do not want to use this because unit tests at the application level will not have an HttpContext.

Will a simple list or dictionary work?

+3
source share
3 answers

A simple list or dictionary object may work if you don't mind that cache items never expire (unless you write code to remove their list / dictionary), probably not a good solution if you are not only caching a small amount of data. One solution for finding applications without ASP.NET is to use the enterprise library caching application block .

+3
source

. , StructureMap, , . Hybrid, ThreadLocal HttpContext , . .

+2

, , ICacheService. Dependency Injection - factory, - , .

, , , .

1). " DummyCacheService", , , .

2). AspCacheService, HttpContext.Current.Cache

3). MemCacheService.

4). Windows Azure, AzureCacheService...

The great thing about this strategy is that we did not need to change one line of code in our application code ... we just wrote a new service, obtained from ICacheService , and we tested it completely outside the application, and as soon as it was ready, we used the Windsor / container Castle DI to enter our production site.

For reference, here is the interface we use ... it can be changed according to your needs, but it works great for us

public interface ICacheService
    {
        // TO-DO consider a cache group concept where you have groups of cache items, similar to cache manager in EntLib

        void Flush();
        void Add(string key, object item, int expirationInSeconds);
        void Add(string key, object item, int expirationInSeconds, CacheItemPriority priority);
        void Add(string key, object item, DateTime absoluteExpiration);
        void Add(string key, object item, DateTime absoluteExpiration, CacheItemPriority priority);
        void Add(string key, object item, TimeSpan slidingExpiration);
        void Add(string key, object item, TimeSpan slidingExpiration, CacheItemPriority priority);
        object Get(string key);
        object Remove(string key);
        T Get<T>(string key);
        IList<string> GetCacheKeys();
        bool ContainsData(string key);

    }
+2
source

Source: https://habr.com/ru/post/1725205/


All Articles