CentOS 7 安装Memcached服务

前端之家收集整理的这篇文章主要介绍了CentOS 7 安装Memcached服务前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Memcached 简介

Memcached 安装

  • yum安装Memcached
[root@linuxprobe ~]# yum -y install memcached
[root@linuxprobe ~]# cat /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
  • 源码安装Memcached
wget http://memcached.org/latest                    下载最新版本
tar -zxvf memcached-1.x.x.tar.gz                    解压源码
cd memcached-1.x.x                                  进入目录
./configure --prefix=/usr/local/memcached           配置
make && make test                                   编译
sudo make install                                   安装

注意:如果使用自动安装 memcached 命令位于 /usr/local/bin/memcached。
启动选项:
-d 是启动一个守护进程;
-m 是分配给Memcache使用的内存数量,单位是MB;
-u 是运行Memcache的用户
-l 是监听的服务器IP地址,可以有多个地址;
-p 是设置Memcache监听的端口,,最好是1024以上的端口;
-c 是最大运行的并发连接数,默认是1024;
-P 是设置保存Memcache的pid文件

# 启动memcached 
[root@linuxprobe ~]# systemctl start memcached 
[root@linuxprobe ~]# systemctl enable memcached
Created symlink from /etc/systemd/system/multi-user.target.wants/memcached.service to /usr/lib/systemd/system/memcached.service.
  • 防火墙开启情况下,执行下面命令
[root@linuxprobe ~]# firewall-cmd --add-port=11211/tcp --permanent
success
[root@linuxprobe ~]# firewall-cmd --reload
success

Memcached 连接

  • python使用memcached
[1] Install Python Memcached Client Library.
[root@linuxprobe ~]# yum -y install python-memcached
[2] Basic usage on Python.
[root@linuxprobe ~]$ vi use_memcache.py
#!/usr/bin/env python
import memcache
client = memcache.Client(["127.0.0.1:11211"],cache_cas=True)
# set and get a Key
client.set("key01","value01")
print "key01.value :",client.get("key01")
# append and get a Key
client.append("key01",",value02")
print "key01.value :",client.get("key01")
client.set("key02",1)
# increment
client.incr("key02",100)
print "key02.value :",client.get("key02")
# decrement
client.decr("key02",51)
print "key02.value :",client.get("key02")
# CAS
client.set("key03","value03")
print "key03.value :",client.gets("key03")
print "key03.casid :",client.cas_ids["key03"]
client.cas("key03","value04")
print "key03.value :",client.gets("key03")
# run
[root@linuxprobe ~]$ python use_memcache.py
key01.value : value01
key01.value : value01,value02
key02.value : 101
key02.value : 50
key03.value : value03
key03.casid : 297
key03.value : value04
  • PHP使用memcached
[1] Install PHP Memcached Client Module.
# install from EPEL
[root@linuxprobe ~]# yum --enablerepo=epel -y install PHP-pecl-memcached
[2]     Basic usage on PHP.
[root@linuxprobe ~]$ vi use_memcache.PHP
<?PHP
$memcache = new Memcached();
$memcache->addServer('localhost',11211);
$memcache->setOption(Memcached::OPT_COMPRESSION,false);
// set and get a Key
$memcache->set('key01','value01');
print 'key01.value : ' . $memcache->get('key01') . "\n";
// append and get a Key
$memcache->append('key01',',value02');
print 'key01.value : ' . $memcache->get('key01') . "\n";
$memcache->set('key02',1);
print 'key02.value : ' . $memcache->get('key02') . "\n";
// increment
$memcache->increment('key02',100);
print 'key02.value : ' . $memcache->get('key02') . "\n";
// decrement
$memcache->decrement('key02',51);
print 'key02.value : ' . $memcache->get('key02') . "\n";
$memcache->set('key03','value03');
print 'key03.value : ' . $memcache->get('key03') . "\n";
// CAS (on the example below,the Value of key03 will not update to value05)
$memcache->get('key03',null,$cas);
$memcache->replace('key03','value04');
if ($memcache->getResultCode() == Memcached::RES_NOTFOUND) {
    $memcache->add('key03','value03');
} else {
    $memcache->cas($cas,'key03','value05');
}
print 'key03.value : ' . $memcache->get('key03') . "\n";
?>
# run
[root@linuxprobe ~]$ PHP use_memcache.PHP
key01.value : value01
key01.value : value01,value02
key02.value : 1
key02.value : 101
key02.value : 50
key03.value : value03
key03.value : value04
  • Node.js 使用memcached
[1] Install Memcached Client Module.
[root@linuxprobe ~]$ npm install memcache
memcache@0.3.0 node_modules/memcache
[2] Basic usage on Node.js.
[root@linuxprobe ~]$ vi use_memcache.js
var memcache = require('memcache');
var client = new memcache.Client();
client.connect();
// set and get a Key
client.set('key01','value01');
client.get('key01',function (err,val) {
    console.log("key01.value :",val);
});
// append and get a Key
client.append('key01',value02');
client.get('key01',val);
});
client.set('key02',1);
client.get('key02',val) {
    console.log("key02.value :",val);
});
// increment
client.increment('key02',100);
client.get('key02',val);
});
// decrement
client.decrement('key02',51);
client.get('key02',val);
});
// delete a Key
client.delete('key03');
client.get('key03',val) {
    console.log("key03.value :",val);
});
client.close();
# run
[root@linuxprobe ~]$ node use_memcache.js
key01.value : value01
key01.value : value01,value02
key02.value : 1
key02.value : 101
key02.value : 50
key03.value : null

猜你在找的CentOS相关文章