c# 使用EnyimMemcached 连接memcache
首先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的更多相关文章
- nginx第三方库安装以及连接memcache
		一.nginx第三方模块的安装 第三方模块查询地址:https://www.nginx.com/resources/wiki/modules/ 后来新出来一个nginx memcache增强版,有空可 ... 
- 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 ... 
- python memcache操作-安装、连接memcache
		安装memecache wget http://memcached.org/latest tar -zxvf memcached-1.x.x.tar.gz cd memcached-1.x.x ./c ... 
- Memcached总结二:Memcached环境安装设置以及连接memcache服务器
		1 在Ubuntu上安装Memcached 要在Ubuntu上安装Memcached,打开终端,然后输入以下命令: $sudo apt-get update $sudo apt-get install ... 
- MemCached总结一:Unbutu操作系统下memcached服务器安装和telnet方式连接memcache
		1.在Unbutu上安装memcached服务器 sudo apt-get update sudo apt-get install memcached 2. 确认memcached是否安装 要确认me ... 
- Ubuntu中安装memcache并且在Python中连接memcache
		1.安装memcache到Ubuntu. PS:依赖libevent,需要提前安装 yum install libevent-devel #centos中使用这个 apt-get install li ... 
- PHP连接Memcache代码
		<?php $mem = new Memcache; $mem->connect('127.0.0.1', 11211) or die ("Could not connect&q ... 
- Linux 连接memcache 拒绝连接,防火墙关闭,selinux disabled 仍然不行,最后在外站找到原因,为服务器添加memcache访问权限
		最后啊,不行,直接装memcached https://www.runoob.com/memcached/memcached-install.html 附上连接:https://www.presta ... 
- 通过telnet连接查看memcache服务器(转)
		memcache作为一款优秀的进程外缓存,常常被运用于高并发系统架构中.这里主要谈谈怎么通过telnet工具,查看memcache运行状况并对其key进行管理维护.假设memcache安装目录:/us ... 
随机推荐
- C语言学习之插入排序
			此前的一些博文分别写了C语言中经典的排序方式,选择排序 冒泡排序 桶排序,此文就写 插入排序吧. 相对于冒泡排序,插入排序就比较方便快捷了.和冒泡 选择排序一样,插入排序也需要比较大小.可以这样理解插 ... 
- 你不知道的JavaScript之类型
			JavaScript是一门简单易用的语言,应用广泛,同时它的语言机制又十分复杂和微妙,即使经验丰富的开发人员也需要用心学习才能真正掌握. <你不知道的JavaScript>中是这样定义类型 ... 
- HDU - 1172
			思路:假设答案是x,那么x必定满足所有语句给定的条件.例如3585和4815就有2个相同的数,1和相同的位. 只要这个数满足所有条件,那么就是一个可能的答案,当答案数量超过1个时,就是"No ... 
- 关键字final的用法
			final关键字可以用来修饰类.方法和变量. 1.final修饰的类不能被继承. 2.final修饰的方法不能被重写. 3.final修饰的变量是常量,不能修改其值. 
- Web/app端自动化测试对比
			Web/app端自动化测试 做了一段时间的Android自动化测试,对比个人之前做的web端自动化测试,有一些感想.(由于个人接触的时间也不是太久,很多东西理解也并不深刻,先写下菜鸟时期的感想.) 区 ... 
- php留言板
			这个小项目的学习,就这样结束啦.由于过程中需要使用到js,这个目前还是感觉不会.之前的分析还是不太懂的.现在心里还是有点迷茫.什么都是照着葫芦画瓢. 我的拥有自己的东西才行. 
- SQL语句学习
			看似简单,但其实包含很多技巧思维 1.查询课程表中所有科目大于80的学生 select distinct name from student where name not in (select nam ... 
- Centos安装jdk8
			1.下载jdk1.8的tar cd /usr/local/src #切换到该目录下 wget url #下载jdk8的tar包 2.下载完成后解压tar包 tar -zxvf jdk-8u152-li ... 
- windowsXP下搭建JAVA环境教程
			一.工具准备安装JKD6:传送门:http://www.java.net/download/jdk6/6u10/promoted/b32/binaries/jdk-6u10-rc2-bin-b32-w ... 
- Struts2实现文件上传(三)
			Struts2实现文件上传 配置文件web.xml <?xml version="1.0" encoding="UTF-8"?> <web-a ... 
