Asp.net使用缓存 (二)
前篇-Asp.net使用缓存 (一)向大家简单介绍
这篇是分享把缓存程序变得更有弹性 第二篇大纲
情境: 目前有个项目使用? 在内存缓存中除了使用 Asp.Net 中 如果我们原本使用 public class HomeController : Controller { System.Web.Caching.Cache cacheContainer = HttpRuntime.Cache; public ActionResult Index() { string cacheData = cacheContainer.Get("data") as string; if (cacheData==null) { cacheContainer.Insert("test1",DateTime.Now.ToShortDateString()); } return View(cacheData); } } 虽然使用不同缓存方式,但记得我上篇的重点缓存会有两个动作,读和写,所以最基本就会有读和写这两个动作 OOP有个很重要的观念?多个类有重复动作考虑提出父类 为了方便了解我把 public class NetCache { System.Web.Caching.Cache cacheContainer = HttpRuntime.Cache; public object GetCacheObject(string key) { return cacheContainer.Get(key); } public void SetCache(string key,object obj) { cacheContainer.Insert(key,obj); } } 这边有另一个 public class MemeryCache { private ObjectCache _cache = MemoryCache.Default; public object GetCacheObject(string key) { return _cache[cacheKey]; } public void SetCache(string key,object obj) { var policy = new CacheItemPolicy(); policy.RemovedCallback = OnFileContentsCacheRemove; // 设定缓存时间2分钟 policy.AbsoluteExpiration = DateTimeOffset.Now.Minute(2); _cache.Set(cacheKey,fileContents,policy); } } 先不关注这两个对象里面细节,我们可以发现他们都有? 这时我们就可以适时提出界面(interface),当作这两个类的合约 public interface ICache { void Set(string key,object obj); object Get(string key); } 之后将他们两个类实现? public class MemeryCache : ICache { private ObjectCache _cache = MemoryCache.Default; public object Get(string key) { return _cache[cacheKey]; } public void Set(string key,policy); } } public class NetCache : ICache { System.Web.Caching.Cache cacheContainer = HttpRuntime.Cache; public object Get(string key) { return cacheContainer.Get(key); } public void Set(string key,obj); } } 提出界面有什么好处? 我们可以把前面程序改成IOC依赖注入的方式,不要在程序写死使用 Note:我使用建构子注入法 public class HomeController : Controller { //不用写死使用 HttpRuntime.Cache //System.Web.Caching.Cache cacheContainer = HttpRuntime.Cache; ICache cacheContainer; public HomeController(ICache Container){ cacheContainer = Container; } public ActionResult Index() { string cacheData = cacheContainer.Get("data") as string; if (cacheData==null) { cacheContainer.Insert("test1",DateTime.Now.ToShortDateString()); } return View(cacheData); } }
我在StackOverFlow解答的方式就是第二种 其中最主要的技巧就是把 public T GetOrSetCache 让我们在使用时可以变成 var data = DateTime.Now.ToShortDateString(); int numberOfMinutes = 3; data = GetOrSetCache("name1",data,numberOfMinutes ); 我们只需要调用
.Net有提供一个很方便的机制?扩充方法,这个机制帮我们解决一个很重要的问题. 在这段程序中,使用 public static TObj GetOrSetCache 我们使用时如下 变更简洁动作更漂亮 int numberOfMinutes = 3; data = GetOrSetCache(()=> DateTime.Now.ToShortDateString(),"name1",numberOfMinutes ); 同场加映: 扩展方法和界面搭配使用 public class WebDefaultCache : ICache { Cache cacheContainer = HttpRuntime.Cache; public object Get(string key) { return cacheContainer.Get(key); } public void Set(string key,object obj) { cacheContainer.Insert(key,obj); } } public interface ICache{ void Set(string key,object obj); object Get(string key); } public static class InfrastructureExtension { public static TObj GetOrSetCache 虽然在使用上和第三种一样 同场加映程序我放在我自己常用的ExtenionTool项目中 如果本文对您帮助很大,可街口支付斗内鼓励石头^^ 原文:大专栏 ?Asp.net使用缓存 (二) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – background-image无法正常工作
- asp.net-mvc – Asp.net MVC控制器Action字符串参
- 未能加载文件或程序集或它的某一个依赖项
- asp.net-mvc – WebSecurity.InitializeDatabase
- asp.net-mvc – 我如何可以渲染局部视图在asp.ne
- asp.net core生成路由连接,请求参数如何获取和模
- asp.net-mvc – 如何将模型加载到_Layout.cshtml
- 我可能正在开发一个ASP.NET应用程序. Java / PHP
- asp.net – DotNetNuke模块Page_Load触发两次
- 使用ASP.NET重新发明我的身份验证策略