zoukankan      html  css  js  c++  java
  • 试用memcached

    Memcached是一个key-value的内存数据库,提供快速缓存数据的功能,接口简单易用。

    系统环境ubuntu 64位,kernel 3.8.0-30,首先安装memcached。

    1 sudo apt-get install memcached

    memcached的版本是1.4.14。然后启动,全部默认参数

    1 memcached start

    其实,memcached的参数还是挺多的,可以-h查看,挑几个常用的列举一下

    -p <num>      TCP port number to listen on (default: 11211)
    -U <num>      UDP port number to listen on (default: 11211, 0 is off)
    -s <file>     UNIX socket path to listen on (disables network support)
    -a <mask>     access mask for UNIX socket, in octal (default: 0700)
    -l <addr>     interface to listen on (default: INADDR_ANY, all addresses)
                  <addr> may be specified as host:port. If you don't specify
                  a port number, the value you specified with -p or -U is
                  used. You may specify multiple addresses separated by comma
                  or by using -l multiple times
    -d            run as a daemon
    -r            maximize core file limit
    -u <username> assume identity of <username> (only when run as root)
    -m <num>      max memory to use for items in megabytes (default: 64 MB)
    -M            return error on memory exhausted (rather than removing items)
    -c <num>      max simultaneous connections (default: 1024)
    -v            verbose (print errors/warnings while in event loop)
    -h            print this help and exit
    -f <factor>   chunk size growth factor (default: 1.25)
    -n <bytes>    minimum space allocated for key+value+flags (default: 48)
    -L            Try to use large memory pages (if available). Increasing
                  the memory page size could reduce the number of TLB misses
                  and improve the performance. In order to get large pages
                  from the OS, memcached will allocate the total item-cache
                  in one large chunk.
    -t <num>      number of threads to use (default: 4)
    -R            Maximum number of requests per event, limits the number of
                  requests process for a given connection to prevent 
                  starvation (default: 20)
    -C            Disable use of CAS

    然后用telnet就能用了

    telnet localhost 11211
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    set hello 0 0 3
    123
    STORED
    get hello
    VALUE hello 0 3
    123
    END
    incr hello 1
    124

    很明显,memcached把hello这个变量当做数值类别来用了,不过似乎并不完全是数值类别,如下

    append hello 0 0 1
    a
    STORED
    get hello
    VALUE hello 0 4
    124a
    END2
    incr hello 1
    CLIENT_ERROR cannot increment or decrement non-numeric value

    在hello后面append了一个字符串,依然能够work,但是再次incr就不行了,说明memcached并不苛求存储value的类型。在使用set的时候,后面跟着一些数字,这些都是表示key的设定的。

    set hello <flag> <expires> <byte>
    get hello
    VALUE 123 <flag> <byte>
    flag: 0-不压缩,1-压缩
    expires: key在多久之后删除,以秒为单位,0是不删除
    byte: value的字节数

    get的时候可以获得key的flag和byte设定。

    当然,如果这样的一个工具只有telnet这样的接口肯定是不行的,试用一下ruby的接口

     1 require "rubygems"
     2 require "memcache"
     3 
     4 server = ['localhost:11211']
     5 option = {}
     6 
     7 myCache = MemCache.new(server, option)
     8 
     9 # save data
    10 myCache['key1'] = 11111
    11 myCache['key2'] = "aaaaa"
    12 myCache['key3'] = %w[abc 123abc]
    13 myCache['key4'] = {:foo => 1, :bar => 'a'}
    14 
    15 # get data
    16 puts (myCache['key1'])
    17 puts (myCache['key2'])
    18 puts (myCache['key3'])
    19 puts (myCache['key4'])

     以下是输出,

    11111
    aaaaa
    abc
    123abc
    {:foo=>1, :bar=>"a"}

    看起来似乎没什么特别的地方,去memcached上看一下

    telnet localhost 11211
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    get key1
    VALUE key1 0 6
    ig+
    END
    get key2
    VALUE key2 0 15
    I"
    aaaaa:EF
    END
    get key3
    VALUE key3 0 28
    [Iabc:EFI"
                123abc;F
    END
    get key4
    VALUE key4 0 25
    {fooibarI"a:EF
    END

    显示中夹杂着很多乱码和不可见ASCII字符,明显这是ruby的memcached-client序列化之后的结果。

  • 相关阅读:
    shell特殊符号cut命令 sort_wc_uniq命令 tee_tr_split命令 shell特殊符号
    管道符和作业控制 shell变量 环境变量配置文件
    8.1 shell介绍 8.2 命令历史 8.3 命令补全和别名 8.4 通配符 8.5 输入输出重定向 
    yum更换国内源 yum下载rpm包 源码包安装
    mysql Communication link failure, message from server: "Can't get hostname for your address"
    7.1 安装软件包的三种方法 7.2 rpm包介绍 7.3 rpm工具用法 7.4 yum工具用法 7.5 yum搭建本地仓库
    java 对象数组
    zip压缩工具 tar打包 打包并压缩
    java链接mysql 中文乱码
    压缩打包介绍/gzip压缩工具/bzip2压缩工具/xz压缩工具
  • 原文地址:https://www.cnblogs.com/valleylord/p/3500662.html
Copyright © 2011-2022 走看看