它山之石可以攻玉

键盘上的生活
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

CentOS 7 安装Memcached服务

Posted on 2017-06-08 10:45  陈达辉  阅读(1055)  评论(0编辑  收藏  举报

Memcached 简介

  • Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。
  • Memcached 官方地址:http://memcached.org/
  • Memcached 教程: http://www.runoob.com/Memcached/Memcached-tutorial.html

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', function (err, val) {
    console.log("key01.value :", val);
});
client.set('key02', 1);
client.get('key02', function (err, val) {
    console.log("key02.value :", val);
});
// increment
client.increment('key02', 100);
client.get('key02', function (err, val) {
    console.log("key02.value :", val);
});
// decrement
client.decrement('key02', 51);
client.get('key02', function (err, val) {
    console.log("key02.value :", val);
});
// delete a Key
client.delete('key03');
client.get('key03', function (err, 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