首先nuget安装EnyimMemcached,本地启动memcache,往app.config(mvc项目则是web.config)加入以下内容:

configSection内加入:

<sectionGroup name="enyim.com">
<section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" />
</sectionGroup>

configSection外加入:

<enyim.com>
<memcached protocol="Text">
<servers>
<!-- make sure you use the same ordering of nodes in every configuration you have -->
<add address="127.0.0.1" port="11211" />
<!-- <add address="ip address" port="port number" />-->
</servers>
<socketPool minPoolSize="5" maxPoolSize="10" /> </memcached>
</enyim.com>

这里要注意一点:教程上写<memcached protocol="Text">中protocol可配置为Binary,但是如果这么配的话必定会连不上memcache。

完整配置示例:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="enyim.com">
<section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" />
</sectionGroup>
</configSections> <enyim.com>
<memcached protocol="Text">
<servers>
<!-- make sure you use the same ordering of nodes in every configuration you have -->
<add address="127.0.0.1" port="11211" />
</servers>
<socketPool minPoolSize="5" maxPoolSize="10" /> </memcached>
</enyim.com>
<!--
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>-->
</configuration>

EnyimMemcached的详细配置参数:https://github.com/enyim/EnyimMemcached/wiki/MemcachedClient-Configuration

具体用法:https://github.com/enyim/EnyimMemcached/wiki/MemcachedClient-Usage

封装MemcacheHelper类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Enyim.Caching;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached; namespace Common
{
public class MemcachedHelper
{
private static MemcachedClient MemClient;
static readonly object padlock = new object(); //线程安全的单例模式
public static MemcachedClient getInstance()
{
if (MemClient == null)
{
lock (padlock)
{
if (MemClient == null)
{
MemClientInit();
}
}
}
return MemClient;
} static void MemClientInit()
{
try
{
MemClient = new MemcachedClient();
}
catch (Exception ex)
{
throw ex;
}
} /// <summary>
/// 插入指定值
/// </summary>
/// <param name="key">缓存名称 </param>
/// <param name="value">缓存值</param>
/// <param name="dateTime">过期时间</param>
/// <returns>返回是否成功</returns>
// public static bool Set(string key, string value, int minutes = 10080)
public static bool Set(string key, string value, DateTime dateTime)
{
MemcachedClient mc = getInstance();
var data = mc.Get(key); if (data == null)
return mc.Store(StoreMode.Add, key, value, dateTime);
else
return mc.Store(StoreMode.Replace, key, value, dateTime);
} /// <summary>
/// 插入指定值
/// </summary>
/// <param name="key">缓存名称 </param>
/// <param name="value">缓存值</param>
/// <returns>返回是否成功</returns>
// public static bool Set(string key, string value, int minutes = 10080)
public static bool Set(string key, string value)
{
MemcachedClient mc = getInstance();
var data = mc.Get(key); DateTime dateTime = DateTime.Now.AddMinutes();
if (data == null)
return mc.Store(StoreMode.Add, key, value, dateTime);
else
return mc.Store(StoreMode.Replace, key, value, dateTime);
} /// <summary>
/// 获取缓存值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static object Get(string key)
{
MemcachedClient mc = getInstance();
return mc.Get(key);
} /// <summary>
/// 删除指定缓存
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool Remove(string key)
{
MemcachedClient mc = getInstance(); return mc.Remove(key);
} /// <summary>
/// 清空缓存服务器上的缓存
/// </summary>
public static void FlushCache()
{
MemcachedClient mc = getInstance(); mc.FlushAll();
}
} }

c# 使用EnyimMemcached 连接memcache的更多相关文章

  1. nginx第三方库安装以及连接memcache

    一.nginx第三方模块的安装 第三方模块查询地址:https://www.nginx.com/resources/wiki/modules/ 后来新出来一个nginx memcache增强版,有空可 ...

  2. memcached命令行、Memcached数据导出和导入、php连接memcache、php的session存储到memcached

    1.memcached命令行 telnet 127.0.0.1 11211set key2 0 30 2abSTOREDget key2VALUE key2 0 2abEND  如: set key3 ...

  3. python memcache操作-安装、连接memcache

    安装memecache wget http://memcached.org/latest tar -zxvf memcached-1.x.x.tar.gz cd memcached-1.x.x ./c ...

  4. Memcached总结二:Memcached环境安装设置以及连接memcache服务器

    1 在Ubuntu上安装Memcached 要在Ubuntu上安装Memcached,打开终端,然后输入以下命令: $sudo apt-get update $sudo apt-get install ...

  5. MemCached总结一:Unbutu操作系统下memcached服务器安装和telnet方式连接memcache

    1.在Unbutu上安装memcached服务器 sudo apt-get update sudo apt-get install memcached 2. 确认memcached是否安装 要确认me ...

  6. Ubuntu中安装memcache并且在Python中连接memcache

    1.安装memcache到Ubuntu. PS:依赖libevent,需要提前安装 yum install libevent-devel #centos中使用这个 apt-get install li ...

  7. PHP连接Memcache代码

    <?php $mem = new Memcache; $mem->connect('127.0.0.1', 11211) or die ("Could not connect&q ...

  8. Linux 连接memcache 拒绝连接,防火墙关闭,selinux disabled 仍然不行,最后在外站找到原因,为服务器添加memcache访问权限

    最后啊,不行,直接装memcached  https://www.runoob.com/memcached/memcached-install.html 附上连接:https://www.presta ...

  9. 通过telnet连接查看memcache服务器(转)

    memcache作为一款优秀的进程外缓存,常常被运用于高并发系统架构中.这里主要谈谈怎么通过telnet工具,查看memcache运行状况并对其key进行管理维护.假设memcache安装目录:/us ...

随机推荐

  1. Bootstrap表单验证

    主要用过两个: jqBootstrapValidation: https://github.com/ReactiveRaven/jqBootstrapValidation bootstrapValid ...

  2. mac idea中的Application Server was not connected before run configuration stop, reason: Unable to ping server at localhost:1099问题

    今天上午用的好好的就出现了这个问题 真是奇怪: Google了一番有以下解决办法: 1.hosts文件的问题: vi /etc/hosts文件 添加127.0.0.1  localhost里面 2.m ...

  3. Ubuntu上搭建SVN

    参考文档:http://www.linuxidc.com/Linux/2016-08/133961.htm http://www.linuxidc.com/Linux/2015-01/111956.h ...

  4. MysqL自动提交机制的关闭

    MysqL在执行一句数据库操作命令的时候,通常都是自动提交的.常用引擎下有两种,分别是MyIsam和InnoDB,MyIsam是不支持事务处理的,但InnoDB支持,但InnoDB在不开启事务处理的情 ...

  5. Linux下yum安装MysqL数据库

    1.命令安装mysql # yum install mysql mysql-server mysql-devel -y 最后提示 Complete!  表示安装成功 2.查看是否生成了mysqld服务 ...

  6. Asp.Net Core 2.1+的视图缓存(响应缓存)

    响应缓存Razor 页与 ASP.NET 核心 2.0 中不支持. 此功能将支持ASP.NET 核心 2.1 版本. 在老的版本的MVC里面,有一种可以缓存视图的特性(OutputCache),可以保 ...

  7. DLL文件修复

    当你在Windows计算机中安装非操作系统的软件时,往往会覆盖或改写系统共享文件, 如动态链接库(.dll文件)和可执行文件(.exe文件). * 对于Windows系统来说,当用户操作不当(如非正常 ...

  8. uva10410 栈

    根据DFS和BFS重建树. BFS反映了当前节点到达根结点的距离,通过栈把当前处理的树或则子树的根结点放在栈顶,通过遍历DFS序列,判断当前元素与栈顶元素的关系,如果是子节点,就将它压入栈中成为新的栈 ...

  9. 【转载】一行代码加载网络图片到ImageView——Android Picasso

    原文链接:一句代码加载网络图片到ImageView——Android Picasso  注意:此处使用下面代码需要先配置一下gradle,下载所需包. 具体操作如下图: compile 'com.sq ...

  10. php-fpm问题

    这个问题怎么说呢?之前遇到这个问题内心是奔溃的.因为我压根不知道是哪里出问题啦.不过,在我努力探索下,最终还是解决了问题. so请记住,坚持不一定成功,但放弃一定失败. 简单描述一下问题: 1.本地的 ...