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(10080);
            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();
        }
    }

}

 

转载于:https://www.cnblogs.com/axel10/p/8378865.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值