Memcached (2) command line, data export and import, storage sessions

5. memcached command line

Memcached is similar to mysql, and also supports creating a library, creating a table, inserting a table, viewing table data, etc. in mysql.

1 Log in to Memcached

[root@yt-01 ~]# telnet 127.0.0.1 11211   //测试端口
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
# 进入memcached命令行

stats     关于当前 memcached 实例的信息
STAT pid 13412
STAT uptime 1885
STAT time 1522943511
STAT version 1.4.24
STAT libevent 2.0.21-stable
STAT pointer_size 64
STAT rusage_user 0.017423
STAT rusage_system 0.046463
……


set key2 0 30 2   //手动存储一个数据

其中key2 代表着 key值
0代表着 flags 标记
30代表着 过期时间
2代表着 2个字节
---------------------------------------------
set key2 0 30 2     //存储key2,标记为0,过期时间30s,2个字节的数据
ab                          //输入数据,只能2个字节:ab
STORED 
get key2               //获取key2数据(因为输入慢了,数据过期了)
END
set key2 0 30 2     //重新存
ab
STORED
get key2               //获取成功
VALUE key2 0 2
ab
END

2 Memcached syntax rules

  • <command name> <key> <flags> <exptime> <bytes>\r\n <data block>\r\n
  • Note: \r\n is the Enter key under Windows
  • <command name>Can be set, add, replace
  • set means to <key>store the data according to the corresponding, increase when not, and sometimes overwrite
  • add means to <key>add the data according to the corresponding, but if the data <key>already exists, the operation will fail
  • replace means to <key>replace the data according to the corresponding, but if it <key>does not exist, the operation fails.
  • <key>The key that the client needs to store the data with - <flags>is a 16-bit unsigned integer (represented in decimal). This flag will be stored with the data to be stored and returned when the client gets the data. Clients can use this flag for special purposes, and this flag is opaque to the server.
  • <exptime>for the expiry time. If it is 0, it means that the stored data will never expire (but can be replaced by server algorithm: LRU, etc.). If it is not 0 (unix time or the number of seconds from this time), when it expires, the server can ensure that the user cannot get the data (based on the server time).
  • <bytes><bytes>The number of bytes to store, can be 0 when the user wants to store empty data
  • <data block>The content that needs to be stored, after the input is completed, the client needs to add \r\n (directly click Enter) as the end mark.

3 Memcached data example

set key3 1 100 4         //存取一个4字节值
abcd
STORED
get key3               //查询key3值
VALUE key3 1 4
abcd
END
replace key3 1 200 5         //替换key3的值
abcdx
STORED
get key3                     //查询替换后的值
VALUE key3 1 5       
abcdx
END
delete key3    //删除key3的值
DELETED
get key3
END
quit           //退出
Connection closed by foreign host.

6. memcached data export and import

1 Export

[root@yt-01 ~]# date -d "+1 hour" +%s    //显示当前时间加一个小时时间戳
1522948361
[root@yt-01 ~]# memcached-tool 127.0.0.1:11211 dump >data.txt      //导出
Dumping memcache contents
  Number of buckets: 1
  Number of items : 4
Dumping bucket 1 - 4 total items
[root@zhdy-01 ~]# cat data.txt
add key1 1 1522948361 3
asd
add name 1 1522948361 5
justi
add sex 1 1522948361 3
man
add age 1 1522948361 2
27

2 Import

[root@yt-01 ~]# nc 127.0.0.1 11211 < data.txt
若nc命令不存在,yum install nc

Note: The exported data has a timestamp, which is the time when the data expires. If the current time has exceeded the timestamp, it cannot be imported. If it is just for testing, this can be done, because memcached is cached in memory, as long as the machine is restarted or the memcached service is restarted, the cached data will be lost, and then we can use nc again to import it.

[root@yt-01 ~]# systemctl restart memcached
[root@yt-01 ~]# nc 127.0.0.1 11211 <data.txt
STORED
STORED
STORED
STORED

7. php connect to memcached

1 First install the memcache extension of php

[root@yt-01 ~]# cd /usr/local/src/
[root@yt-01 src]# wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz 
[root@yt-01 src]# tar zxf memcache-2.2.3.tgz
[root@yt-01 src]# cd memcache-2.2.3
[root@yt-01 memcache-2.2.3]# ls
config9.m4 config.w32 example.php memcache_consistent_hash.c memcache_queue.c memcache_session.c php_memcache.h
config.m4 CREDITS memcache.c memcache.dsp memcache_queue.h memcache_standard_hash.c README
[root@yt-01 memcache-2.2.3]# /usr/local/php/bin/phpize     //nginx的php目录是/usr/local/php-fpm/bin/phpize
Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
[root@yt-01 memcache-2.2.3]# ./configure --with-php-config=/usr/local/php/bin/php-config
[root@yt-01 memcache-2.2.3]# echo $?
0
[root@yt-01 src]# make & make install
# 报错
/usr/local/src/memcache-2.2.3/memcache.c: 在函数‘php_mmc_connect'中:
/usr/local/src/memcache-2.2.3/memcache.c:1902: 错误:提供给函数‘zend_list_insert'的实参太少
/usr/local/src/memcache-2.2.3/memcache.c:1919: 错误:提供给函数‘zend_list_insert'的实参太少
/usr/local/src/memcache-2.2.3/memcache.c: 在函数‘zif_memcache_add_server'中:
/usr/local/src/memcache-2.2.3/memcache.c:1975: 错误:提供给函数‘zend_is_callable'的实参太少
/usr/local/src/memcache-2.2.3/memcache.c:2003: 错误:提供给函数‘zend_list_insert'的实参太少
/usr/local/src/memcache-2.2.3/memcache.c: 在函数‘zif_memcache_set_server_params'中:
/usr/local/src/memcache-2.2.3/memcache.c:2059: 错误:提供给函数‘zend_is_callable'的实参太少
/usr/local/src/memcache-2.2.3/memcache.c: 在函数‘mmc_find_persistent'中:
/usr/local/src/memcache-2.2.3/memcache.c:2159: 错误:提供给函数‘zend_list_insert'的实参太少
/usr/local/src/memcache-2.2.3/memcache.c:2177: 错误:提供给函数‘zend_list_insert'的实参太少
make: *** [memcache.lo] 错误 1

# 解决方法
# vim memcache.c
将所有的:zend_list_insert(pool, le_memcache_pool);
改为:zend_list_insert(pool, le_memcache_pool TSRMLS_CC);
将所有的:zend_list_insert(mmc, le_pmemcache);
改为:zend_list_insert(mmc, le_pmemcache TSRMLS_CC);

讲所有的:if (!zend_is_callable(failure_callback, 0, NULL))
改为:if (!zend_is_callable(failure_callback, 0, NULL, NULL))

修改完成后,重新make编译;

[root@yt-01 memcache-2.2.3]# make & make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20131226/
[root@yt-01 memcache-2.2.3]# ls /usr/local/php/lib/php/extensions/no-debug-zts-20131226/
memcache.so opcache.so

[root@yt-01 memcache-2.2.3]# vim /usr/local/php/etc/php.ini
搜索extension,在extension模块行的最后加上: extension="memcache.so"
[root@yt-01 memcache-2.2.3]# /usr/local/php/bin/php -m
有memcache 模块就可以了

2 test

[root@yt-01 memcache-2.2.3]# vim 1.php  //编辑测试脚本
<?php
//连接Memcache Memcache
$mem = new Memcache;
$mem->connect("localhost", 11211);
//保存数据
$mem->set('key1', 'This is first value', 0, 60);
$val = $mem->get('key1');
echo "Get key1 value: " . $val ."<br>";
//替换数据
$mem->replace('key1', 'This is replace value', 0, 60);
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "<br>";
//保存数组数据
$arr = array('aaa', 'bbb', 'ccc', 'ddd');
$mem->set('key2', $arr, 0, 60);
$val2 = $mem->get('key2');
echo "Get key2 value: ";
print_r($val2);
echo "<br>";
//删除数据
$mem->delete('key1');
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "<br>";
//清除所有数据
$mem->flush();
$val2 = $mem->get('key2');
echo "Get key2 value: ";
print_r($val2);
echo "<br>";
//关闭连接
$mem->close();
?>
# 脚本意为: 连接memcache set一个数据, 保存 替换删除 关闭 等。

[root@yt-01 memcache-2.2.3]# /usr/local/php/bin/php 1.php   //测试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
)


或者,将1.php放到某个虚拟主机的根目录下,在浏览器测试
显示如上内容就表示没问题

8. Storing sessions in memcached

The application scenario in this section is load balancing under the LNMP architecture. If the first login is on the A server and the second login is on the B server, if the nginx proxy upstream is used, ip_hash can be used; what if LVS is used? The solution is: the session does not exist on the server's disk, but on memcached. memcached as a public server, any web server can connect!

本实例是在lamp/lnmp环境下实现
编辑php.ini添加两行(待测试)
session.save_handler = memcache
session.save_path = "tcp://192.168.122.130:11211"
或者httpd.conf中对应的虚拟主机中添加
php_value session.save_handler "memcache"
php_value session.save_path "tcp://192.168.122.130:11211"
或者php-fpm.conf对应的pool中添加
php_value[session.save_handler] = memcache
php_value[session.save_path] = "tcp://192.168.122.130:11211 "

1 Download the test file

wget http://study.lishiming.net/.mem_se.txt
将其移动到虚拟主机目录中:
mv /root/.mem_se.txt /data/wwwroot/www.111.com

cp .mem_se.txt 1.php
在php-fpm.conf对应的pool中添加:

vim /usr/local/php-fpm/etc/php-fpm.conf

添加:
php_value[session.save_handler] = memcache
php_value[session.save_path] = "tcp://192.168.122.130:11211"

注:如上要是有专门的memcache服务器,这儿就需要配置独立memcache的地址。

/etc/init.d/php-fpm restart

2 test

[root@yt-01 www.111.com]# curl localhost/1.php
1507648188<br><br>1507648188<br><br>41026no8snqjq81phrl7ukovc6
[root@yt-01 www.111.com]# curl localhost/1.php
1507648268<br><br>1507648268<br><br>e2apbv2kkibntt4r59llfj52r1
[root@yt-01 www.111.com]# curl localhost/1.php
1507648269<br><br>1507648269<br><br>4mjm7v7thjldh413kuebcbjbe0
[root@yt-01 www.111.com]# curl localhost/1.php
1507648270<br><br>1507648270<br><br>1if2vpeut27r1siebjlv87ik92
登录 查看值:
[root@yt-01 www.111.com]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get i7vddchc2jdjpedsvck2tppra2
VALUE i7vddchc2jdjpedsvck2tppra2 0 37
TEST|i:1507648271;TEST3|i:1507648271;
END
如上使用get 去查看一个值! 可以查询到相关的数据。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324502576&siteId=291194637