c# - How to pass down the System.Web.Cache to my service layer -
i using unity ioc.
in service layer want use system.web.cache in class.
should pass parameter , unity automatically pass dependancy?
example:
public class userservice { public userservice(system.web.caching.cache cache) { _cache = cache; } }
you should use system.runtime.caching
. specially designed handle caching of object. nopcommerce has great implementation this. should created cachemanager like:
/// <summary> /// cache manager interface /// </summary> public interface icachemanager { /// <summary> /// gets or sets value associated specified key. /// </summary> /// <typeparam name="t">type</typeparam> /// <param name="key">the key of value get.</param> /// <returns>the value associated specified key.</returns> t get<t>(string key); /// <summary> /// adds specified key , object 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 value indicating whether value associated specified key cached /// </summary> /// <param name="key">key</param> /// <returns>result</returns> bool isset(string key); /// <summary> /// removes value specified key cache /// </summary> /// <param name="key">/key</param> void remove(string key); /// <summary> /// removes items pattern /// </summary> /// <param name="pattern">pattern</param> void removebypattern(string pattern); /// <summary> /// clear cache data /// </summary> void clear(); }
and can implement icachemanager interface memorycache or other cache manager rediscache below:
/// <summary> /// represents memorycachecache /// </summary> public partial class memorycachemanager : icachemanager { protected objectcache cache { { return memorycache.default; } } /// <summary> /// gets or sets value associated specified key. /// </summary> /// <typeparam name="t">type</typeparam> /// <param name="key">the key of value get.</param> /// <returns>the value associated specified key.</returns> public virtual t get<t>(string key) { return (t)cache[key]; } /// <summary> /// adds specified key , object 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 value indicating whether value associated specified key cached /// </summary> /// <param name="key">key</param> /// <returns>result</returns> public virtual bool isset(string key) { return (cache.contains(key)); } /// <summary> /// removes value specified key cache /// </summary> /// <param name="key">/key</param> public virtual void remove(string key) { cache.remove(key); } /// <summary> /// removes items 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 cache data /// </summary> public virtual void clear() { foreach (var item in cache) remove(item.key); } }
review nopcommerce link more details. register icachemanager in unity create object memorycachemanager , use in application. happy coding :)
Comments
Post a Comment