Memcached安装和使用

Memcached安装命令

E:\memcached\memcached.exe -d install 安装

E:\memcached\memcached.exe -d uninstall 卸载

E:\memcached\memcached.exe -d start  启动服务

E:\memcached\memcached.exe -d stop 停止服务

要卸载服务之前必须要先停止服务才行

Memcached安装文件使用的是1.4版本的,下载地址:

链接:https://pan.baidu.com/s/1xCjL3FeajQsJfT8vOmsbFw
提取码:eqvv
 

Memcached使用时需要引用nuget包,我这里使用的是

加入nuget包后会对应引入程序集

 

在此处封装一个Memcached的使用类

using Enyim.Caching;
using Enyim.Caching.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyTest.Cache
{
    public class MemcachedHelper
    {
        private static MemcachedClient mc = null;
        static MemcachedHelper()
        {
            if (mc == null)
            {
                mc = new MemcachedClient();
            }
        }

        /// <summary>
        /// 设置缓存 time过期时间 IsSerialize =1需要序列话,=0 不需要序列化
        /// </summary>
        public static bool Set(string key, object value, TimeSpan time,int IsSerialize=0)
        {
            if (IsSerialize == 1)
            {
                value = JsonConvert.SerializeObject(value);
            }
            return mc.Store(Enyim.Caching.Memcached.StoreMode.Set, key, value, time);
        }

        //设置缓存 time过期时间 IsSerialize =1需要序列话,=0 不需要序列化
        public static bool Set(string key, object value, DateTime time, int IsSerialize = 0)
        {
            if (IsSerialize == 1)
            {
                value = JsonConvert.SerializeObject(value);
            }
            TimeSpan timeexpire = time - DateTime.Now;
            return mc.Store(Enyim.Caching.Memcached.StoreMode.Set, key, value, timeexpire);
        }


        /// <summary>
        /// 获取缓存,返回object类型 ,没有进行反序列化
        /// </summary>
        public static object Get(string key)
        {
            return mc.Get(key);
        }


        /// <summary>
        /// 获取缓存,进行了反序列化
        /// </summary>
        public static T Get<T>(string key)
        {
            object obj = mc.Get(key);
            if (obj==null)
            {
                return default(T);
            }
            return JsonConvert.DeserializeObject<T>(mc.Get(key).ToString());
        }

        //清除缓存
        public static bool Remove(string key)
        {
            return mc.Remove(key);
        }


    }
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值