一、什么是Memchache
Memcache是一个高性能的分布式的内存对象缓存系统,它是通过在内存里维护一个巨大的hash表。hash表是根据键值对(Key value)直接进行访问的数据结构。
二、Memcache的工作原理
Memcache实际是一个C/S软件,而Memcached则是它的进程,以守护程序方式运行于一个或多个服务器中,随时会接收客户端的连接和操作。
三、Memcache的安装
1.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
2.Windows:
安装:memcahced.exe -d install
卸载:memcahced.exe -d uninstall
启动:memcahced.exe -d -m 50 -l 127.0.0.1 -p 11211 start
(-m 50 指定使用内存大小为50m,默认是64m;-l 127.0.0.1 指定监听IP地址;-p 11211 指定端口号,更多设置如下:)
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 显示帮助
四、操作(命令行方式,telnet作为客户端)
连接到memcache: telnet localhost 11211
命令名
描述
案例
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