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 ...
随机推荐
- FLEX类似谷歌地图拖拽功能
要实现类似于谷歌地图拖拽功能,可以用s:Scroller标签来实现,代码如下: mxml: <s:Scroller width="100%" height="100 ...
- HDU 5060
题意略. 这个题目最关键的是在于计算球冠的体积.令球冠体积为V. 我们可以用祖暅原理来计算V, 这里,可以看出,球冠的体积等于左图的上半个圆柱减去那个倒扣的圆台. 祖暅原理:界于两个平行平面之间的两个 ...
- windows转mac-开发环境搭建(一):需要搭建的环境及安装的工具
作为一个java后端开发者来说,随着项目的增加,前段时间用windows真是受尽折磨,电脑卡到不行,在我们开发部技术大佬的一再安利之下,狠下心选了个17年13寸带touch bar的MacBook P ...
- uva1625
思路:每次选择颜色面临有两个选择:1.序列A的首部颜色 2.序列B的首部元素,定义状态d[i][j]表示A序列已经选取了前i个颜色,B序列已经选取了前j个颜色的情况下最小的L(c)总和. 状态转移:c ...
- 求第k小的数 O(n)复杂度
思路:利用快速排序的思想,把数组递归划分成两部分.设划分为x,数组左边是小于等于x,右边大于x.关键在于寻找一个最优的划分,经过 Blum . Floyd . Pratt . Rivest . Tar ...
- ARC068E - Snuke Line
原题链接 题意简述 给出个区间和.求对于任意,有多少个区间包含的倍数. 题解 考虑怎样的区间不包含的倍数. 对于的倍数和,满足的区间不包含任何的倍数. 于是转化为二维数点问题,可以用可持久化线段树解决 ...
- dll和lib(包括静态链接库和与dll同时生成的lib)
转:http://blog.csdn.net/galaxy_li/article/details/7411956 1:神马是Dll和Lib,神马是静态链接和动态链接 大家都懂的,DLL就是动态链接库, ...
- NewLife.XCode 上手指南2018版(一)代码生成
目录 NewLife.XCode 上手指南2018版(一)代码生成 NewLife.XCode 上手指南2018版(二)增 NewLife.XCode 上手指南2018版(三)查 NewLife.XC ...
- List,Set,Map
1.Collection 和 Map 的区别 容器内每个为之所存储的元素个数不同.Collection类型者,每个位置只有一个元素.List,SetMap类型者,持有 key-value pair,像 ...
- 2018.3.6学习java的第一天
学习java,那么不得不先了解一下java,java分为三部分,JAVA SE,JAVA EE和JAVA ME,其中前期我们首先要学习Java SE.java是一门语言,我们平时人与人之间对话,用中文 ...