高速缓存系统

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhiyuan411/article/details/89048756

memcached

memcached是一套分布式的高速缓存系统,使用key-value形式,在内存存储数据,以BSD license授权发布。
官网:https://memcached.org/

安装方法

使用apt-get,yum或者官网下载源码安装,参见:https://memcached.org/downloads

启动方法

#示例
memcached -d -p 6666 -u nobody -m 1024 -c 16384 -P /var/run/memcached/memcached.pid

#参数说明
memcached -h
memcached 1.4.25
-p <num>      TCP port number to listen on (default: 11211)
-U <num>      UDP port number to listen on (default: 11211, 0 is off)
-s <file>     UNIX socket path to listen on (disables network support)
-A            enable ascii "shutdown" command
-a <mask>     access mask for UNIX socket, in octal (default: 0700)
-l <addr>     interface to listen on (default: INADDR_ANY, all addresses)
              <addr> may be specified as host:port. If you don't specify
              a port number, the value you specified with -p or -U is
              used. You may specify multiple addresses separated by comma
              or by using -l multiple times
-d            run as a daemon
-r            maximize core file limit
-u <username> assume identity of <username> (only when run as root)
-m <num>      max memory to use for items in megabytes (default: 64 MB)
-M            return error on memory exhausted (rather than removing items)
-c <num>      max simultaneous connections (default: 1024)
-k            lock down all paged memory.  Note that there is a
              limit on how much memory you may lock.  Trying to
              allocate more than that would fail, so be sure you
              set the limit correctly for the user you started
              the daemon with (not for -u <username> user;
              under sh this is done with 'ulimit -S -l NUM_KB').
-v            verbose (print errors/warnings while in event loop)
-vv           very verbose (also print client commands/reponses)
-vvv          extremely verbose (also print internal state transitions)
-h            print this help and exit
-i            print memcached and libevent license
-V            print version and exit
-P <file>     save PID in <file>, only used with -d option
-f <factor>   chunk size growth factor (default: 1.25)
-n <bytes>    minimum space allocated for key+value+flags (default: 48)
-L            Try to use large memory pages (if available). Increasing
              the memory page size could reduce the number of TLB misses
              and improve the performance. In order to get large pages
              from the OS, memcached will allocate the total item-cache
              in one large chunk.
-D <char>     Use <char> as the delimiter between key prefixes and IDs.
              This is used for per-prefix stats reporting. The default is
              ":" (colon). If this option is specified, stats collection
              is turned on automatically; if not, then it may be turned on
              by sending the "stats detail on" command to the server.
-t <num>      number of threads to use (default: 4)
-R            Maximum number of requests per event, limits the number of
              requests process for a given connection to prevent 
              starvation (default: 20)
-C            Disable use of CAS
-b <num>      Set the backlog queue limit (default: 1024)
-B            Binding protocol - one of ascii, binary, or auto (default)
-I            Override the size of each slab page. Adjusts max item size
              (default: 1mb, min: 1k, max: 128m)
-F            Disable flush_all command
-o            Comma separated list of extended or experimental options
              - (EXPERIMENTAL) maxconns_fast: immediately close new
                connections if over maxconns limit
              - hashpower: An integer multiplier for how large the hash
                table should be. Can be grown at runtime if not big enough.
                Set this based on "STAT hash_power_level" before a 
                restart.
              - tail_repair_time: Time in seconds that indicates how long to wait before
                forcefully taking over the LRU tail item whose refcount has leaked.
                Disabled by default; dangerous option.
              - hash_algorithm: The hash table algorithm
                default is jenkins hash. options: jenkins, murmur3
              - lru_crawler: Enable LRU Crawler background thread
              - lru_crawler_sleep: Microseconds to sleep between items
                default is 100.
              - lru_crawler_tocrawl: Max items to crawl per slab per run
                default is 0 (unlimited)
              - lru_maintainer: Enable new LRU system + background thread
              - hot_lru_pct: Pct of slab memory to reserve for hot lru.
                (requires lru_maintainer)
              - warm_lru_pct: Pct of slab memory to reserve for warm lru.
                (requires lru_maintainer)
              - expirezero_does_not_evict: Items set to not expire, will not evict.
                (requires lru_maintainer)

访问方法

telnet 192.168.4.6 11211     //连接memcached
set myname 0 180 3                //添加或者替换变量,变量名称为myname;0表示不压缩,180为数据缓存时间(单位秒,0为永不超时),3为需要存储的数据字节数量;然后根据提示输入变量值
get name                        //获取变量的值
add myname 0 180 10            //新建,myname不存在则添加,存在则报错
replace myname 0 180 10        //替换,如果myname不存在则报错
append myname 0 180 10        //向变量中追加数据
delete myname                    //删除变量
stats                        //查看状态
flush_all                        //清空所有
quit                             //退出

redis

redis是一个key-value存储系统。数据都是缓存在内存中,并且会周期性回写到磁盘上,提供两种主要的持久化策略:RDB快照和AOF日志。在速度上,可以达到memcached的级别。以BSD license授权发布
官网:https://redis.io/

安装方法

官网下载源码安装:https://redis.io/download

启动方法

# 需要更改配置后才能在后台以守护进程形式启动
redis-server --help
Usage: ./redis-server [/path/to/redis.conf] [options]
       ./redis-server - (read config from stdin)
       ./redis-server -v or --version
       ./redis-server -h or --help
       ./redis-server --test-memory <megabytes>

Examples:
       ./redis-server (run the server with default conf)
       ./redis-server /etc/redis/6379.conf
       ./redis-server --port 7777
       ./redis-server --port 7777 --slaveof 127.0.0.1 8888
       ./redis-server /etc/myredis.conf --loglevel verbose

Sentinel mode:
       ./redis-server /etc/sentinel.conf --sentinel

访问方法

redis-cli支持的完整命令列表,参见:https://redis.io/commands

# 交互方式
redis-cli -p 你的redis端口 -h IP -a 密钥
# 命令方式
redis-cli -p 你的redis端口 -h IP -a 密钥 set test_key 123
redis-cli -p 你的redis端口 -h IP -a 密钥 get test_key
# 命令帮助
redis-cli --help
redis-cli 2.8.9

Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
  -h <hostname>      Server hostname (default: 127.0.0.1).
  -p <port>          Server port (default: 6379).
  -s <socket>        Server socket (overrides hostname and port).
  -a <password>      Password to use when connecting to the server.
  -r <repeat>        Execute specified command N times.
  -i <interval>      When -r is used, waits <interval> seconds per command.
                     It is possible to specify sub-second times like -i 0.1.
  -n <db>            Database number.
  -x                 Read last argument from STDIN.
  -d <delimiter>     Multi-bulk delimiter in for raw formatting (default: \n).
  -c                 Enable cluster mode (follow -ASK and -MOVED redirections).
  --raw              Use raw formatting for replies (default when STDOUT is
                     not a tty).
  --csv              Output in CSV format.
  --latency          Enter a special mode continuously sampling latency.
  --latency-history  Like --latency but tracking latency changes over time.
                     Default time interval is 15 sec. Change it using -i.
  --slave            Simulate a slave showing commands received from the master.
  --rdb <filename>   Transfer an RDB dump from remote server to local file.
  --pipe             Transfer raw Redis protocol from stdin to server.
  --pipe-timeout <n> In --pipe mode, abort with error if after sending all data.
                     no reply is received within <n> seconds.
                     Default timeout: 30. Use 0 to wait forever.
  --bigkeys          Sample Redis keys looking for big keys.
  --scan             List all keys using the SCAN command.
  --pattern <pat>    Useful with --scan to specify a SCAN pattern.
  --intrinsic-latency <sec> Run a test to measure intrinsic system latency.
                     The test will run for the specified amount of seconds.
  --eval <file>      Send an EVAL command using the Lua script at <file>.
  --help             Output this help and exit.
  --version          Output version and exit.

Examples:
  cat /etc/passwd | redis-cli -x set mypasswd
  redis-cli get mypasswd
  redis-cli -r 100 lpush mylist x
  redis-cli -r 100 -i 1 info | grep used_memory_human:
  redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3
  redis-cli --scan --pattern '*:12345*'

  (Note: when using --eval the comma separates KEYS[] from ARGV[] items)

When no command is given, redis-cli starts in interactive mode.
Type "help" in interactive mode for information on available commands.

memcached和redis比较

memcached redis
键名限制在250字节 键名最大上限为512MB
值限制在不超过1MB 值的最大上限为512MB
值为字符串 值可以为5种类型:string,list,set,sorted set,hash
GET/SET操作 支持很多复杂操作,例如排序
数据只放在内存中 数据缓存在内存中,并周期性回写磁盘
过期策略只支持LRU算法 过期策略支持包含LRU在内的多种算法
不支持身份和安全机制 支持身份和安全机制
可靠性低(cache系统) 可靠性高,支持数据持久化和主从复制(DB系统)
只能采用客户端实现分布式存储 支持在服务器端构建分布式存储

适用场景

memcached:
做缓存,提高性能
redis:
NoSQL数据库,消息队列、数据堆栈和数据缓存等;

猜你喜欢

转载自blog.csdn.net/zhiyuan411/article/details/89048756