大师们

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

虽然效果很好,但是我们缺乏一些概括和全球化观点的能力。
因此,对于新的暂存器开发,我们打算在灵活性和可扩展性方面为此类一般实施提供最佳实践。

请提出建议。

最佳答案

您可以创建一个接口来定义在缓存和会话管理中使用的常用操作,其名称类似于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”实现或为用户基于会话的功能的“ Session”实现。

然后,您可以使用选择的依赖项容器将实现注入到service \ controllers中,并连接您的应用程序。

尝试并避免可能对单元测试造成问题的静态类。

10-07 19:25
查看更多