memcached简析

一. memcached简介

     memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in      speeding up dynamic web applications by alleviating database load.

     (由 LiveJournal旗下的Danga Interactive研发)

        #高性能,分布式缓存系统

    

     缓存服务器:

         缓存:cache,无持久存储功能;

         bypass缓存 # 旁挂式缓存

         k/v cache,仅支持存储流式化数据;

        

     特性:

         k/v cache:仅可序列化数据;存储项:k/v (key:value)

         智能性一半依赖于客户端(调用memcached的API开发程序),一半依赖于服务端;

         分布式缓存:互不通信的分布式集群;

         分布式系统请求路由方法:取模法(对机器数量取模来决定发往哪台机器),

                        一致性哈希算法(顺时针哈希环);

         算法复杂度:O(1)

         清理过期缓存项:

             缓存耗尽:LRU

             缓存项过期:惰性清理机制

                #即会定期检查缓存,当缓存失效时候并不做清理,而是有新缓存写入时

                候,直接覆盖原有的旧缓存内容

二. 安装配置:

             由CentOS 7 base仓库直接提供:

             监听的端口:

                 11211/tcp, 11211/udp

        

             主程序:/usr/bin/memcached

             配置文件:/etc/sysconfig/memcached

            wKioL1gUkfPg_QObAAA9N3RpIOo837.png-wh_50

             Unit File:memcached.service

             管理工具:memcached-tool

             协议格式:memcached协议

                 文本格式

                 二进制格式


三.memcache 命令详解

        详细协议帮助 :  /usr/share/doc/memcached-1.4.15/protocol.txt

         命令:

             统计类:stats, stats items, stats slabs, stats sizes

             存储类:set, add, replace, append, prepend

             命令格式:<command name> <key> <flags> <exptime> <bytes>

                            额外信息  过期时间  大小

                   <cas unique>        #回车后才能输入值

    

                key    key 用于查找缓存值

                flags 可以包括键值对的整型参数,客户机使用它存储关于键值对的额外信息

                   # 默认用1 即可

                expiration time 在缓存中保存键值对的时间长度(以秒为单位,0 表示永

                远)

                bytes 在缓存中存储的字节点

                value 存储的值(始终位于第二行)

    

             检索类:get, delete, incr/decr

             清空:flush_all

        

         示例:

             telnet> add KEY <flags> <expiretime> <bytes> \r

             telnet> VALUE参数 用法

        

                 wKiom1gUkfHBKxIaAAAPuoGTe3o781.png-wh_50

         memcached程序的常用选项:

             -m <num>:Use <num> MB memory max to use for object storage; the

                default is 64 megabytes.

             -c <num>:Use <num> max simultaneous connections; the default is 1024.

             -u <username>:以指定的用户身份来运行进程;

             -l <ip_addr>:监听的IP地址,默认为本机所有地址;

             -p <num>:监听的TCP端口, the default is port 11211.

             -U <num>:Listen on UDP port <num>, the default is port 11211, 0 is off.

            wKioL1gUkfLRK0NEAAEYqJnIHBY380.png-wh_50

             -M:内存耗尽时,不执行LRU清理缓存,而是拒绝存入新的缓存项,直到有多余的空

                间可用时为止;

             -f <factor>:增长因子(内存分块的大小增长倍数);默认是1.25;

             -t <threads>:启动的用于响应用户请求的线程数;

            

             memcached默认没有认证机制,可借用于SASL进行认证;

             SASL:Simple Authentication Secure Layer

    

 三.   API:

     php-pecl-memcache

     php-pecl-memcached

     python-memcached

     libmemcached

    #此工具包中包含多个memcache的命令行管理工具

     libmemcached-devel

    

     命令行工具:

     memcached-tool  SERVER:PORT  COMMAND

        ————————————————-

        [root@localhost ~]# rpm -ql libmemcached

        /usr/bin/memaslap

        /usr/bin/memcapable

        /usr/bin/memcat

        /usr/bin/memcp

        /usr/bin/memdump

        /usr/bin/memerror

        /usr/bin/memexist

        /usr/bin/memflush

        /usr/bin/memparse

        /usr/bin/memping

        /usr/bin/memrm

        /usr/bin/memslap

        /usr/bin/memstat

        /usr/bin/memtouch

        ————————————————–

    


四. 利用 telnet 管理memcache 示例

        1. yum install telnet

        –> telnet localhost 11211

        stats:

        

          

              wKiom1gUkfKh9dgRAAA-6iEV5Kc557.png-wh_50


五. nginx 对memcache 的支持

        所需模块:  ngx_http_memcached_module

            Syntax: memcached_bind address [transparent] | off;

            Default: —

            Context: http, server, location

                #用于绑定 memcache 服务器

        

            Syntax: memcached_pass address;

            Default: —

            Context: location, if in location

                #绑定memcache 集群

            eg:

            server {    

            location / {        

                set $memcached_key "$uri?$args";

                    #将指定的key 放入memcache 生成缓存;

                    memcached_pass host:11211;

                    error_page     404 502 504 = @fallback;

                    }    

        

                location @fallback {        

                    proxy_pass  http://backend;    

                    }

             }

             ============================================================

原创文章,作者:ldt195175108,如若转载,请注明出处:http://www.178linux.com/55611

(0)
ldt195175108ldt195175108
上一篇 2016-10-29 20:01
下一篇 2016-10-29 20:29

相关推荐

  • 五大主流数据库模型

    导读:无论是关系型数据库还是非关系型数据库,都是某种数据模型的实现。本文将为大家简要介绍5种常见的数据模型,让我们来追本溯源,窥探现在流行的数据库解决方案背后的神秘世界。 什么是数据模型? 访问数据库中的数据取决于数据库实现的数据模型。数据模型会影响客户端通过API对数据的操作。不同的数据模型可能会提供或多或少的功能。一般而言,数据模型不会直接提供过多的功能…

    Linux干货 2015-04-04
  • 22期第十三周课堂练习

    1、建立samba共享,共享目录为/data,要求:(描述完整的过程)   1)共享名为shared,工作组为magedu;   2)添加组develop,添加用户gentoo,centos和ubuntu,其中gentoo和centos以develop为附加组,ubuntu不属于develop组;密码均为用户名;   3)添加s…

    Linux干货 2017-01-09
  • 在CentOS 7上实现私有CA及申请和吊销证书

    – 创建私有CA openssl的配置文件:/etc/pki/tls/openssl.cnf 42 dir     = /etc/pki/CA       # Where everythi…

    Linux干货 2016-12-01
  • Shell中的循环语句

    在编程语言中,循环语句是最基本的语法之一,在Shell(这里是Bash)中也不例外。把相关内容整理一下吧。 这里包括for/while/until循环,以及变量自增的语法实例。 Shell(以Bash为例)中的循环语句一般有for、while、until这几种,偶尔还有写错语法的时候,这里结合实例来自己总结一下。也为今后使用提供一个快捷的资料获取渠道。 一、…

    Linux干货 2016-08-22
  • 利用varnish构建httpd缓存服务器

    varnish如何存储缓存对象:     file: 单个文件;不支持持久机制;     malloc: 缓存在内存中;     persistent:基于文件的持久存储;(此方式不建议使用) vcl:配置缓存系统的缓存机制;【线程中缓存功…

    Linux干货 2016-06-20
  • Linux磁盘与文件系统管理

    对于一块磁盘,如果我们想要使用的话,需要对磁盘进行分区并建立文件系统。下面,我们就了解一下Linux中的磁盘与文件系统管理 一、MBR 硬盘的0扇区为MBR扇区,其由以下几部分组成: l  主引导程序(偏移地址0000H–0088H),它负责从活动分区中装载,并运行系统引导程序。 l  出错信息数据区,偏移地址0089H&#8…

    Linux干货 2016-08-28