缓存是系统或应用程序将频繁使用的数据保存到存储媒介的能力。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分层架构,加上数据缓存就让我们的系统显得更加的专业。
随着开发和学习的深入,我们将引进更多的高级特性如单元测试等,相信我们团队将会作得更好!