• (七)7-3memcached安装和集群操作


    Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached基于一个存储键/值的hashmap。其守护进程(daemon)是用C语言写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。

    1、官网http://memcached.org/下载最新版本memcached(memcached-1.5.3.tar.gz)
    安装之前需要libevent 官网http://libevent.org/下载libevent-2.0.20-stable.tar.gz

    cd /usr/local/src
    wget https://cloud.github.com/downloads/libevent/libevent/libevent-2.0.20-stable.tar.gz
    tar zxvf  libevent-2.0.20-stable.tar.gz
    cd /usr/local/src/libevent-2.0.20-stable
    ./configure  --prefix=/usr/local/libevent  && make  && make install
    安装memcached
    cd /usr/local/src
    tar zxvf memcached-1.5.3.tar.gz
    cd memcached-1.5.3
    ./configure  --prefix=/usr/local/memcached  --with-libevent=/usr/local/libevent  && make &&  make install

    启动memcache
    mkdir -p /usr/local/memcached/logs
    /usr/local/memcached/bin/memcached -d -m 10 -u root -l 127.0.0.1 -p 11211 -c 256 -P /usr/local/memcached/logs/memcache.pid

    -p 指定端口号(默认11211)  
    -m 指定最大使用内存大小(默认64MB)  
    -t 线程数(默认4)  
    -l 连接的IP地址, 默认是本机  
    -d 以后台守护进程的方式启动
    -c 最大同时连接数,默认是1024
    -P 制定memecache的pid文件
    -h 打印帮助信息
    检查memcache是否启动

    安装 memcache客户端 pip install pymemcache 或者 pip install python-memcached
    使用示例1使用pymemcache:

    import  pymemcache
    from pymemcache.client.base import Client
    mc = Client(('192.168.88.102',11211))
    mc.set("name","cnblogs")
    print mc.get("name")
    运行结果:
    cnblogs
    

     示例2 使用memcache

    import  memcache
    mc = memcache.Client(['192.168.88.102:11211'])
    mc.set("name1","cnblogs1")
    print mc.get("name1")
    运行结果:
    cnblogs1
    

      集群管理

    单机启动多实例:
    /usr/local/memcached/bin/memcached -d -m 10 -u root -l 0.0.0.0 -p 11211 -c 256 -P /usr/local/memcached/logs/memcache1.pid
    /usr/local/memcached/bin/memcached -d -m 10 -u root -l 0.0.0.0 -p 11212 -c 256 -P /usr/local/memcached/logs/memcache2.pid
    示例:

    import  memcache
    mc = memcache.Client([('192.168.88.102:11211',1),('192.168.88.102:11212',2)],debug=True)
    mc.set("k1","v1")
    print mc.get("k1")
    
    
    mc1 = memcache.Client([('192.168.88.102:11211')],debug=True)
    print mc1.get("k1")
    mc2 = memcache.Client([('192.168.88.102:11212')],debug=True)
    print mc2.get("k1")
    

      运行结果:

    v1
    v1
    None
    

      

  • 相关阅读:
    leetcode[164] Maximum Gap
    leetcode[162] Find Peak Element
    leetcode[160] Intersection of Two Linked Lists
    leetcode[156] binary tree upside down
    leetcode[155] Min Stack
    leetcode Find Minimum in Rotated Sorted Array II
    leetcode Find Minimum in Rotated Sorted Array
    leetcode Maximum Product Subarray
    ROP
    windbg bp condition
  • 原文地址:https://www.cnblogs.com/pythonlx/p/7995237.html
Copyright © 2020-2023  润新知