zoukankan      html  css  js  c++  java
  • Memcache简单使用

    1:Memcache的下载https://pan.baidu.com/s/1dFnB3NV/08中

       简单安装:直接点击文件夹中的memcached.exe文件即可.但是每次使用都需要双击,比较麻烦.所以也可以安装到Windows服务中

    2:将Memcache安装到Windows服务中

      2.1 以管理员身份启动命令窗口

          命令: F:

           cd F:memcache

          memcached -d install

          memcached -d start

    3:测试

      通过cmd进入命令提示符窗口后

      telnet 127.0.0.1 11211

        stats

     4 C#操作Memcache所需要的dll文件也在百度云盘中的08文件夹下MemcacheDoNetLibraryAll中

      4.1 创建一个控制条应用程序,并引用文件

      将文件添加到项目的lib文件夹下,添加引用 Commons.dll,ICSharpCode.SharpZipLib.dll,log4net.dll,Memcached.ClientLibrary.dll 

    using Memcached.ClientLibrary;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MemcacheTest
    {
        class Program
        {
             [STAThread]
            static void Main(string[] args)
            {
                 //指定Memcache的IP地址和端口号
                 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();
     
                 // 获得客户端实例--根据IP地址寻找服务器(哈希算法取余数)
                 //负责连接服务端
                 MemcachedClient mc = new MemcachedClient();
                 mc.EnableCompression = false;
     
                 Console.WriteLine("------------测  试-----------");
                //向Memcache服务端存数据,键值对
                 mc.Set("test", "my value");  //存储数据到缓存服务器,这里将字符串"my value"缓存,key 是"test"
                //判断键值对中是否有值
                 if (mc.KeyExists("test"))   //测试缓存存在key为test的项目
                 {
                     Console.WriteLine("test is Exists");
                     Console.WriteLine(mc.Get("test").ToString());  //在缓存中获取key为test的项目
                 }
                 else
                 {
                     Console.WriteLine("test not Exists");
                 }
     
                 Console.ReadLine();
     
                 mc.Delete("test");  //移除缓存中key为test的项目
     
                 if (mc.KeyExists("test"))
                 {
                     Console.WriteLine("test is Exists");
                     Console.WriteLine(mc.Get("test").ToString());
                 }
                 else
                 {
                     Console.WriteLine("test not Exists");
                 }
                 Console.ReadLine();
                 
                 SockIOPool.GetInstance().Shutdown();  //关闭池, 关闭sockets
             }
            
        }
    }
    MemcacheTest

    运行结果:

    5:从Windows服务中卸载Memcache

      4.1 以管理员身份启动命令窗口

      4.2 定位到memcached.exe所在的文件夹

        命令: F:

           cd F:memcache

          memcached -d stop

          memcached -d uninstall

  • 相关阅读:
    THINKPHP3.2视频教程
    PHPCMS 学习
    PHPCMS 后台学习
    phpcms 模板学习
    二叉树的创建与遍历(链式存储)
    MySQL基础~~增、删、改、简单查
    队列的基本操作(链队列)
    MySQL基础~~表结构操作
    行编辑程序
    循环链表的基本操作
  • 原文地址:https://www.cnblogs.com/YK2012/p/6638286.html
Copyright © 2011-2022 走看看