下载文件:https://sourceforge.net/projects/memcacheddotnet/
将Commons.dll,ICSharpCode.SharpZipLib.dll,log4net.dll,Memcached.ClientLibrary.dll 等放到bin目录
引用Memcached.ClientLibrary.dll
控制台操作:
安装好后,直接运行CMD,然后输入telnet 127.0.0.1 11211,进入后输入 stats,查看memcached的状态,如下图所示
(添加数据)操作:add key1 0 0 5//参数解释:key1是(键)的名称。后面的0是(标志位),默认0即可。再后面是(过期时间),0是永不过期,如果设置3则是3秒过期。最后一个参数是规定(长度)。
输入完add key1 0 0 5后回车,然后添加数据(注意规定长度),回车即可保存
重复添加问题:
(获取数据):get key1
(删除数据):delete key1
(修改数据):replace key1 0 0 4 回车修改成的数据
注:add仅仅是添加纪录 set是添加删除记录
程序实例:
using Memcached.ClientLibrary; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Memcached随练 { class Program { static void Main(string[] args) { //分布Memcachedf服务IP 端口 string[] servers = { "127.0.0.1:11211", "192.168.202.128:11211" }; //初始化池 SockIOPool pool = SockIOPool.GetInstance(); pool.SetServers(servers); pool.InitConnections = 3; pool.MinConnections = 3; pool.MaxConnections = 5; pool.SocketConnectTimeout = 1000; pool.SocketTimeout = 3000; pool.MaintenanceSleep = 30; pool.Failover = true; pool.Nagle = false; pool.Initialize(); //客户端实例 MemcachedClient mc = new Memcached.ClientLibrary.MemcachedClient(); mc.EnableCompression = false; mc.Add("aaa", "shit");//添加一个值 mc.Add("bbb", "hello", DateTime.Now.AddSeconds(10));//设置过期时间 mc.Delete("aaa");//删除指定值 mc.Get("aaa"); Console.WriteLine(mc.Get("bbb")); } } }