c# 使用EnyimMemcached 连接memcache

2021-02-17 11:16

阅读:721

首先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>

add address="127.0.0.1" port="11211" />

servers>
socketPool minPoolSize="5" maxPoolSize="10" />

memcached>
enyim.com>

 这里要注意一点:教程上写中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>
        
        add address="127.0.0.1" port="11211" />
      servers>
      socketPool minPoolSize="5" maxPoolSize="10"  />

    memcached>
  enyim.com>

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;
            }
        }

        /// 
        /// 插入指定值
        /// 
        /// 缓存名称 
        /// 缓存值
        /// 过期时间
        /// 返回是否成功
//        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);
        }


        /// 
        /// 插入指定值
        /// 
        /// 缓存名称 
        /// 缓存值
        /// 返回是否成功
        //        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);
        }


        /// 
        /// 获取缓存值
        /// 
        /// 
        /// 
        public static object Get(string key)
        {
            MemcachedClient mc = getInstance();
            return mc.Get(key);
        }

        /// 
        /// 删除指定缓存
        /// 
        /// 
        /// 
        public static bool Remove(string key)
        {
            MemcachedClient mc = getInstance();

            return mc.Remove(key);
        }

        /// 
        /// 清空缓存服务器上的缓存
        /// 
        public static void FlushCache()
        {
            MemcachedClient mc = getInstance();

            mc.FlushAll();
        }
    }

}

 


评论


亲,登录后才可以留言!