key
value
mystr
“abc”
myarr
array(“aa”, “cc”);
object
object值
工作原理
memcache 软件, memcached
memcached是以守护程序方式运行在一个或者多个服务之中,随时客服端的操作和连接.
关于memcache的安装
在linux 下面的安装
基于libevent事件
linux下
安装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
停止: kill `cat /tmp/memcached.pid`;
killall memcached
windows下
memcahced.exe -d install [uninstall]
memcached.exe –d -m 50 –l 127.0.0.1 -p 11211 start
、memcached服务器的管理(启动)
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 显示帮助
七、如何遍历memcache
八、在php程序中使用memcached
a 在php安装memcache扩展
可以按面向过程方式
面向对象的方式
b 在php什么地方使用memcache
一、 数据库读出来的数据(select)使用memcache处理
二、 在会话控制session中使用
c 实例
九、memcache的安全(不让别人访问)
1 .内网
2. 设置放火墙
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
安装. 然后开始 memcached -d start
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
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
当前线程数
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手册 “lxxxiv. memcache functions” 这章。
memcache面向对象的常用接口包括:
memcache::connect -- 打开一个到memcache的连接
memcache::pconnect -- 打开一个到memcache的长连接
memcache::close -- 关闭一个memcache的连接
memcache::set -- 保存数据到memcache服务器上
memcache::get -- 提取一个保存在memcache服务器上的数据
memcache::replace -- 替换一个已经存在memcache服务器上的项目(功能类似memcache::set)
memcache::delete -- 从memcache服务器上删除一个保存的项目
memcache::flush -- 刷新所有memcache服务器上保存的项目(类似于删除所有的保存的项目)
memcache::getstats -- 获取当前memcache服务器运行的状态
memcache::addserver -- 分布式服务器添加一个服务器
使用 php 操作 memcache
connect(localhost, 11211);
/*
* 注意:
* 1. 同一个项目安装两次,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 '
';
print_r($data);
echo '
';
$mem->close();