我们经常会缓存一些对象,比如远程的,不能时时刻刻都去申请远程对象和操作非常繁琐的对象,这样对系统资源和服务器压力产生了极大的影响,所以我们会选择先把这些对象缓存起来,然后用的时候随时拿来用.但是缓存的时候会发现如果缓存的对象对了,层次就麻烦了,自己都不知道缓存对象的结构,这是不可见的.而且缓存的方式太多,可能团队里缓存的方式不一样操作起来也麻烦. 自己就写了这么一个小的框架来解决缓存的问题.
这是大概思路:通过继承一个基类来规定所有缓存的方法,把缓存结构保存到xml里,然后每个缓存类都在遵守这样的规定下扩展自己的功能.缓存基类莫过于,存取对象:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Xml;
using System.Security;
/*******************************
* CWF.CachService 框架缓存服务
* 开发者:欧阳寒玟
* 开发时间:2010-04-27
* 修改时间:2010-04-27
* ****************************/
namespace CWF.CacheService
{
/// <summary>
/// 缓存基类
/// 所有的缓存服务都必须继承于这里方便扩展
/// </summary>
public class ACache
{
/// <summary>
/// 添加一个缓存对象
/// </summary>
/// <param name="Xpath">Xpath树节点</param>
/// <param name="O">要缓存的对象</param>
public virtual void AddCacheObj(string Xpath, object O)
{
}
/// <summary>
/// 获取一个已缓存的对象
/// </summary>
/// <param name="Xpath">Xpath树节点</param>
/// <returns>objec</returns>
public virtual object ReceveCacheobj(string Xpath)
{
return null;
}
/// <summary>
/// 获取已缓存对象的数组
/// </summary>
/// <param name="Xpath">Xpath树节点</param>
/// <returns>object[]</returns>
public virtual object[] ReceveCacheobjlist(string Xpath)
{
return null;
}
/// <summary>
/// 取消缓存
/// </summary>
/// <param name="Xpath">Xpath树节点</param>
public virtual void RemoveCacheObj(string Xpath)
{
}
}
}
定义了3个方法,存单个对象,取单个对象,取一个对象数组.
然后就是对缓存的整体操作:
通过这个缓存操作类我们可以方便的操作每一个自己扩展的基类,然后实现存取对象,这里我们定义的缓存对象保存在自己的框架配置文件里,这样可以把缓存类和实际的逻辑操作完全解耦合.而且可以任意更换缓存操作类而不需要修改一句代码.
using System;
using System.Collections.Generic;
using System.Text;
using CWF.ConfigManager;
using System.Configuration;
/*******************************
* CWF.CachService 框架缓存服务
* 开发者:欧阳寒玟
* 开发时间:2010-04-27
* 修改时间:2010-04-27
* ****************************/
namespace CWF.CacheService
{
/// <summary>
/// 缓存服务类
/// </summary>
public class Cache
{
private static ACache cache;
private static object lockObj = new object();
private Cache()
{
ConfigManager.ConfigManager configmanage = (ConfigManager.ConfigManager)ConfigurationManager.GetSection("Framework");
CacheConfig ccfig = configmanage.getCacheConfig();
cache = (ACache)ccfig.getCache("SampleCache");
}
private Cache(string CacheName)
{
ConfigManager.ConfigManager configmanage = (ConfigManager.ConfigManager)ConfigurationManager.GetSection("Framework");
CacheConfig ccfig = configmanage.getCacheConfig();
cache = (ACache)ccfig.getCache(CacheName);
}
/// <summary>
/// 获取缓存对象
/// </summary>
/// <returns></returns>
public static ACache GetCWFCaheService()
{
if (cache == null)
{
lock (lockObj)
{
if (cache == null)
{
new Cache();
}
}
}
return cache;
}
/// <summary>
/// 获取缓存对象
/// </summary>
/// <returns></returns>
public static ACache GetCWFCaheService(string CacheName)
{
if (cache == null)
{
lock (lockObj)
{
if (cache == null)
{
new Cache(CacheName);
}
}
}
return cache;
}
}
}
下面是一个扩展的简单的缓存操作类,通过hashtable的静态变量来操作缓存,也可以自己扩展自己的操作类来实际操作缓存.
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Xml;
using System.Security;
namespace CWF.CacheService
{
/// <summary>
/// 简单缓存服务类
/// 开发者:欧阳寒玟
/// 开发时间:2010-04-20
/// 修改时间:2010-04-28
/// </summary>
public class SampleCache:CWF.CacheService.ACache
{
/// <summary>
/// 缓存数据库
/// </summary>
private Hashtable cachTable;
/// <summary>
/// 缓存层次表
/// </summary>
private XmlDocument xmldoc;
/// <summary>
/// 实例化
/// </summary>
public SampleCache()
{
xmldoc=new XmlDocument() ;
xmldoc.AppendChild(xmldoc.CreateElement("root"));
cachTable = new Hashtable();
}
/// <summary>
/// 添加一个缓存对象
/// </summary>
/// <param name="obj"></param>
/// <param name="type"></param>
public override void AddCacheObj(string Xpath, object type)
{
string Guid= System.Guid.NewGuid().ToString ();
string xmlTree = Xpath.Split('/')[Xpath.Split('/').Length - 1].ToString();
PareparXpath(Xpath);
XmlNode node = xmldoc.FirstChild.SelectSingleNode(Xpath.Remove(Xpath.LastIndexOf('/'), Xpath.Length - Xpath.LastIndexOf('/')));
XmlNode child = xmldoc.CreateNode(XmlNodeType.Element, xmlTree, null);
((XmlElement)child).SetAttribute("Guid", Guid);
node.AppendChild(child);
cachTable.Add(Guid, type);
}
/// <summary>
/// 获取一个已缓存的对象
/// </summary>
/// <param name="Xpath"></param>
/// <returns></returns>
public override object ReceveCacheobj(string Xpath)
{
XmlNode node = xmldoc.FirstChild.SelectSingleNode(Xpath);
object obj = null;
if (node != null)
{
string uid = node.Attributes["Guid"].Value;
obj = cachTable[uid];
}
return obj;
}
/// <summary>
/// 获取缓存对象数组
/// </summary>
/// <param name="Xpath"></param>
/// <returns></returns>
public override object[] ReceveCacheobjlist(string Xpath)
{
XmlNode node = xmldoc.FirstChild.SelectSingleNode(Xpath);
XmlNodeList nodelist = node.ChildNodes;
object[] objlist = new object[nodelist.Count];
for (int i = 0; i < nodelist.Count; i++)
{
string Guid = nodelist[i].Attributes["Guid"].Value;
objlist[i] = cachTable[Guid];
}
return objlist;
}
/// <summary>
/// 移除一个已保存的对象
/// </summary>
/// <param name="Xpath"></param>
public override void RemoveCacheObj(string Xpath)
{
XmlNode node = xmldoc.FirstChild.SelectSingleNode(Xpath);
if (node.HasChildNodes)
{
foreach (XmlNode N in node)
{
if (N.Attributes["Guid"].Value != null)
{
cachTable.Remove(N.Attributes["Guid"].Value);
}
}
}
if (node.Attributes["Guid"].Value != null)
{
cachTable.Remove(node.Attributes["Guid"].Value);
}
xmldoc.FirstChild.RemoveChild(node);
}
/// <summary>
/// 确认xml数的存在,不存在则创建
/// </summary>
/// <param name="Xpath"></param>
private void PareparXpath(string Xpath)
{
string rootxml = "//root";
XmlNode root = xmldoc.SelectSingleNode(rootxml+"/"+Xpath);
if (root == null)
{
string[] nodes = Xpath.Split('/');
for (int i = 0; i < nodes.Length-1;i++ )
{
XmlNode child = xmldoc.CreateElement( nodes[i].ToString());
XmlNode node = xmldoc.SelectSingleNode(rootxml);
node.AppendChild(child);
rootxml =rootxml+"/"+ nodes[i].ToString();
}
}
}
}
}
缓存操作这里就完了,不管你是用cach还是static来操作缓存或者用其它第三方控件都没关系,这都可以满足你的要求,当然这也是个简单的案例,思路 是这样可以自己扩展,如果转贴请文明转贴,出处