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 ...
随机推荐
- appium+Python 启动app(一)
当我们appium和Python环境都配置好了,如何启动我们第一个app呢?下面介绍appium+Python启动app的操作步骤,为了能够详细查看,我们这里使用夜游神模拟器进行示范. 测试项目:QQ ...
- Java经典编程题50道之二十一
求1+2!+3!+...+20!的和. public class Example21 { public static void main(String[] args) { sum( ...
- HTML5-svg圆形饼状图进度条实现原理
<svg width="440" height="440" viewbox="0 0 440 440"> <circle ...
- Caused by: java.lang.ClassCastException: org.springframework.web.SpringServletContainerInitializer cannot be cast to javax.servlet.ServletContainerInitializer错误解决办法
严重: Failed to initialize end point associated with ProtocolHandler ["http-bio-8080"] java. ...
- 使用commons-csv简单读写CSV文件
文章首发于我的github博客 需求 客户的开发测试环境将做迁移.因此需要对zookeeper上的重要的数据以CSV文件格式做备份. 本文通过Apache的commons-csv操作CSV文件.官网地 ...
- HTTP状态码作用
文章目录 状态码分类 常见的状态码 1xx消息 2xx成功 3xx重定向 4xx客户端错误 5xx服务器错误 HTTP状态码的作用是:web服务器用来告诉客户端,发生了什么事. 状态码位于HTTP R ...
- Android设置View抖动动画
在应用中,有时候我们要吸引用户去点击某些按钮,比如应用市场的推荐按钮,为了能够吸引用户主动点击而且不过分的打扰用户,最好的方法就是给我们想吸引用户注意的view添加一些抖动动画,比如这张图 这里我主要 ...
- Css中路径data:image/png;base64的用法详解
今天查看一些网站的css中发现了 background-image:url(data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAEAAAAkCAYAAAB ...
- javascript DOM document对象
document对象代表整个html文档 用来访问页面所有元素最复杂的一个dom对象 也是window对象的一个子对象. 对于dom编程中,一个html就会当成一个dom树dom会把所有的html元素 ...
- 个性化WinPE封装方法----制作过程需要了解的“命令”
1.在现有的Windows7条件下,自动在E盘建立mywinpe文件夹,设置 Windows PE 构建环境,并保存到E:\mywinpe下 copype.cmd x86 E:\mywinpe 2.将 ...