简体   繁体   English

HttpContext.Current.Cache的替代方法

[英]Alternative to HttpContext.Current.Cache

What's an alternative to HttpContext.Current.Cache ? HttpContext.Current.Cache的替代方法是什么?

I'm dealing with only the application tier and not the web tier so what is best for this? 我只处理应用程序层,而不处理Web层,那么对此最好的是什么?

We obviously don't want to use this because unit tests in the app tier will not have an HttpContext. 我们显然不想使用它,因为应用程序层中的单元测试将没有HttpContext。

Would a simple list or dictionary object work? 一个简单的列表或字典对象会起作用吗?

A simple list or dictionary object could work, as long as you don't mind the cache items never expiring (unless you write code to remove them the list/dictionary)- probably not a good solution unless you are only caching a small quantity of data. 只要您不介意缓存项永不过期(除非您编写代码以将其删除列表/字典),简单的列表或字典对象就可以工作-除非您仅缓存少量的缓存或缓存对象,否则可能不是一个好的解决方案数据。 One solution to look into for non ASP.NET apps is the Enterprise Library Caching Application Block . 针对非ASP.NET应用程序的一种解决方案是企业库缓存应用程序块

I tend to work my hardest to not write any type of caching code. 我倾向于尽最大努力不编写任何类型的缓存代码。 I leverage caching capabilities of other libraries such as StructureMap to handle both my dependency injection and caching needs. 我利用诸如StructureMap之类的其他库的缓存功能来处理我的依赖项注入和缓存需求。 It provides a caching scope called Hybrid which will use ThreadLocal or HttpContext depending on where it's called from. 它提供了一个称为Hybrid的缓存范围,该范围将根据从何处调用来使用ThreadLocal或HttpContext。 It makes testing so much more effective. 它使测试更加有效。

You might want to consider creating a service pattern based cache interface for example ICacheService . 您可能要考虑创建基于服务模式的缓存接口,例如ICacheService You can then use Dependency Injection or some other factory pattern to wire up an implementation of what ever caching implementation you need. 然后,您可以使用Dependency Injection或其他工厂模式来连接所需的任何缓存实现的实现。

For example, i have an application that uses various Cache implementations based on how i am using the application. 例如,我有一个应用程序,它根据我使用应用程序的方式使用各种缓存实现。

1). 1)。 When in test mode, i use a " DummyCacheService " really does not cache at all since for my tests, i dont want data to cache. 在测试模式下,我使用“ DummyCacheService ”实际上根本不缓存,因为对于我的测试,我不希望数据缓存。

2). 2)。 Shared Hosting I use a AspCacheService which implements the standard HttpContext.Current.Cache 共享主机我使用一个AspCacheService来实现标准的HttpContext.Current.Cache

3). 3)。 I then moved it to a dedicated server farm and used a MemCacheService implementation. 然后,我将其移至专用服务器场并使用MemCacheService实现。

4). 4)。 And recently am testing on Windows Azure so we recently created a AzureCacheService ... 最近在Windows Azure上进行测试,因此我们最近创建了AzureCacheService ...

The beautiful thing about this strategy is we did not have to change a single line of code in our application code... we simple wrote a new service derived from ICacheService and fully tested outside of the application and once it was ready, we used Windsor / Castle DI container to inject into our production site. 这种策略的妙处在于,我们不必在应用程序代码中更改一行代码...我们简单地编写了一个从ICacheService派生的新服务,并在应用程序外部进行了全面测试,一旦准备就绪,便使用了Windsor / Castle DI容器注入我们的生产现场。

For reference, here is the interface that we use.... this could be modified to fit your need, but its worked 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);

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM