简述
memcached是一款高性能、分布式的对象缓存系统,是基于libevent事件处理实现无阻塞通信。
安装
memcached依赖libevent,所以安装时要首先安装libevent,再安装memcached
安装libevent
- 解压
- 安装到指定路径
命令:./configure --prefix=/opt/install/libevent - make & make install
(下载路径:http://libevent.org/)
安装memcached
- 解压
- 安装到指定目录
命令:./configure --prefix=/opt/install/memcached --with-libevent=/opt/install/libevent
这里需要制定libevent的安装目录 - make&make install
(下载路径:http://memcached.org/)
启动
- 启动参数
| 参数 | 参数作用 |
|---|---|
| -d | 启动一个守护进程 |
| -m | 分配给memcached使用的内存数量 |
| -u | 运行memcached的用户 |
| -l | 监听的服务器IP地址 |
| -p | 监听的端口 |
| -c | 最大运行的并发连接数 |
| -P | 设置保存Memcached的pid文件 |
eg: ./memcached -d -u root -l 192.168.139.143 -p 2222 -c 128 -m 100 -P myPid
常用命令
| 命令 | 作用 |
|---|---|
| set | 用于向缓存添加新的键值对 |
| add | 当缓存中不存在键时则写入 |
| replace | 当键已经存在时替换 |
| append | 在已有结果上追加数据 |
| prepend | 在已有数据前补充数据 |
| cas | 检查和更新,通常和gets一起使用 |
| get/gets | 获取数据/数据+版本号 |
| delete | 删除数据 |
| Incr/decr | 增加和减少数据 |
memached中key的组成:key flags expTime bytes
- key:键值 key-value 结构中的 key,用于查找缓存值。
- flags:可以包括键值对的整型参数,客户机使用它存储关于键值对的额外信息 。
- exptime:在缓存中保存键值对的时间长度(以秒为单位,0 表示永远)
- bytes:在缓存中存储的字节数