• 1. Memcached简介
    • 1.1. 特征
  • 2. 安装与运行
    • 2.1. 自动安装
    • 2.2. 源码安装
    • 2.3. 验证
    • 2.4. 运行
      • 2.4.1. 前台运行
      • 2.4.2. 后台运行
    • 2.5. 连接
  • 3. Memcached运行参数

    1. Memcached简介

    • Memcached是一个开源的,高性能,分布式内存对象缓存系统。

    • Memcached是一种基于内存的key-value存储,用来存储小块的任意数据(字符串、对象)。这些数据可以是数据库调用、API调用或者是页面渲染的结果。

    • 一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态Web应用的速度、提高可扩展性。

    1.1. 特征

    memcached作为高速运行的分布式缓存服务器,具有以下的特点。

    • 协议简单
    • 基于libevent的事件处理
    • 内置内存存储方式
    • memcached不互相通信的分布式

    2. 安装与运行

    2.1. 自动安装

    1. # For Redhat/Fedora
    2. yum install -y memcached
    3. # For Debian or Ubuntu
    4. apt-get install memcached

    2.2. 源码安装

    安装指定版本的Memcached可以从 https://github.com/memcached/memcached/wiki/ReleaseNotes 地址下载。

    1. # Memcached depends on libevent
    2. yum install libevent-devel
    3. # install
    4. wget https://memcached.org/latest
    5. [you might need to rename the file]
    6. tar -zxf memcached-1.x.x.tar.gz
    7. cd memcached-1.x.x
    8. ./configure --prefix=/usr/local/memcached
    9. make && make test && sudo make install

    问题

    如遇以下报错,可再执行make install

    1. Signal handled: Interrupt.
    2. ok 51 - shutdown
    3. ok 52 - stop_server
    4. /bin/sh:行3: prove: 未找到命令
    5. make: *** [test] Error 127

    2.3. 验证

    确认是否安装成功,可执行以下命令

    1. /usr/local/memcached/bin/memcached -h

    2.4. 运行

    2.4.1. 前台运行

    1. /usr/local/memcached/bin/memcached -p 11211 -m 64m -vv

    2.4.2. 后台运行

    1. /usr/local/memcached/bin/memcached -p 11211 -m 64m -d -c 102400 -t 8 -P /tmp/memcached.pid

    2.5. 连接

    1. $ telnet 127.0.0.1 11211
    2. Trying 127.0.0.1...
    3. Connected to 127.0.0.1.
    4. Escape character is '^]'.
    5. set foo 0 0 3 保存命令
    6. bar 数据
    7. STORED 结果
    8. get foo 取得命令
    9. VALUE foo 0 3 数据
    10. bar 数据
    11. END 结束行
    12. quit 退出

    3. Memcached运行参数

    1. # /usr/local/memcached/bin/memcached -h
    2. memcached 1.5.12
    3. -p, --port=<num> TCP port to listen on (default: 11211)
    4. -U, --udp-port=<num> UDP port to listen on (default: 0, off)
    5. -s, --unix-socket=<file> UNIX socket to listen on (disables network support)
    6. -A, --enable-shutdown enable ascii "shutdown" command
    7. -a, --unix-mask=<mask> access mask for UNIX socket, in octal (default: 0700)
    8. -l, --listen=<addr> interface to listen on (default: INADDR_ANY)
    9. -d, --daemon run as a daemon
    10. -r, --enable-coredumps maximize core file limit
    11. -u, --user=<user> assume identity of <username> (only when run as root)
    12. -m, --memory-limit=<num> item memory in megabytes (default: 64 MB)
    13. -M, --disable-evictions return error on memory exhausted instead of evicting
    14. -c, --conn-limit=<num> max simultaneous connections (default: 1024)
    15. -k, --lock-memory lock down all paged memory
    16. -v, --verbose verbose (print errors/warnings while in event loop)
    17. -vv very verbose (also print client commands/responses)
    18. -vvv extremely verbose (internal state transitions)
    19. -h, --help print this help and exit
    20. -i, --license print memcached and libevent license
    21. -V, --version print version and exit
    22. -P, --pidfile=<file> save PID in <file>, only used with -d option
    23. -f, --slab-growth-factor=<num> chunk size growth factor (default: 1.25)
    24. -n, --slab-min-size=<bytes> min space used for key+value+flags (default: 48)
    25. -L, --enable-largepages try to use large memory pages (if available)
    26. -D <char> Use <char> as the delimiter between key prefixes and IDs.
    27. This is used for per-prefix stats reporting. The default is
    28. ":" (colon). If this option is specified, stats collection
    29. is turned on automatically; if not, then it may be turned on
    30. by sending the "stats detail on" command to the server.
    31. -t, --threads=<num> number of threads to use (default: 4)
    32. -R, --max-reqs-per-event maximum number of requests per event, limits the
    33. requests processed per connection to prevent
    34. starvation (default: 20)
    35. -C, --disable-cas disable use of CAS
    36. -b, --listen-backlog=<num> set the backlog queue limit (default: 1024)
    37. -B, --protocol=<name> protocol - one of ascii, binary, or auto (default)
    38. -I, --max-item-size=<num> adjusts max item size
    39. (default: 1mb, min: 1k, max: 128m)
    40. -F, --disable-flush-all disable flush_all command
    41. -X, --disable-dumping disable stats cachedump and lru_crawler metadump
    42. -o, --extended comma separated list of extended options
    43. most options have a 'no_' prefix to disable
    44. - maxconns_fast: immediately close new connections after limit
    45. - hashpower: an integer multiplier for how large the hash
    46. table should be. normally grows at runtime.
    47. set based on "STAT hash_power_level"
    48. - tail_repair_time: time in seconds for how long to wait before
    49. forcefully killing LRU tail item.
    50. disabled by default; very dangerous option.
    51. - hash_algorithm: the hash table algorithm
    52. default is murmur3 hash. options: jenkins, murmur3
    53. - lru_crawler: enable LRU Crawler background thread
    54. - lru_crawler_sleep: microseconds to sleep between items
    55. default is 100.
    56. - lru_crawler_tocrawl: max items to crawl per slab per run
    57. default is 0 (unlimited)
    58. - lru_maintainer: enable new LRU system + background thread
    59. - hot_lru_pct: pct of slab memory to reserve for hot lru.
    60. (requires lru_maintainer)
    61. - warm_lru_pct: pct of slab memory to reserve for warm lru.
    62. (requires lru_maintainer)
    63. - hot_max_factor: items idle > cold lru age * drop from hot lru.
    64. - warm_max_factor: items idle > cold lru age * this drop from warm.
    65. - temporary_ttl: TTL's below get separate LRU, can't be evicted.
    66. (requires lru_maintainer)
    67. - idle_timeout: timeout for idle connections
    68. - slab_chunk_max: (EXPERIMENTAL) maximum slab size. use extreme care.
    69. - watcher_logbuf_size: size in kilobytes of per-watcher write buffer.
    70. - worker_logbuf_size: size in kilobytes of per-worker-thread buffer
    71. read by background thread, then written to watchers.
    72. - track_sizes: enable dynamic reports for 'stats sizes' command.
    73. - no_inline_ascii_resp: save up to 24 bytes per item.
    74. small perf hit in ASCII, no perf difference in
    75. binary protocol. speeds up all sets.
    76. - no_hashexpand: disables hash table expansion (dangerous)
    77. - modern: enables options which will be default in future.
    78. currently: nothing
    79. - no_modern: uses defaults of previous major version (1.4.x)

    常用参数:

    • -d是启动一个守护进程;
    • -m是分配给Memcache使用的内存数量,单位是MB;
    • -u是运行Memcache的用户;
    • -l是监听的服务器IP地址,可以有多个地址;
    • -p是设置Memcache监听的端口,,最好是1024以上的端口;
    • -c是最大运行的并发连接数,默认是1024;
    • -t是线程数,默认为4;
    • -P是设置保存Memcache的pid文件。

    参考文章:

    • https://github.com/memcached/memcached/wiki/Overview
    • https://github.com/memcached/memcached/wiki/Install
    • http://www.runoob.com/memcached/memcached-tutorial.html