
什么是memcached
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。
安装memcached
mac下安装
brew install memcached
查看是否安装成功
jockie:~$ which memcached
/usr/local/bin/memcached
jockie:~$ brew list | grep memcached
memcached
ubuntu下安装
sudo apt-get install memcached
查看是否安装成功
:~$ which memcached
/usr/bin/memcached
启动memcached
jockie:~$ memcached -d
- -p 指定占用端口,默认11211
- -d 让memcached在后台运行
- -m 指定占用内存,单位为MB,默认64MB
- -l 如果想要让别的机器连接,就必须设置-l 0.0.0.0
查看服务是否启动
jockie:~$ ps aux | grep memcached
jockie 4980 0.0 0.0 2442020 1992 s001 S+ 7:12PM 0:00.00 grep memcached
jockie 4974 0.0 0.0 2480544 2484 ?? Ss 7:12PM 0:00.01 memcached -d
telnet操作memcached
连接和退出
连接:telnet ip地址 [11211]
退出:quit
基本命令
存储命令
- 基本语法:
command key flags exptime bytes [noreply] value
- 参数说明:
- command: 存储命令,包含
set
/add
/replace
/append
/prepend
- key: 键值 key-value 结构中的 key,用于查找缓存值。
- flags: 可以包括键值对的整型参数,客户机使用它存储关于键值对的额外信息 。
- exptime: 在缓存中保存键值对的时间长度(以秒为单位,0 表示永远)。
- bytes: 在缓存中存储的字节数。
- noreply(可选): 该参数告知服务器不需要返回数据。
- value: 存储的值(始终位于第二行)(可直接理解为key-value结构中的value)。
- command: 存储命令,包含
- 示例:
jockie:~$ telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. set username 0 0 4 jack STORED add username 0 0 4 john NOT_STORED add pass 0 0 6 123456 STORED replace username 0 0 5 peter STORED append username 0 0 5 athon STORED prepend pass 0 0 2 00 STORED
- 基本语法:
查找命令
- 基本语法
command key1 [key2 key3]
- 参数说明:
- command: 包含
get
/gets
/delete
- command: 包含
- 示例:
get username VALUE username 0 9 johnathon END get username pass VALUE username 0 9 johnathon VALUE pass 0 8 00123456 END delete pass DELETED get pass END
- 基本语法
自增自减命令
- 基本语法:
command key value
- 参数说明:
- command: 包含
incr/decr
- value: 自增或自减的值
- command: 包含
- 示例:
set height 0 0 3 180 STORED get height VALUE height 0 3 180 END incr height 5 185 get height VALUE height 0 3 185 END decr height 10 175 get height VALUE height 0 3 175 END
- 基本语法:
统计命令
- stats
stats STAT pid 4974 STAT uptime 4990 STAT time 1512477311 STAT version 1.5.3 STAT libevent 2.1.8-stable STAT pointer_size 64 STAT rusage_user 0.510169 STAT rusage_system 0.355403 STAT max_connections 1024 STAT curr_connections 10 STAT total_connections 20 STAT rejected_connections 0 STAT connection_structures 11 STAT reserved_fds 20 STAT cmd_get 27 STAT cmd_set 19 STAT cmd_flush 6 STAT cmd_touch 0 STAT get_hits 16 STAT get_misses 11 STAT get_expired 0 STAT get_flushed 4 STAT delete_misses 0 STAT delete_hits 2 STAT incr_misses 0 STAT incr_hits 1 STAT decr_misses 0 STAT decr_hits 1 STAT cas_misses 0 STAT cas_hits 0 STAT cas_badval 0 STAT touch_hits 0 STAT touch_misses 0 STAT auth_cmds 0 STAT auth_errors 0 STAT bytes_read 1015 STAT bytes_written 786 STAT limit_maxbytes 67108864 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT time_in_listen_disabled_us 0 STAT threads 4 STAT conn_yields 0 STAT hash_power_level 16 STAT hash_bytes 524288 STAT hash_is_expanding 0 STAT slab_reassign_rescues 0 STAT slab_reassign_chunk_rescues 0 STAT slab_reassign_evictions_nomem 0 STAT slab_reassign_inline_reclaim 0 STAT slab_reassign_busy_items 0 STAT slab_reassign_busy_deletes 0 STAT slab_reassign_running 0 STAT slabs_moved 0 STAT lru_crawler_running 0 STAT lru_crawler_starts 3315 STAT lru_maintainer_juggles 8302 STAT malloc_fails 0 STAT log_worker_dropped 0 STAT log_worker_written 0 STAT log_watcher_skipped 0 STAT log_watcher_sent 0 STAT bytes 144 STAT curr_items 2 STAT total_items 15 STAT slab_global_page_pool 0 STAT expired_unfetched 0 STAT evicted_unfetched 0 STAT evicted_active 0 STAT evictions 0 STAT reclaimed 1 STAT crawler_reclaimed 0 STAT crawler_items_checked 2 STAT lrutail_reflocked 0 STAT moves_to_cold 16 STAT moves_to_warm 5 STAT moves_within_lru 0 STAT direct_reclaims 0 STAT lru_bumps_dropped 0 END
- stats items
stats items STAT items:1:number 2 STAT items:1:number_hot 0 STAT items:1:number_warm 0 STAT items:1:number_cold 2 STAT items:1:age_hot 0 STAT items:1:age_warm 0 STAT items:1:age 705 STAT items:1:evicted 0 STAT items:1:evicted_nonzero 0 STAT items:1:evicted_time 0 STAT items:1:outofmemory 0 STAT items:1:tailrepairs 0 STAT items:1:reclaimed 1 STAT items:1:expired_unfetched 0 STAT items:1:evicted_unfetched 0 STAT items:1:evicted_active 0 STAT items:1:crawler_reclaimed 0 STAT items:1:crawler_items_checked 2 STAT items:1:lrutail_reflocked 0 STAT items:1:moves_to_cold 16 STAT items:1:moves_to_warm 5 STAT items:1:moves_within_lru 0 STAT items:1:direct_reclaims 0 STAT items:1:hits_to_hot 5 STAT items:1:hits_to_warm 0 STAT items:1:hits_to_cold 11 STAT items:1:hits_to_temp 0 END
- stats slabs
tats slabs STAT 1:chunk_size 96 STAT 1:chunks_per_page 10922 STAT 1:total_pages 1 STAT 1:total_chunks 10922 STAT 1:used_chunks 2 STAT 1:free_chunks 10920 STAT 1:free_chunks_end 0 STAT 1:mem_requested 144 STAT 1:get_hits 16 STAT 1:cmd_set 19 STAT 1:delete_hits 2 STAT 1:incr_hits 1 STAT 1:decr_hits 1 STAT 1:cas_hits 0 STAT 1:cas_badval 0 STAT 1:touch_hits 0 STAT active_slabs 1 STAT total_malloced 1048576 END
- stats
清理缓存命令
- 基本语法:
flush_all [time] [noreply]
- 参数说明:
[time]可选参数,用于在指定时间执行清理缓存操作。 - 示例:
flush_all OK get username END get pass END
- 基本语法:
python操作memcached
- 安装
python-memcached
:pip install python-memcached
- 连接memcached
import memcache mc = memcache.Client(['127.0.0.1:11211'], debug=True)
- 设置数据
mc.set('username', 'john', time=60) mc.set('count',10,time=60) mc.set_multi({'gender': 'male', 'phone': '111111'}, time=60 * 2)
- 获取数据
print(mc.get('phone'))
- 删除数据
mc.delete('gender')
- 自增、自减
mc.incr('count') # delta默认值为1 mc.decr('count', delta=3)