2

大师们,

我们在过去的应用程序中通过各种方式实现了CacheManagerSessionManager,例如通过创建SessionHelper静态类和CacheHelper静态类。

虽然它工作得很好,但我们缺乏一些概括和全球化视角的能力。因此,对于新的从头开发,我们打算在灵活性和可扩展性方面为这种通用实施提供最佳实践。

请建议。

4

2 回答 2

10

您可以创建一个接口来定义缓存和会话管理中使用的常见操作,命名为 IStateManager。例如

/// <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”实现或提供基于用户会话的功能的“会话”实现。

然后,您可以使用您选择的依赖容器将实现注入您的服务\控制器并连接您的应用程序。

尽量避免对单元测试有问题的静态类。

于 2013-01-03T11:26:39.220 回答
0

可以使用过滤器属性进行缓存,并且可以通过单例类处理会话。

http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx

您可以在上面的链接中获取一些示例,以了解最佳方式或方法。

于 2013-01-03T06:51:44.807 回答