在Windows .NET平台下使用Memcached (Enyim使用)

时间:2022-12-19 12:06:58

1. 启动并配置Memcached的服务端

1. 下载Memcached  http://download.csdn.net/download/ful1021/7969231

2. 解压到任意目录下,例如: 解压缩文件到c:/memcached ,进入cmd控制台

3. 命令行输入 'c:/memcached/memcached.exe -d install'       --安装 Memcached

4. 命令行输入 'c:/memcached/memcached.exe -d start' ,该命令启动 Memcached ,默认监听端口为 11211。

注:通过 memcached.exe -h 可以查看其帮助     这里不多做介绍了

2. .NET平台使用enyim
Memcached Providers使用,可以参考 http://blog.csdn.net/dinglang_2009/article/details/6917794

Enyim也是应用比较广泛的Memcached .net客户端,和之前的Memchachedonet 客户端相比,分布式算法应该做了相应优化

开源中国地址: http://www.oschina.net/p/eniym

GitHub地址: https://github.com/enyim/

下载地址:https://github.com/enyim/EnyimMemcached/downloads

配置

<configuration>
<configSections>
    <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching"/>
</configSections>
  <memcached>
    <keyTransformer type="Enyim.Caching.Memcached.TigerHashKeyTransformer, Enyim.Caching"/>
    <servers>
      <add address="127.0.0.1" port="11211"/>
    </servers>
    <socketPool minPoolSize="2" maxPoolSize="200" connectionTimeout="00:00:10" deadTimeout="00:02:00"/>
  </memcached>
</configuration>
简单的封装了一个类
public class DataCache
{
/// <summary>
/// memcached缓存数据
/// </summary>
/// <param name="dt"></param>
/// <param name="mc"></param>
/// <param name="key"></param>
/// <param name="exp">过期时间</param>
public static void StoreCache(Object dt, MemcachedClient mc, String key, int exp)
{
var finish = mc.Store(StoreMode.Set, key, dt, DateTime.Now.AddDays(exp));
}

/// <summary>
/// 获取缓存数据
/// </summary>
/// <param name="key">缓存键</param>
/// <returns></returns>
public static Object GetCache(String key)
{
var mc = new MemcachedClient("memcached");
Object obj = mc.Get(key);
return obj;
}

/// <summary>
/// 设置缓存
/// </summary>
/// <param name="key">缓存键</param>
/// <param name="obj">缓存对象</param>
/// <param name="exp">缓存天数</param>
public static void SetCache(String key, Object obj, int exp)
{
var mc = new MemcachedClient("memcached");
StoreCache(obj, mc, key, exp);
}
}

3. 相关参考
1. NET中使用Memcached的相关资源整理:http://www.cnblogs.com/dudu/archive/2009/07/19/1526407.html

2.Windows下的.NET+ Memcached安装:http://www.cnblogs.com/luluping/archive/2009/01/14/1375453.html

3. 在Windows .NET平台上使用memcached:http://blog.csdn.net/dinglang_2009/article/details/6917794

4.使用Memcached提高.NET应用程序的性能:http://zhoufoxcn.blog.51cto.com/792419/528212/
---------------------
作者:我心V飞翔
来源:CSDN
原文:https://blog.csdn.net/ful1021/article/details/39525903
版权声明:本文为博主原创文章,转载请附上博文链接!