今天没事研究一下,谁想到遇到了几个dll找不到,网上也不好找到,索性功夫不负有心人。贴出代码和相关的dll
Memcached代码:(网上都是的,很多人都保存了这个代码)
using Memcached.ClientLibrary; using System; namespace Framework.MemCached { public class MemCacheHelper { private static readonly MemcachedClient mc = null; static MemCacheHelper() { //最好放在配置文件中 string[] serverlist = { "127.0.0.1:11211", "10.0.0.132:11211" }; //初始化池 SockIOPool pool = SockIOPool.GetInstance(); pool.SetServers(serverlist); 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(); // 获得客户端实例 mc = new MemcachedClient(); mc.EnableCompression = false; } /// <summary> /// 存储数据 /// </summary> /// <param name="key">键</param> /// <param name="value">值</param> /// <returns>执行结果</returns> public static bool Set(string key, object value) { return mc.Set(key, value); } /// <summary> /// 存储数据 /// </summary> /// <param name="key">键</param> /// <param name="value">值</param> /// <param name="time">过期日期</param> /// <returns>执行结果</returns> public static bool Set(string key, object value, DateTime time) { return mc.Set(key, value, time); } /// <summary> /// 获取数据 /// </summary> /// <param name="key">键</param> /// <returns>缓存数据</returns> public static object Get(string key) { return mc.Get(key); } /// <summary> /// 删除 /// </summary> /// <param name="key">键</param> /// <returns>true:删除成功;false:删除失败.</returns> public static bool Delete(string key) { if (mc.KeyExists(key)) { return mc.Delete(key); } return false; } } }
测试代码:(很简单,随便写的)
protected void Button1_Click(object sender, EventArgs e) { //add cache bool result = Framework.MemCached.MemCacheHelper.Set("user", "jay"); Response.Write(result); } protected void Button2_Click(object sender, EventArgs e) { //get cache object r = Framework.MemCached.MemCacheHelper.Get("user"); if (r != null) { Response.Write(r.ToString()); } else { Response.Write("error"); } } protected void Button3_Click(object sender, EventArgs e) { //delete cache bool r = Framework.MemCached.MemCacheHelper.Delete("user"); Response.Write(r); }
主要是找到对应版本的dll不好找,网上很多代码,但几乎都没有附带资源 百度网盘:https://pan.baidu.com/s/1-1kMXxIc-Hsv5neUIV5jgw
研究过程:
1.下载:http://static.runoob.com/download/memcached-1.4.5-amd64.zip 解压。
2.使用管理员身份执行以下命令将 memcached 添加来任务计划表中:
cmd.exe的位置:C:\Windows\System32
右键》以管理员身份运行(不然会报错:拒绝访问):
安装memcached 在命令行里输入:schtasks /create /sc onstart /tn memcached /tr "'G:\memcached-amd64\memcached.exe' -m 512"
注意:你需要使用你本机的路径替代 G:\memcached-amd64\memcached.exe。
注意:-m 512 意思是设置 memcached 最大的缓存配置为512M。
注意:我们可以通过使用 "G:\memcached-amd64\memcached.exe -h" 命令查看更多的参数配置,不过都是英文,翻译吧。
3.安装完成后,启动服务:G:\memcached-amd64\memcached.exe -d start
4.如果需要删除 memcached 的任务计划可以执行以下命令:schtasks /delete /tn memcached
5.一切就绪后,开始进行测试,发现仍然没法添加缓存,cmd里执行这个:
sc create "Memcached11211" binPath= "G:\memcached-amd64\memcached.exe -d runservice -p 11211" DisplayName= "Memcached11211" start= auto
具体是为什么,没查到呢还。应该是需要创建一个能自动启动的windows系统服务:https://blog.csdn.net/lsj19830812/article/details/6187233
我是这里看到的:https://blog.csdn.net/swjtu_yhz/article/details/60132572
我查过的文章:
http://www.runoob.com/memcached/window-install-memcached.html
http://www.cnblogs.com/caokai520/p/4390646.html
http://www.cnblogs.com/liangwenchao-912/p/5529000.html
https://www.cnblogs.com/minily/p/7456322.html
https://blog.csdn.net/sweetlei/article/details/78719822