大师,
我们已经通过各种方式在过去的应用程序中实现了 CacheManager 和 SessionManager ,例如通过创建SessionHelper
静态类和CacheHelper
静态类
虽然它运作良好,但我们缺乏一般化和全球化视角的能力。 因此,对于新的临时开发,我们打算在灵活性和可扩展性方面实现这种一般实现的最佳实践。
请建议。
答案 0 :(得分:8)
您可以创建一个接口来定义缓存和会话管理中使用的常用操作,命名类似于IStateManager。 e.g。
/// <summary>
/// An interface to provide access to a state storage implementation
/// </summary>
public interface IStateManager
{
/// <summary>
/// Gets or sets the value associated with the specified key.
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="key">The key of the value to get.</param>
/// <returns>The value associated with the specified key.</returns>
T Get<T>(string key);
/// <summary>
/// Adds the specified key and object to the state manager.
/// </summary>
/// <param name="key">key</param>
/// <param name="data">Data</param>
void Set(string key, object data);
/// <summary>
/// Adds the specified key and object to the state manager.
/// </summary>
/// <param name="key">key</param>
/// <param name="data">Data</param>
/// <param name="cacheTime">Cache time</param>
void Set(string key, object data, int cacheTime);
/// <summary>
/// Gets a value indicating whether the value associated with the specified key is in the state manager.
/// </summary>
/// <param name="key">key</param>
/// <returns>Result</returns>
bool IsSet(string key);
/// <summary>
/// Removes the value with the specified key from the state manager.
/// </summary>
/// <param name="key">/key</param>
void Remove(string key);
/// <summary>
/// Removes items by pattern
/// </summary>
/// <param name="pattern">pattern</param>
void RemoveByPattern(string pattern);
/// <summary>
/// Clear all state manager data
/// </summary>
void Clear();
}
然后,您可以创建接口的实现以提供不同的功能。例如。内存实现,使用System.Runtime.Caching
/// <summary>
/// Represents an in memory cache
/// </summary>
public class MemoryCacheManager : IStateManager
{
public MemoryCacheManager()
{
}
protected ObjectCache Cache
{
get
{
return MemoryCache.Default;
}
}
/// <summary>
/// Gets or sets the value associated with the specified key.
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="key">The key of the value to get.</param>
/// <returns>The value associated with the specified key.</returns>
public T Get<T>(string key)
{
return (T)Cache[key];
}
/// <summary>
/// Adds the specified key and object to the cache with a default cache time of 30 minutes.
/// </summary>
/// <param name="key">key</param>
/// <param name="data">Data</param>
public void Set(string key, object data)
{
Set(key, data, 30);
}
/// <summary>
/// Adds the specified key and object to the cache.
/// </summary>
/// <param name="key">key</param>
/// <param name="data">Data</param>
/// <param name="cacheTime">Cache time</param>
public void Set(string key, object data, int cacheTime)
{
if (data == null)
return;
var policy = new CacheItemPolicy();
policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime);
Cache.Add(new CacheItem(key, data), policy);
}
/// <summary>
/// Gets a value indicating whether the value associated with the specified key is cached
/// </summary>
/// <param name="key">key</param>
/// <returns>Result</returns>
public bool IsSet(string key)
{
return (Cache.Contains(key));
}
/// <summary>
/// Removes the value with the specified key from the cache
/// </summary>
/// <param name="key">/key</param>
public void Remove(string key)
{
Cache.Remove(key);
}
/// <summary>
/// Removes items by pattern
/// </summary>
/// <param name="pattern">pattern</param>
public void RemoveByPattern(string pattern)
{
var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
var keysToRemove = new List<String>();
foreach (var item in Cache)
if (regex.IsMatch(item.Key))
keysToRemove.Add(item.Key);
foreach (string key in keysToRemove)
{
Remove(key);
}
}
/// <summary>
/// Clear all cache data
/// </summary>
public void Clear()
{
foreach (var item in Cache)
Remove(item.Key);
}
}
您可以创建此接口的多个实现,例如“Memcached”实现为您的应用程序提供分布式缓存,或者“Session”实现以提供基于用户会话的功能。
然后,您可以使用您选择的依赖项容器将实现注入您的服务\控制器并连接您的应用程序。
尝试并避免可能对单元测试造成问题的静态类。
答案 1 :(得分:0)
可以使用过滤器属性进行缓存,并可以通过单例类处理会话。
http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx
您可以在上面的链接中获取一些样本,以获得最佳方法或方法。