参考资料链接:
https://www.bbsmax.com/A/pRdBaBbD5n/
https://github.com/enyim/EnyimMemcached/wiki/MemcachedClient-Usage
https://github.com/enyim/EnyimMemcached/wiki/MemcachedClient-Configuration
Memcached 可视化界面 TreeSoft TreeNMS 。由于他是使用java语言写的,所以当我们存入C#的值进去,会被序列化成对象,无法查看他的值。
1、Redis和Memcached的配置不能同时存在app.config中 否者会报错,找不到 RedisConfig
2、App.config
3、 使用Nuget 添加相关的包:EnyimMemcached
MemcachedHelper:
using Enyim.Caching;
using System;
using System.Collections.Generic;
using System.Linq;
using Enyim.Caching.Memcached;
using System.Text;
namespace TestMemcached
{
///
/// MemcachedClient 帮组类 对外提供接口方法
///
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)
{
try
{
MemClient = new MemcachedClient();//app.config中存在默认配置部分 ,所以不需要去找IP和Port
// MemClient = new MemcachedClient("enyim.com/memcached");
}
catch (Exception ex)
{
throw ex;
}
}
}
}
return MemClient;
}
///
/// 删除指定缓存
///
///
///
public static bool Remove(string key)
{
MemcachedClient mc = getInstance();
return mc.Remove(key);
}
///
/// 清空缓存服务器上的缓存
///
public static void FlushCache()
{
MemcachedClient mc = getInstance();
mc.FlushAll();
}
#region 存取值
///
/// 获取缓存值
///
///
///
public static object Get(string key)
{
if (key == null || key.Trim() == "") return null;
MemcachedClient mc = getInstance();
return mc.Get(key);
}
public static bool Set(string key, object value)
{
if (key == null || key.Trim() == "") return false;
MemcachedClient mc = getInstance();
var data = mc.Get(key);
if (data == null)
return mc.Store(StoreMode.Add, key, value);
else
return mc.Store(StoreMode.Replace, key, value);
}
public static bool Set(string key, T value)
{
if (key == null || key.Trim() == "") return false;
MemcachedClient mc = getInstance();
var data = mc.Get(key);
if (data == null)
//如果返回false 可以查看我的另外一篇文章 专门讲解了这个错误 坑
return mc.Store(StoreMode.Add, key, value, new TimeSpan(0, 5, 0));
else
return mc.Store(StoreMode.Replace, key, value);
}
public static bool Set(string key, T value,TimeSpan timeSpan)
{
if (key == null || key.Trim() == "") return false;
MemcachedClient mc = getInstance();
var data = mc.Get(key);
if (data == null)
return mc.Store(StoreMode.Add, key, value, timeSpan);
else
return mc.Store(StoreMode.Replace, key, value);
}
///
/// 通过key 来得到一个对象(前类型)
///
/// 类型
public static T Get(string key)
{
MemcachedClient mc = getInstance();
return mc.Get(key);
}
#endregion
}
}
3、在 Programma中获取值。我测试了多种数据类型的存取。供参考
namespace TestMemcached
{
class Program
{
static void Main(string[] args)
{
// 程序调用 groupName用来区分不同的服务或应用场景
// MemcachedHelper.SetValue( "1","sun", "ting", DateTime.Now.AddMinutes(5));
//var restr = MemcachedHelper.GetValue("1", "sun");
// var obj = MemcachedHelper.GetValue(groupName, "p001");
// MemcachedHelper.Set( "1","sun");
var restr = MemcachedHelper.Get("1");
// User u = new User();
// u.name = "sun";
// u.sex = "gril";
//string str = Newtonsoft.Json.JsonConvert.SerializeObject(u);
// bool isok= MemcachedHelper.Set("user",str);
// string restr = MemcachedHelper.Get("user").ToString();
// User user =Newtonsoft.Json.JsonConvert.DeserializeObject(restr);
// Console.WriteLine(user);
List list = new List();
list.Add("as1");
list.Add("as2");
list.Add("as3");
//List list = new List();
//list.Add(1);
//list.Add(1);
//list.Add(1);
//list.Add(1);
//bool isok = MemcachedHelper.Set>("list", list);
//List list2 = MemcachedHelper.Get>("list");
//Console.WriteLine(list2);
//var s = MemcachedHelper.Get("list");
//List list2 = new List(s.ToString().Split(','));
//Console.WriteLine(list2);
//User u = new User();
//u.name = "sun";
//u.sex = "gril";
//bool isok = MemcachedHelper.Set("user",u);
//User us= MemcachedHelper.Get("user");
//Console.WriteLine(us);
//float[] a = new float[] { 2,1,4};
//bool isok2= MemcachedHelper.Set("a",a);
//var aa= MemcachedHelper.Get("a");
//Console.WriteLine(aa);
User u = new User();
u.name = "sun";
u.sex = "gril";
List users = new List();
users.Add(u);
users.Add(u);
//bool isok3 = MemcachedHelper.Set>("users", users);
//List list3 = MemcachedHelper.Get>("users");
//Console.WriteLine(list3);
bool isok3 = MemcachedHelper.Set("users", users);
List list3 = MemcachedHelper.Get>("users");
bool isok4 = MemcachedHelper.Set("qw", 1234434);
bool isok5= MemcachedHelper.Set("as4", "sun4");
Console.WriteLine(list3);
}
}
[Serializable]
class User
{
public string name;
public string sex;
}