2017-08-18 21 views
2

我正在使用Unity作为IOC。如何将System.Web.Cache传递到我的服务层

在我的服务层,我想在我的类中使用System.Web.Cache。

我应该只是将它作为参数传递,Unity会自动将它作为参数传递?

例如:

public class UserService 
{ 
    public UserService(System.Web.Caching.Cache cache) 
    { 
    _cache = cache; 
    } 
} 
+0

为什么不在问之前先测试代码? –

+0

@ManpritSinghSahota我做了,参数缓存为空。 –

+0

在WPF应用程序中使用System.Web可能是一种不好的做法。为此使用'System.Runtime.Cache'命名空间。我已经为此添加了答案。希望它对你有帮助。 :) –

回答

1

您应该使用System.Runtime.Caching。它专门用于处理Object的缓存。 NopCommerce对此有很好的实施。你应该创建像的CacheManager:

/// <summary> 
    /// Cache manager interface 
    /// </summary> 
    public interface ICacheManager 
    { 
     /// <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 cache. 
     /// </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 cached 
     /// </summary> 
     /// <param name="key">key</param> 
     /// <returns>Result</returns> 
     bool IsSet(string key); 

     /// <summary> 
     /// Removes the value with the specified key from the cache 
     /// </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 cache data 
     /// </summary> 
     void Clear(); 
    } 

然后你就可以实现与的MemoryCache的的ICacheManager接口或任何其它高速缓存管理器一样RedisCache如下:

/// <summary> 
    /// Represents a MemoryCacheCache 
    /// </summary> 
    public partial class MemoryCacheManager : ICacheManager 
    { 
     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 virtual T Get<T>(string key) 
     { 
      return (T)Cache[key]; 
     } 

     /// <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 virtual 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 virtual 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 virtual void Remove(string key) 
     { 
      Cache.Remove(key); 
     } 

     /// <summary> 
     /// Removes items by pattern 
     /// </summary> 
     /// <param name="pattern">pattern</param> 
     public virtual 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 virtual void Clear() 
     { 
      foreach (var item in Cache) 
       Remove(item.Key); 
     } 
    } 

回顾这NopCommerce link了解更多详情。在Unity中注册ICacheManager以为MemoryCacheManager创建对象并在您的应用程序中使用它。快乐编码:)

相关问题