memcached、php-memcache安装及使用

安装phpize命令:yum install php-devel

 

安装libevent

https://cloud.github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz

tar xzvf libevent-2.0.21-stable.tar.gz

cd libevent-2.0.21-stable

./configure

make && make install

 

安装memcached

http://www.memcached.org/files/memcached-1.4.20.tar.gz

tar xzvf memcached-1.4.20.tar.gz

cd memcached-1.4.20

./configure --perfix=/data2/memcached

make && make install

 

安装php-memcache扩展

http://pecl.php.net/package/memcache/download/

tar xzvf memcache-2.2.7.tgz

cd memcache

cd memcache-2.2.7

yum install php-devel

phpize

./configure --enable-memcache --with-php-config=/usr/bin/php-config --with-zlib-dir

make && make install

 

php配置文件中(默认在/etc/php.ini)添加一句

extension=memcache.so

 

保存退出,重启apache即可

可通过phpinfo()函数查询php是否支持memcache

 

 

Memcached启动和结束

/data2/memcached/bin/memcached -d -m 1024 -u root -p 11211 -c 256 -P /tmp/memcached.pid

-d 选项是启动一个守护进程, 

-m 是分配给Memcache使用的内存数量,单位是MB,这里是10MB

-u 是运行Memcache的用户,这里是root

-l 是监听的服务器IP地址,如果有多个地址的话,这里指定了服务器的IP地址192.168.0.122 

-p 是设置Memcache监听的端口,这里设置了12000,最好是1024以上的端口

-c 选项是最大运行的并发连接数,默认是1024,这里设置了256,按照你服务器的负载量来设定

-P 是设置保存Memcachepid文件

kill `cat /tmp/memcached.pid`

 

连接到memcached

telnet 127.0.0.1 11211

 

基本 memcached 客户机命令

您将使用五种基本 memcached 命令执行最简单的操作。这些命令和操作包括:

 

set

add

replace

get

delete

 

格式:

command <key> <flags> <expiration time> <bytes>

<value>

 memcached 修改命令参数

参数 用法

key key 用于查找缓存值

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

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

bytes 在缓存中存储的字节点

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

 

 

set 

set 命令用于向缓存添加新的键值对。如果键已经存在,则之前的值将被替换。

 

注意以下交互,它使用了 set 命令:

 

set userId 0 0 5

12345

STORED

 

 

如果使用 set 命令正确设定了键值对,服务器将使用单词 STORED 进行响应。本示例向缓存中添加了一个键值对,其键为 userId,其值为 12345。并将过期时间设置为 0,这将向 memcached 通知您希望将此值存储在缓存中直到删除它为止。

 

add 

仅当缓存中不存在键时,add 命令才会向缓存中添加一个键值对。如果缓存中已经存在键,则之前的值将仍然保持相同,并且您将获得响应 NOT_STORED

 

下面是使用 add 命令的标准交互:

 

set userId 0 0 5

12345

STORED

 

add userId 0 0 5

55555

NOT_STORED

 

add companyId 0 0 3

564

STORED

 

replace 

仅当键已经存在时,replace 命令才会替换缓存中的键。如果缓存中不存在键,那么您将从 memcached 服务器接受到一条 NOT_STORED 响应。

 

下面是使用 replace 命令的标准交互:

 

replace accountId 0 0 5

67890

NOT_STORED

 

set accountId 0 0 5

67890

STORED

 

replace accountId 0 0 5

55555

STORED

 

 

最后两个基本命令是 get 和 delete。这些命令相当容易理解,并且使用了类似的语法,如下所示:

 

command <key>

 

 

接下来看这些命令的应用。

 

get 

get 命令用于检索与之前添加的键值对相关的值。您将使用 get 执行大多数检索操作。

 

下面是使用 get 命令的典型交互:

 

set userId 0 0 5

12345

STORED

 

get userId

VALUE userId 0 5

12345

END

 

get bob

END

 

delete 

最后一个基本命令是 deletedelete 命令用于删除 memcached 中的任何现有值。您将使用一个键调用 delete,如果该键存在于缓存中,则删除该值。如果不存在,则返回一条 NOT_FOUND 消息。

 

下面是使用 delete 命令的客户机服务器交互:

 

set userId 0 0 5

98765

STORED

 

delete bob

NOT_FOUND

 

delete userId

DELETED

 

get userId

END

 

 

高级 memcached 客户机命令

 

可以在 memcached 中使用的两个高级命令是 gets 和 casgets 和 cas 命令需要结合使用。您将使用这两个命令来确保不会将现有的名称/值对设置为新值(如果该值已经更新过)。我们来分别看看这些命令。

 

gets 

gets 命令的功能类似于基本的 get 命令。两个命令之间的差异在于,gets 返回的信息稍微多一些:64 位的整型值非常像名称/值对的 “版本” 标识符。

 

下面是使用 gets 命令的客户机服务器交互:

 

set userId 0 0 5

12345

STORED

 

get userId

VALUE userId 0 5

12345

END

 

gets userId

VALUE userId 0 5 4

12345

END

 

缓存管理命令

 

最后两个 memcached 命令用于监控和清理 memcached 实例。它们是 stats 和 flush_all 命令。

 

stats 

stats 命令的功能正如其名:转储所连接的 memcached 实例的当前统计数据。在下例中,执行 stats 命令显示了关于当前 memcached 实例的信息:

 

stats

STAT pid 63

STAT uptime 101758

STAT time 1248643186

STAT version 1.4.11

STAT pointer_size 32

STAT rusage_user 1.177192

STAT rusage_system 2.365370

STAT curr_items 2

STAT total_items 8

STAT bytes 119

STAT curr_connections 6

STAT total_connections 7

STAT connection_structures 7

STAT cmd_get 12

STAT cmd_set 12

STAT get_hits 12

STAT get_misses 0

STAT evictions 0

STAT bytes_read 471

STAT bytes_written 535

STAT limit_maxbytes 67108864

STAT threads 4

END

 

flush_all 

flush_all 是最后一个要介绍的命令。这个最简单的命令仅用于清理缓存中的所有名称/值对。如果您需要将缓存重置到干净的状态,则 flush_all 能提供很大的用处。下面是一个使用 flush_all 的例子:

 

set userId 0 0 5

55555

STORED

 

get userId

VALUE userId 0 5

55555

END

 

flush_all

OK

 

get userId

END

 

 

缓存性能

 

在本文的最后,我将讨论如何使用高级 memcached 命令来确定缓存的性能。stats 命令用于调优缓存的使用。需要注意的两个最重要的统计数据是 et_hits 和 get_misses。这两个值分别指示找到名称/值对的次数(get_hits)和未找到名称/值对的次数(get_misses)。

 

结合这些值,我们可以确定缓存的利用率如何。初次启动缓存时,可以看到 get_misses 会自然地增加,但在经过一定的使用量之后,这些 get_misses 值应该会逐渐趋于平稳 — 这表示缓存主要用于常见的读取操作。如果您看到 get_misses 继续快速增加,而 get_hits 逐渐趋于平稳,则需要确定一下所缓存的内容是什么。您可能缓存了错误的内容。

 

计算缓存命中率

 

stats

STAT pid 6825

STAT uptime 540692

STAT time 1249252262

STAT version 1.2.6

STAT pointer_size 32

STAT rusage_user 0.056003

STAT rusage_system 0.180011

STAT curr_items 595

STAT total_items 961

STAT bytes 4587415

STAT curr_connections 3

STAT total_connections 22

STAT connection_structures 4

STAT cmd_get 2688

STAT cmd_set 961

STAT get_hits 1908

STAT get_misses 780

STAT evictions 0

STAT bytes_read 5770762

STAT bytes_written 7421373

STAT limit_maxbytes 536870912

STAT threads 1

END 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值