首页 > 代码库 > Memcached快速入门
Memcached快速入门
Memcached快速入门
What is Memcached
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。
安装
Ubuntu
$ sudo apt-get install memcached
$ ps aux | grep memcached
memcache 26355 0.0 0.0 325400 1208 ? Sl 16:30 0:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1
源码
sudo apt-get install g++ make libevent-dev
curl -O --location http://memcached.org/latest
mv latest memcached-latest.tar.gz
tar vxzf memcached-latest.tar.gz
cd memcached-*
./configure && make
sudo make install
配置
vim /etc/memcached.conf
- -l 127.0.0.1 监听ip
- -m 64 允许最大内存,这里默认时4 Megabytes(MB)
和memcached通信
安装telnet
sudo apt-get install telnet
试试
$ telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is ‘^]‘.
详细的ASCII(文本)协议
https://github.com/memcached/memcached/blob/master/doc/protocol.txt
查询基本信息
stats
STAT pid 26355
STAT uptime 457
STAT time 1404290303
STAT version 1.4.14
STAT libevent 2.0.21-stable
STAT pointer_size 64
STAT rusage_user 0.007959
STAT rusage_system 0.007959
STAT curr_connections 5
STAT total_connections 6
STAT connection_structures 6
STAT reserved_fds 20
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 0
STAT get_misses 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
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 7
STAT bytes_written 0
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT evictions 0
STAT reclaimed 0
END
查看配置
stats settings
STAT maxbytes 67108864
STAT maxconns 1024
STAT tcpport 11211
STAT udpport 11211
STAT inter 127.0.0.1
STAT verbosity 0
STAT oldest 0
STAT evictions on
STAT domain_socket NULL
STAT umask 700
STAT growth_factor 1.25
STAT chunk_size 48
STAT num_threads 4
STAT num_threads_per_udp 4
STAT stat_key_prefix :
STAT detail_enabled no
STAT reqs_per_event 20
STAT cas_enabled yes
STAT tcp_backlog 1024
STAT binding_protocol auto-negotiate
STAT auth_enabled_sasl no
STAT item_size_max 1048576
STAT maxconns_fast no
STAT hashpower_init 0
STAT slab_reassign no
STAT slab_automove 0
END
保存值
set mykey 0 300 16
I Love Memcached
按回车将会得到STORED
set mykey 0 300 16
I Love Memcached
STORED
现在我们就可以通过get获取值了
get mykey
VALUE mykey 0 16
I Love Memcached
END
set格式:
<command name> <key> <flags> <exptime> <bytes>
设置Memcached开机启动
$ /etc/init.d/memcached status
* memcached is running
disable
sudo update-rc.d memcached disable
enable
sudo update-rc.d memcached enable
确认运行默认级别:
sudo update-rc.d memcached defaults
分布式部署
memcached -p 3030
memcached -p 3031
import pylibmc
mc = pylibmc.Client(["127.0.0.1:3030", "127.0.0.1:3031"], binary=True,
behaviors={"tcp_nodelay": True, "ketama": True})
mc["ahmed"] = "Hello World"
mc["tek"] = "Hello World"
- binary = True: 二进制协议,非ASCII协议
- behaviors={"tcp_nodelay": True, "ketama": True} :"ketama" = True 意味使用md5的hash算法进行对key进行分布
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。