NorthScale Memcached Server的.NET客户端NorthScaleClient使用简介

博客园已经全面使用NorthScale Memcached Server,用下来感觉不错。下面简单分享一下.NET客户端的使用方法。

NorthScale Memcached Server官方推荐的.NET客户端是EnyimMemcached。EnyimMemcached有两种方式可以访问NorthScale Memcached Server:

1) Enyim.Caching.MemcachedClient(Enyim.Caching.dll);

2) NorthScale.Store.NorthScaleClient(Northscale.Store.dll)。NorthScaleClient是基于Enyim.Caching针对NorthScale Memcached Server专门开发的客户端。

这两个客户端的主要区别在于:

1)Enyim.Caching.MemcachedClientt只能访问NorthScale Memcached Server的默认Bucket(默认访问服务器11211端口);

2)NorthScale.Store.NorthScaleClient只能访问NorthScale Memcached Server(默认访问服务器8080端口)。

这里主要讲一下NorthScaleClient的使用方法:

1. 下载NorthScaleClient,下载地址:http://github.com/enyim/EnyimMemcached/downloads

2. 在项目中添加对Northscale.Store.dll的引用。

3. 在web.config中添加相应的配置:

  a) 在configSections中添加下面的配置:

<section name="northscale" type="NorthScale.Store.Configuration.NorthScaleClientSection, NorthScale.Store" />

  b) 在configuration中添加如下的配置:

<northscale>
<servers>
<add uri="http://localhost:8080/pools/default" />
<add uri="http://localhost:8080/pools/default" />
</servers>
<socketPool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:00:10" deadTimeout="00:02:00"/>
</northscale>

  注:如果只有一台Memcached服务器,要在这里填两个相同的uri,不然调用时会报错。

4. 客户端调用示例代码:

复制代码
public class EnyimMemcachedProvider : ICacheProvider
{
private static NorthScaleClient client;

static EnyimMemcachedProvider()
{
try
{
client
= new NorthScaleClient();
}
catch (Exception ex)
{
log4net.LogManager.GetLogger(
"cnblogs").Info("EnyimMemcachedProvider", ex);
}
}

#region ICacheProvider Members

public void Add(string key, object value)
{
if (client != null)
{
client.Store(StoreMode.Set, key, value);
}
}

public void Add(string key, object value, int cacheSecond)
{
if (client != null)
{
client.Store(StoreMode.Set, key, value,
new TimeSpan(0, 0, cacheSecond));
}
}

public object GetData(string key)
{
if (client == null)
{
return null;
}
return client.Get(key);
}

public void Remove(string key)
{
if (client != null)
{
client.Remove(key);
}
}

#endregion
}
复制代码

需要注意的地方:

1) 由于创建一个NorthScaleClient的对象成本很高,所以这里要使用静态变量private static NorthScaleClient client。

2) 在NorthScale Memcached Server停运时,NorthScaleClient会抛出"none of the pool urls are working."的异常,而不是写入日志,这样会影响网站的正常访问。建议在静态构造函数中创建NorthScaleClient对象,在创建对象失败时捕获异常并写入日志。

在高性能.NET Web应用开发中,缓存是很重要的环节,之前.NET平台缺少好的缓存解决方案,而NorthScale Memcached Server+EnyimMemcached客户端让我们眼前一亮。

由于刚接触NorthScale Memcached Server,理解还不深,这篇随笔写得也很简单,只是希望能抛个砖,希望园子里有更多朋友分享与探讨在缓存方面的心得。

 

相关随笔:

介绍一款Memcached服务器软件:NorthScale Memcached Server

博客园已经用上NorthScale Memcached Server

posted @   dudu  阅读(4604)  评论(35编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~
历史上的今天:
2006-08-09 博客园Blog程序遇到的奇怪问题
2006-08-09 爬网的影响
2005-08-09 Skin设计小组新作品发布—CornflowerBlue
点击右上角即可分享
微信分享提示