memcached-tool 127.0.0.1:11211 display
Lazy Expiration
Memcached 内部不会监视记录是否过期,而是在get时查看记录的时间戳,检查记录是否过期。这种技术被称为lazy(惰性)expiration。因此,Memcached不会在过期监视上耗费CPU时间。
LRUMemcached
会优先使用已超时的记录的空间,但即使如此,也会发生追加新记录时空间不足的情况,此时就要使用名为Least Recently Used(LRU)
机制来分配空间。顾名思义,这是删除“最近最少使用”的记录的机制。因此,当内存空间不足时(无法从slab class
获取到新的空间时),就从最近未被使用的记录中搜索,并将其空间分配给新的记录。从缓存的实用角度来看,该模型十分理想。
[root@aming-01 ~]# yum install -y memcached libmemcached libevent
[root@aming-01 ~]# systemctl start memcached
[root@aming-01 ~]# ps aux |grep memcached
memcach+ 1704 0.0 0.1 344080 1672 ? Ssl 21:14 0:00 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024
-u
指定运行memcached服务的用户-p
执行监听端-m
指定内存大小 单位M-c
指定最大并发数vim /etc/sysconfig/memcached
内容如下:
PORT="11211" ##端口
USER="memcached" ##用户
MAXCONN="1024" ##最大并发数
CACHESIZE="64" ##分配内存大小
OPTIONS="" #监听的ip
memcached-tool 127.0.0.1:11211 stats
#127.0.0.1:11211 Field Value
accepting_conns 1
auth_cmds 0
auth_errors 0
bytes 0
bytes_read 7
bytes_written 0
cas_badval 0
cas_hits 0
cas_misses 0
cmd_flush 0
cmd_get 0
cmd_set 0
cmd_touch 0
conn_yields 0
connection_structures 11
curr_connections 10
curr_items 0
decr_hits 0
decr_misses 0
delete_hits 0
delete_misses 0
evicted_unfetched 0
evictions 0
expired_unfetched 0
get_hits 0
get_misses 0
hash_bytes 524288
hash_is_expanding 0
hash_power_level 16
incr_hits 0
incr_misses 0
libevent 2.0.21-stable
limit_maxbytes 67108864
listen_disabled_num 0
pid 1704
pointer_size 64
reclaimed 0
reserved_fds 20
rusage_system 0.021315
rusage_user 0.000000
threads 4
time 1542201963
total_connections 11
total_items 0
touch_hits 0
touch_misses 0
uptime 698
version 1.4.15
yum install -y nc
echo stats |nc 127.0.0.1 11211
STAT pid 1704
STAT uptime 1460
STAT time 1542202725
STAT version 1.4.15
STAT libevent 2.0.21-stable
STAT pointer_size 64
STAT rusage_user 0.000000
STAT rusage_system 0.038224
STAT curr_connections 10
STAT total_connections 12
STAT connection_structures 11
STAT reserved_fds 20
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 0
STAT get_misses 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 13
STAT bytes_written 1024
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evictions 0
STAT reclaimed 0
END
yum install -y libmemcached
memstat --servers=127.0.0.1:11211
Server: 127.0.0.1 (11211)
pid: 1704
uptime: 1712
time: 1542202977
version: 1.4.15
libevent: 2.0.21-stable
pointer_size: 64
rusage_user: 0.000000
rusage_system: 0.042545
curr_connections: 10
total_connections: 13
connection_structures: 11
reserved_fds: 20
cmd_get: 0
cmd_set: 0
cmd_flush: 0
cmd_touch: 0
get_hits: 0
get_misses: 0
delete_misses: 0
delete_hits: 0
incr_misses: 0
incr_hits: 0
decr_misses: 0
decr_hits: 0
cas_misses: 0
cas_hits: 0
cas_badval: 0
touch_hits: 0
touch_misses: 0
auth_cmds: 0
auth_errors: 0
bytes_read: 30
bytes_written: 2069
limit_maxbytes: 67108864
accepting_conns: 1
listen_disabled_num: 0
threads: 4
conn_yields: 0
hash_power_level: 16
hash_bytes: 524288
hash_is_expanding: 0
bytes: 0
curr_items: 0
total_items: 0
expired_unfetched: 0
evicted_unfetched: 0
evictions: 0
reclaimed: 0
1. memcached-tool IP:端口 stats
2. echo stats |nc IP 端口
3. memstat --servers=IP:端口
telnet IP 端口
1. yum install -y telnet
2. telnet 127.0.0.1 11211
3. 退出Telnet按Ctrl+]再输入quit撤销按Ctrl+退格键
nc IP 端口
nc 127.0.0.1 11211
``\r\n
\r\n\
set key3 1 100 4 ## 1为标记 100为存储时间 4为4位数
abcd
STORED
get key3
VALUE key3 1 4
abcd
END
replace key3 1 200 5 ##replace和set一样也要输入
abcdx
STORED
delete key3
DELETED
memcached-tool IP:端口 dump > 备份文件
[root@aming-01 ~]# memcached-tool 127.0.0.1:11211 dump > date.txt
Dumping memcache contents
Number of buckets: 1
Number of items : 2
Dumping bucket 1 - 2 total items
[root@aming-01 ~]# cat date.txt
add shenxinyu 2 1542201265 2
ab
add zhaoyujie 1 1542201265 1
a
[root@aming-01 ~]# date -d @1542201265
2018年 11月 14日 星期三 21:14:25 CST
nc IP 端口 < 备份文件
1. [root@aming-01 ~]# systemctl restart memcached ##无法导入已存在的数据,所以需要重启服务清空数据后再测试
2. [root@aming-01 ~]# nc 127.0.0.1 11211 < date.txt
STORED
STORED
[root@aming-01 ~]# nc 127.0.0.1 11211
get zhaoyujie
VALUE zhaoyujie 1 1
a
END
get shenxinyu
VALUE shenxinyu 2 2
ab
END
[root@aming-01 ~]# cat date.txt
set shenxinyu 2 1542209568 2
bb
set zhaoyujie 1 1542209568 1
b
[root@aming-01 ~]# nc 127.0.0.1 11211 < date.txt
STORED
STORED
[root@aming-01 ~]# nc 127.0.0.1 11211
get zhaoyujie
VALUE zhaoyujie 1 1
b
END
get shenxinyu
VALUE shenxinyu 2 2
bb
END
cd /usr/loca/src
wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz
tar zxvf memcache-2.2.3.tgz
cd memcache-2.2.3
/usr/local/php-fpm/bin/phpize
./configure --with-php-config=/usr/local/php-fpm/bin/php-config
make && make install
安装完后会有类似这样的提示:Installing shared extensions: /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
vim /usr/local/php-fpm/etc/php.ini
增加以下内容:
extension=memcache.so
[root@aming-01 memcache-2.2.3]# /usr/local/php-fpm/bin/php -m|grep memcache
memcache
curl www.apelearn.com/study_v2/.memcache.txt > 1.php 2>/dev/null
也可以参考:https://coding.net/u/aminglinux/p/yuanke_centos7/git/blob/master/21NOSQL/1.php
/usr/local/php-fpm/bin/php 1.php
输出内容如下时表示连接成功:
[root@aming-01 ~]# /usr/local/php-fpm/bin/php 1.php
Get key1 value: This is first value<br>Get key1 value: This is replace value<br>Get key2 value: Array
(
[0] => aaa
[1] => bbb
[2] => ccc
[3] => ddd
)
<br>Get key1 value: <br>Get key2 value: <br>[root@aming-01 ~]#
vim /usr/local/php/etc/php.ini
找到下面参数,并注释掉:
session.save_handler = files
在文件内添加以下内容:
session.save_handler = memcache
session.save_path = "tcp://192.168.157.128:11211"
php_value session.save_handler "memcache"
php_value session.save_path "tcp://192.168.157.128:11211"
php_value[session.save_handler] = memcache
php_value[session.save_path] = " tcp://192.168.157.128:11211 "
wget http://study.lishiming.net/.mem_se.txt
mv .mem_se.txt /data/wwwroot/zhaoyujie.com/test.php
[root@aming-01 ~]# curl localhost/test.php
1542383652<br><br>1542383652<br><br>7dvifjains9h2sic5ge82sd6a5
[root@aming-01 ~]# curl localhost/test.php
1542383654<br><br>1542383654<br><br>hroqsbofe2atctq6vvjegulre2
[root@aming-01 ~]# curl localhost/test.php
1542383654<br><br>1542383654<br><br>m6uvqd4hhkq9k7svq79i48hrh2
[root@aming-01 ~]# memcached-tool 127.0.0.1:11211 stats |grep curr_items
curr_items 3
[root@aming-01 ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get m6uvqd4hhkq9k7svq79i48hrh2
VALUE m6uvqd4hhkq9k7svq79i48hrh2 0 37
TEST|i:1542383654;TEST3|i:1542383654;
END
get hroqsbofe2atctq6vvjegulre2
VALUE hroqsbofe2atctq6vvjegulre2 0 37
TEST|i:1542383654;TEST3|i:1542383654;
END