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 ...
随机推荐
- cassandra 鉴权
1. 修改cassandra.yaml配置文件.启用用户密码登录形式. authenticator: PasswordAuthenticator authorizer: CassandraAuthor ...
- asp.net core 一 Centos 环境部署
.netcore的运行环境,创建asp.net core 项目 CentOS 7 ,dotnet-sdk-2.0.0-2.0.0-1.x86_64 直接在liunx创建项目并运 ...
- MYSQL索引的类型和索引的方式
索引的类型: normal:表示普通索引 unique:表示唯一的,不允许重复的索引,如果该字段信息保证不会重复例如身份证号用作索引时,可设置为unique full textl: 表示 全文搜索的索 ...
- 老男孩Python全栈开发(92天全)视频教程 自学笔记08
day8课程内容: 文件操作 f=open('小重山','r',encoding='utf8') #以读的方式打开文件 data=f.read() print(data) f.close() # ...
- 震撼功能:逐浪CMS全面支持PWA移动生成意指未来
Progressive Web App, 简称 PWA,是提升 Web App 的体验的一种新方法,能给用户原生应用的体验. PWA 能做到原生应用的体验不是靠特指某一项技术,而是经过应用一些新技术进 ...
- 在kali安装中文输入法的教程
1终端下vi /etc/apt/sources.list 修改镜像元 (按E进行编辑 具体实例不同可能没有) 按 i进入编辑 擦除原有的几个官方源改为deb http://mirrors.ali ...
- ACdream1032 Component 树形DP
思路:dp[i][j]表示以i为根结点有j个连通节点的最小和, 当进行状态转移时需要利用01背包,节点u下面有多个子节点,每个子节点可以最多可以贡献cnt[v]个节点,cnt[v]表示以v为根结点的树 ...
- hdu1394 分治 or 线段树
利用分治求一次逆序数,然后每次把第一个元素放到末尾,设该交换元素的值为x,设上一次求得的逆序数为y,那么此时的逆序数等于y - x + (n - x - 1),减去x是因为x作为第一个元素,其后共有x ...
- java日期转化
package com.kang.util; import java.text.ParseException; import java.text.SimpleDateFormat; import ja ...
- 如何更改Ubuntu的root密码
安装Ubuntu系统时,只提示了设定用户密码,该密码可用于普通用户暂时获取root的权限,执行一些需要root权限的操作,而没有要求我们设置root密码,在需要用到root密码时,却想不起来,很尴尬啊 ...