您的位置:首页 > 运维架构 > 网站架构

网站架构之缓存应用(3)

2011-01-17 10:48 218 查看
实现篇

这篇来讲如何利用memcached实现一级缓存,以及如何让一级缓存组件支持在企业库,memcached或者其它第三方实施方案之间的切换。memcached本人并没有太多经验,如果文中有说的不对的地方,还希望批评指出,且文中关于memcached的代码大多来自网络。

创建memcached实现类MemcachedWebCacheProvider,由它来继承缓存提供者接口IWebCacheProvider,主里memcached客户端我采用.NET
memcached client library ,这个类库很久没有更新这过了,没有和java版同步,有部分功能目前没有实现。

1:初始化memcached服务,这段初始化代码在程序中保证执行一次就够,一般可以放在gloabl文件中,或者是设置一个静态变量来存储服务的状态。

代码

public class CacheHelper
{
/// <summary>
/// 主分区
/// </summary>
public const string REGION = "MyBlog";
/// <summary>
/// 子分区
/// </summary>
public const string SUB_REGION = "default";
public const string BlogListConfigKey = "BlogListConfigKey";
#region 页面间数据传递
/// <summary>
/// 新增页面间传递数据到WebCache
/// </summary>
/// <returns>返回PageKeyID,用于页面间传递的键值</returns>
public static string InsertPageParams(string configKey, object obj,string pageKey)
{
string result = null;

MyWebCacheServiceClient cacheClient = CacheClientFactory.GetWebCacheServiceClient(REGION, SUB_REGION, configKey);
cacheClient.Insert(
MyWebCacheServiceClient.BuildKey(configKey,pageKey),
obj,
REGION,
SUB_REGION);

return result;
}
/// <summary>
/// 从Cache里获取页面传递Cache
/// </summary>
/// <param name="key">FlightCacheKey里的常量</param>
/// <param name="pageKeyID">页面传递的键值</param>
public static object GetPageParams(string configKey, string pageKey)
{
object result = null;
MyWebCacheServiceClient cacheClient = CacheClientFactory.GetWebCacheServiceClient(REGION,
SUB_REGION, configKey);
result = cacheClient.Get(
MyWebCacheServiceClient.BuildKey(configKey, pageKey),
REGION,
SUB_REGION);

return result;

}
#endregion
}

两级缓存类结构图:

以上代码贴出来看起来有点乱,这里贴出网站两级缓存类结构图:



作者:姜敏
出处:http://www.cnblogs.com/aspnet2008/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: