Memcached安装,操作,用C#操作

2020-12-13 16:06

阅读:356

标签:style   blog   http   io   color   ar   os   使用   sp   

 

本文来自:http://li19910722.blog.163.com/blog/static/136856822201406103313163/

1;安装
下载Memcache:http://code.jellycan.com/Memcache/
将服务程序拷贝到一个磁盘上的目录,同时将CMD命令窗口也复制到该文件夹下面
安装服务:cmd→Memcached.exe -d install 打开服务监控窗口可以查看服务是否启动。
或者改为:.\Memcached.exe -d install
启动服务:cmd→Memcached.exe -d start(restart重启,stop关mem闭服务)
检查服务是否启动:连接到Memcache控制台:telnet 127.0.0.1 11211  输入命令:stats检查当前服务状态。
卸载服务:Memcached.exe -d uninstall
遇到问题:
1;win8下安装服务。无法启动此程序,因为计算机中丢失 MSVCR71.dll。尝试重新安装该程序以解决此问题。下载dll地址:http://www.dll-files.com/dllindex/dll-files.shtml?msvcr71
2;关于无法用Telnet:
是这样的,Win7默认没有安装telnet功能,所以你直接用telnet命令是用不了的;
你可以去“控制面板”--》“程序和功能”(在左下角)---》“打开或关闭Windows功能”,勾上“telnet客户端”,确定就可以正常使用telnet命令了

2;

添加一条数据:
add key 0 30 10           --表示失效时间为30s,长度为10 
set  key 0 30 10           --添加一条数据
 
得到一条数据
get key
 
3; 如何使用C#操作Memcached:
    1. 添加引用
    2. 写代码
      MemcachedHelp类:
    3.  public class MemcachedHelper
         {
             ///
             /// 定义一个静态MemcachedClient客户端,它随类一起加载,所有对象共用
             ///
             private static MemcachedClient mc;
             ///
             /// 静态构造函数,初始化Memcached客户端
             ///
             static MemcachedHelper()
             {
                 string[] serverList = { "127.0.0.1:11211" };
                 SockIOPool pool = SockIOPool.GetInstance("test");
                 pool.SetServers(serverList);
                 pool.Initialize();
                 mc = new MemcachedClient();
                 mc.PoolName = "test";
                 mc.EnableCompression = false;
             }
             ///
             /// 向Memcached缓存中添加一条数据
             ///
             ///
             ///
             /// 过期时间
             /// 返回是否添加成功
             public static bool SetValue(string key, object value, DateTime expiry)
             {
                 return mc.Set(key, value, expiry);
             }
             ///
             /// 通过key 来得到一个对象
             ///
             ///
             /// 对象
             public static object GetValue(string key)
             {
                 return mc.Get(key);
             }
         }
      4;测试:
       
      ///
              ///  向Memcached中添加一条数据
              ///
              ///
              ///
              private void btnAddMemCached_Click(object sender, EventArgs e)
              {
                  if (MemcachedHelper.SetValue("name", "licong", DateTime.Now.AddMinutes(1)))
                  {
                      MessageBox.Show("数据添加成功");
                  }
                  object obj = MemcachedHelper.GetValue("name");
                  MessageBox.Show(obj.ToString());
              }

Memcached安装,操作,用C#操作

标签:style   blog   http   io   color   ar   os   使用   sp   

原文地址:http://www.cnblogs.com/zhouyunbaosujina/p/4079536.html

上一篇:python常用内建函数

下一篇:Java 数组


评论


亲,登录后才可以留言!