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