PHP中的Memcache的应用
一、Memcache概述
1、memcache是一个高性能的分布式的内存对象缓存系统,它能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。使用Memcache的网站一般流量都是比较大的,为了缓解数据库的压力,让Memcache作为一个缓存区域,把部分信息保存在内存中,在前端能够迅速的进行存取。
2、Memcache(内存,缓存):是一个高性能的分布式的内存对象缓存系统。通过在内存里维护一个巨大的hash表。(key=value)
Hash表
| key | value |
| mystr | “abc” |
| myarr | Array(“aa”, “cc”); |
| object | Object值 |
3、就是一个软件(服务软件) c/s软件
Mysql 192.168.1.222 3306
Apache www.baidu.com 80
Memcache 192.168.1.111 11211
维护内存,是将数据在内存中使用, 减少I/O 150k 开源
二、Memcache工作原理
Memcache 软件, memcached
http httpd
vsftp vsftpd
c/s软件
memcached是以守护程序方式运行于一个或多个服务器中,随时会接收客户端的连接和操作。
客户端使用各种语言去编写 PHP/java/c/c++/perl/python/ruby等
三、安装Memcache服务器(Linux和Window上分别安装)
1、Linux下:基于libevent事件
安装libevent时
./configure –with-libevent=/usr
Make && make install
安装memcached
./configure –with-libevent=/usr
Make && make install
启动:Memcahced –d –m 128 –l 192.168.1.111 –p 11211 –u root (-m表示分配的,-l表示服务地址,-p表示端口,-u表示启动的用户)
停止: kill `cat /tmp/memcached.pid`; Killall memcached
2、Windows下
Memcahced.exe -d install [uninstall]
Memcached.exe –d -m 50 –l 127.0.0.1 -p 11211 start
a.安装包在上面的文件夹中(需要memcached-1.2.1-win32和php_memcache.dll)
b.解压到目录:c;memcached (自定义,可在任何目录)
c.c:memcachedmemcached.exe -d install (安装)
d.c:memcachedmemcached.exe -d start (启动服务)
e.telnet 127.0.0.1 11211 (使用telnet测试是否成功) 输入stats命令查看基本信息
f.复制php_memcache.dll到 phpext目录下
g.在php.ini中添加:extension=php_memcache.dll,重启服务器,然后在phpinfo()中查看安装情况
四、Memcached服务器的管理(启动)
1、Memcached的基本设置:
-p 监听的端口
-l 连接的IP地址, 默认是本机
-d start 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u 以的身份运行 (仅在以root运行的时候有效)
-m 最大内存使用,单位MB。默认64MB ,最大好像2G-M 内存耗尽时返回错误,而不是删除项
-c 最大同时连接数,默认是1024
-f 块大小增长因子,默认是1.25
-n 最小分配空间,key+value+flags默认是48
-h 显示帮助
Memcahced.exe -d install [uninstall]
Memcached.exe –d -m 50 –l 127.0.0.1 -p 11211 start
2、操作Memcached (命令行方式telnet作为客户端)
telnet 192.168.1.128 80 ---apache
telnet 192.168.1.129 21 ---ftpd
telnet 192.168.1.111 22 ---ssh
telnet 192.168.1.222 11211 ---memcached
3、Memcache各种命令说明
| Command | Description | Example |
| get | Reads a value | get mykey |
| set | Set a key unconditionally | set mykey 0 60 5 |
| add | Add a new key | add newkey 0 60 5 |
| replace | Overwrite existing key | replace key 0 60 5 |
| append | Append data to existing key | append key 0 60 15 |
| prepend | Prepend data to existing key | prepend key 0 60 15 |
| incr | Increments numerical key value by given number | incr mykey 2 |
| decr | Decrements numerical key value by given number | decr mykey 5 |
| delete | Deletes an existing key | delete mykey |
| flush_all | Invalidate specific items immediately | flush_all |
| Invalidate all items in n seconds | flush_all 900 | |
| stats | Prints general statistics | Stats |
| Prints memory statistics | stats slabs | |
| Prints memory statistics | stats malloc | |
| Print higher level allocation statistics | stats items | |
| stats detail | ||
| stats sizes | ||
| Resets statistics | stats reset | |
| version | Prints server version. | version |
| verbosity | Increases log level | verbosity |
| quit | Terminate telnet session | quit |
4、stats命令说明
| pid | memcache服务器的进程ID |
| uptime | 服务器已经运行的秒数 |
| time | 服务器当前的unix时间戳 |
| version | memcache版本 |
| pointer_size | 当前操作系统的指针大小(32位系统一般是32bit) |
| rusage_user | 进程的累计用户时间 |
| rusage_system | 进程的累计系统时间 |
| curr_items | 服务器当前存储的items数量 |
| total_items | 从服务器启动以后存储的items总数量 |
| bytes | 当前服务器存储items占用的字节数 |
| curr_connections | 当前打开着的连接数 |
| total_connections | 从服务器启动以后曾经打开过的连接数 |
| connection_structures | 服务器分配的连接构造数 |
| cmd_get | get命令(获取)总请求次数 |
| cmd_set | set命令(保存)总请求次数 |
| get_hits | 总命中次数 |
| get_misses | 总未命中次数 |
| evictions | 为获取空闲内存而删除的items数(分配给memcache的空间用满后需要删除旧的items来得到空间分配给新的items) |
| bytes_read | 总读取字节数(请求字节数) |
| bytes_written | 总发送字节数(结果字节数) |
| limit_maxbytes | 分配给memcache的内存大小(字节) |
| threads | 当前线程数 |
五、在PHP程序中使用Memcached
1、在PHP安装Memcache扩展
可以按面向过程方式
面向对象的方式
2、在PHP什么地方使用memcache
a、数据库读出来的数据(select)使用memcache处理
b、在会话控制session中使用
3、memcached的使用
a、使用方法
$memcache = new memcache;
$memcache->connect("127.0.0.1",11211) or die("连接失败"); //第一个参数是地址,第二个是端口号
$memcache->set("key","value","zip","time");//set方法添加一个缓存值(键名,键值,是否压缩,保持时间),前面两个参数必须
$var = $memcache->get("key"); //get通过键名,获取缓存中的内容
b、Memcache一下常用到的方法
Memcache::add — 添加一个值,如果已经存在,则返回false
Memcache::addServer — 添加一个可供使用的服务器地址
Memcache::close — 关闭一个Memcache对象
Memcache::connect — 创建一个Memcache对象
memcache_debug — 控制调试功能
Memcache::decrement — 对保存的某个key中的值进行减法操作
Memcache::delete — 删除一个key值
Memcache::flush — 清除所有缓存的数据
Memcache::get — 获取一个key值
Memcache::getExtendedStats — 获取进程池中所有进程的运行系统统计
Memcache::getServerStatus — 获取运行服务器的参数
Memcache::getStats — 返回服务器的一些运行统计信息
Memcache::getVersion — 返回运行的Memcache的版本信息
Memcache::increment — 对保存的某个key中的值进行加法操作
Memcache::pconnect — 创建一个Memcache的持久连接对象
Memcache::replace — R对一个已有的key进行覆写操作
Memcache::set — 添加一个值,如果已经存在,则覆写
Memcache::setCompressThreshold — 对大于某一大小的数据进行压缩
Memcache::setServerParams — 在运行时修改服务器的参数
六、Memcache的安全(不让别人访问)
内网设置放火墙
Iptables –A INPUT –p tcp –s 192.168.1.111 –dport 11211 –j ACCEPT
Iptables –A INPUT –p udp –s 192.168.1.111 –dpost 11211 –j ACCEPT
七、在PHP中的Memcache应用
1、最基本的在内存中设置值,增加、重置、删除、Memcache的方法、各种不同数据存储:str,arr,obj
<?php
$mem=new Memcache;
$mem->connect("localhost", 11211);
// $mem->addServer("www.lamp.com", 11221);
// $mem->addServer("192.167.1.112", 11211);
$mem->add("mystr", "this is a memcache test!", MEMCACHE_COMPRESSED, 3600);
$mem->set("mystr", "wwwwwwwwwwwwww", MEMCACHE_COMPRESSED, 3600);
$mem->delete("mystr"); //删除值
$mem->flush(); //删除所有的值
$str=$mem->get("mystr");
echo "string: ".$str."<br>";
// $mem->add("myarr", array("aaa", "bbb", "ccc", "ddd")); //默认存30天,也不能超过30天
print_r($mem->get("myarr"));
echo '<br>';
class Person {
var $name="zhangsan";
var $age=10;
}
// $mem->add("myobj", new Person);
var_dump($mem->get("myobj"));
echo "<br>";
echo $mem->getVersion();
echo '<pre>';
print_r($mem->getStats());
echo '</pre>';
$mem->close();
?>
2、从数据库取出的数据放到Memcache中
<?php
$mem=new Memcache;
$mem->connect("localhost", 11211);
//注意:同一个项目安装两次,key要有前缀
$sql="select * from shops";
$key=substr(md5($sql), 10, 8);
$data=$mem->get($key);
if(!$data){
$mysqli=new mysqli("localhost", "root", "123456", "xsphpdb");
$result=$mysqli->query($sql);
$data=array();
while($row=$result->Fetch_assoc()){
$data[]=$row;
}
$result->free();
$mysqli->close();
$mem->set($key, $data, MEMCACHE_COMPRESSED, 3600);
echo $sql;
}
echo '<pre>';
print_r($data);
echo '</pre>';
$mem->close();
?>

浙公网安备 33010602011771号