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