forked from hackerlank/note
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemcache笔记.txt
More file actions
120 lines (103 loc) · 4.19 KB
/
Copy pathmemcache笔记.txt
File metadata and controls
120 lines (103 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
Memcache 高性能的分布式内存对象缓存系统 key-value型存储
安装
win
服务器端 Memcached.exe
基于命令行的执行型程序,直接双击运行即可
-d install 安装服务
-d uninstall 卸载服务
-p TCP监听端口 默认为11211
-l 监听的网卡ip 在一台服务器存在多个ip地址时,指定memcached服务器,仅仅监听那个ip地址
-m number 执行memcached服务器,所占用的最大的内存容量,以M兆为单位.默认64M
客户端
支持网络连接,并可以发送纯文本的指令,就可以作为mamcached客户端使用
telnet 链接
stats 查看状态
linux
libevent-2.0.21-stable
memcached-1.4.27
memcached -u memcached(用户) 启动
memcached & 开启
apache
加载php-memcache扩展
php官方下载php_memcache.dll文件 放到php/ext/文件夹下
php.ini 增加 extension=php_memcache.dll
重启apache
linux
扩展源码 memcache
生成扩展文件 phpize
常用指令
set key 压缩标识 有效期 长度 设置
value
key 不能大于250字节
标识
2 压缩 使用MEMCACHE_COMPRESSED指定对值进行压缩(使用zlib)
1 序列化后
0 字符串
有效期 时间间隔(s) 时间戳 时间间隔<=24*30*3600<=时间戳
长度 值的大小(字节为单位)
value 字符串 单个值不能超过1M大小
get key 获取
incr key number 自增多少
decr key number 自减多少
add 和set一样 仅添加 key存在则失败
replace 和set一样 仅替换 key不存在则失败
delete key 删除指定的key
flush_all 清空所有缓存
version
verbosity
quit
stars 获取当前memcached服务器的状态
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使用memcache
$memcache = new Memacache();
$memcache->addServer($host,$port); //添加集群
$memcache->connect('192.168.1.1','11211'); //连接memcache
$memcache->set('key','value'[,标识,有效期]); //设置
$memcache->get('key'); //获取
$memcache->delete('key'); //删除
$memcache->close(); //关闭连接
缓存失效
缓存过期
Memcache在处理过去的缓存项时,采用懒惰模式处理方法
缓存项过期不会立即删除,直到对该缓存项执行了get操作,才会删除过期的缓存项
缓存空间已满
Memcache在插入新数据时,如果空间不足采用的删除旧缓存项的策略.
采用删除最近最少使用(使用频率低)的缓存项(LRU策略)
session入memcache
php.ini session.save_handler = memcache
ini_set('session.save_handler','mamcache');
ini_set('session.save_path','tcp://127.0.0.1:11211');
ini_set('session.save_path','tcp://127.0.0.1:11211;tcp://127.0.0.2:11212'); //分布式
TP使用memcache
初始化缓存配置
S(array(
'type'=>'memcache', //redis,mongo
'host'=>'127.0.0.1',
'port'=>'11211'
));
S('key'); //获取
S('key', 'value'); //获取
S('key', null); //删除