缓存让你的设计更专业
来源:互联网 发布:mac上怎么卸载程序 编辑:程序博客网 时间:2024/04/29 07:38
缓存是系统或应用程序将频繁使用的数据保存到存储媒介的能力。Asp.net提供两种缓存功能:数据缓存和页面缓存,这里对数据缓存做简略的介绍。Cache对象是一个中心容器,他能存储应用程序的全局数据, 可以看作是一个快速存储的数据仓库。
当熟悉了缓存的基本原理之后我们可以按照面向对象的软件工程设计思想对其进行进一步的封装。为了提高类的可复用性这里可以按照两种方式来设计类:1.装箱与拆箱,2.面向对象泛型编程。
泛型类:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Collections;
namespace CachLayer
{
class Caching<T>
{
//缓存键值
private string _cacheKey=string .Empty;
public string CacheKey
{
get { return _cacheKey; }
set { _cacheKey = value; }
}
//过期时间
private int _cacheDurcation;
public int CacheDurcation
{
get { return _cacheDurcation; }
set { _cacheDurcation = value; }
}
Caching() { }
Caching(string key, int durcation)
{
this._cacheKey = key;
this._cacheDurcation = durcation;
}
///从缓存中获取数据
public T GetCache()
{
if (HttpContext.Current.Cache[this._cacheKey] != null)
return (T)HttpContext.Current.Cache.Get(this._cacheKey);
else return default(T);
}
///设置缓存
public void SetCache(T obj, System.Web.Caching.CacheItemPriority priority)
{
DateTime expiration = DateTime.Now.AddMinutes(this._cacheDurcation);
if (HttpContext.Current.Cache[this._cacheKey] != null)
return;
else
HttpContext.Current.Cache.Add(this._cacheKey, obj, null, expiration, TimeSpan.Zero,priority,null);
}
///删除数据缓存
public void Remove()
{
if (HttpContext.Current.Cache[this.CacheKey] == null)
return;
else
HttpContext.Current.Cache.Remove(this._cacheKey);
}
///静态方法清除所有缓存
public static void Clear()
{
foreach (DictionaryEntry entry in HttpContext.Current.Cache)
{
HttpContext.Current.Cache.Remove(entry.Key.ToString());
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Collections;
namespace CachLayer
{
class Caching<T>
{
//缓存键值
private string _cacheKey=string .Empty;
public string CacheKey
{
get { return _cacheKey; }
set { _cacheKey = value; }
}
//过期时间
private int _cacheDurcation;
public int CacheDurcation
{
get { return _cacheDurcation; }
set { _cacheDurcation = value; }
}
Caching() { }
Caching(string key, int durcation)
{
this._cacheKey = key;
this._cacheDurcation = durcation;
}
///从缓存中获取数据
public T GetCache()
{
if (HttpContext.Current.Cache[this._cacheKey] != null)
return (T)HttpContext.Current.Cache.Get(this._cacheKey);
else return default(T);
}
///设置缓存
public void SetCache(T obj, System.Web.Caching.CacheItemPriority priority)
{
DateTime expiration = DateTime.Now.AddMinutes(this._cacheDurcation);
if (HttpContext.Current.Cache[this._cacheKey] != null)
return;
else
HttpContext.Current.Cache.Add(this._cacheKey, obj, null, expiration, TimeSpan.Zero,priority,null);
}
///删除数据缓存
public void Remove()
{
if (HttpContext.Current.Cache[this.CacheKey] == null)
return;
else
HttpContext.Current.Cache.Remove(this._cacheKey);
}
///静态方法清除所有缓存
public static void Clear()
{
foreach (DictionaryEntry entry in HttpContext.Current.Cache)
{
HttpContext.Current.Cache.Remove(entry.Key.ToString());
}
}
}
}
装箱类:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Collections;
namespace CachLayer
{
class Cache
{
//缓存键值
private string _cacheKey = string.Empty;
public string CacheKey
{
get { return _cacheKey; }
set { _cacheKey = value; }
}
//过期时间
private int _cacheDurcation;
public int CacheDurcation
{
get { return _cacheDurcation; }
set { _cacheDurcation = value; }
}
Cache() { }
Cache(string key, int durcation)
{
this._cacheKey = key;
this._cacheDurcation = durcation;
}
///从缓存中获取数据
public object GetCache()
{
if (HttpContext.Current.Cache[this._cacheKey] != null)
return HttpContext.Current.Cache.Get(this._cacheKey);
else return null;
}
///设置缓存
public void SetCache(object obj, System.Web.Caching.CacheItemPriority priority)
{
DateTime expiration = DateTime.Now.AddMinutes(this._cacheDurcation);
if (HttpContext.Current.Cache[this._cacheKey] != null)
return;
else
HttpContext.Current.Cache.Add(this._cacheKey, obj, null, expiration, TimeSpan.Zero, priority, null);
}
///删除数据缓存
public void Remove()
{
if (HttpContext.Current.Cache[this.CacheKey] == null)
return;
else
HttpContext.Current.Cache.Remove(this._cacheKey);
}
///静态方法清除所有缓存
public static void Clear()
{
foreach (DictionaryEntry entry in HttpContext.Current.Cache)
{
HttpContext.Current.Cache.Remove(entry.Key.ToString());
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Collections;
namespace CachLayer
{
class Cache
{
//缓存键值
private string _cacheKey = string.Empty;
public string CacheKey
{
get { return _cacheKey; }
set { _cacheKey = value; }
}
//过期时间
private int _cacheDurcation;
public int CacheDurcation
{
get { return _cacheDurcation; }
set { _cacheDurcation = value; }
}
Cache() { }
Cache(string key, int durcation)
{
this._cacheKey = key;
this._cacheDurcation = durcation;
}
///从缓存中获取数据
public object GetCache()
{
if (HttpContext.Current.Cache[this._cacheKey] != null)
return HttpContext.Current.Cache.Get(this._cacheKey);
else return null;
}
///设置缓存
public void SetCache(object obj, System.Web.Caching.CacheItemPriority priority)
{
DateTime expiration = DateTime.Now.AddMinutes(this._cacheDurcation);
if (HttpContext.Current.Cache[this._cacheKey] != null)
return;
else
HttpContext.Current.Cache.Add(this._cacheKey, obj, null, expiration, TimeSpan.Zero, priority, null);
}
///删除数据缓存
public void Remove()
{
if (HttpContext.Current.Cache[this.CacheKey] == null)
return;
else
HttpContext.Current.Cache.Remove(this._cacheKey);
}
///静态方法清除所有缓存
public static void Clear()
{
foreach (DictionaryEntry entry in HttpContext.Current.Cache)
{
HttpContext.Current.Cache.Remove(entry.Key.ToString());
}
}
}
}
上面的类很简单这里就不再详述了。
在以往的开发中我只用到了MVC分层架构,加上数据缓存就让我们的系统显得更加的专业。
随着开发和学习的深入,我们将引进更多的高级特性如单元测试等,相信我们团队将会作得更好!
- 缓存让你的设计更专业
- 让你的SERV-U FTP看起来更专业一点
- java中的专业英语名词解释-->让你更专业
- 文件缓存的工具类,让你开发更轻松
- 让你的页面更专业--css框架960Grid System
- 程序员专用的简历神器,让你制作简历更简单,方便,专业
- 如何让你的PCB设计更优秀
- 4条心理学原理让你的设计更贴心
- 6个策略让你的表单设计更美好
- (20 完结)使用缓存让你的结果加载的更快
- 让自己变得更专业
- (转)怎么让自己的网站做的更专业
- 让你的设计更简单直观的7个实用规则
- 设计让生活更美好 你所看不到的YunOS背后的故事
- 让你的Blog更漂亮
- 让你的Smarty更聪明
- 让你的WINDOWS XP更爽
- 让你的桌面更干净
- 六间房免费传图
- 是否是危言耸听:都不敢吃盐了?
- 关于phpmyadmin的使用说明问题
- Linux+Apache+Mysql+PHP典型配置
- DIB简介
- 缓存让你的设计更专业
- jade 学习笔记二
- 如何让我们的开发向专业化的方向发展?
- vim+ctags用法
- Exuberant Ctags中文手册
- vim+ctags+taglist插件安装使用vim+ctags+taglist插件安装使用
- 成功往往在失败中酝酿
- http://popoedy.tk/ !! popoedy个人空间!顶级域名!!哈哈
- 迷失在大学...技术的海....