memcached命令

来源:互联网 发布:电脑锣编程视频 编辑:程序博客网 时间:2024/04/30 05:40
    • Standard Protocol
      • No Reply
    • Storage Commands
      • set
      • add
      • replace
      • append
      • prepend
      • cas
    • Retrieval Commands
      • get
      • gets
    • delete
    • incr/decr
    • Statistics
      • stats
      • stats items
      • stats slabs
      • stats sizes
    • flush_all

Memcached handles a small number of basic commands.

Full documentation can be found in the Protocol Documentation.

Standard Protocol

The "standard protocol stuff" of memcached involves running a command against an "item". An item consists of:

  • A key (arbitrary string up to 250 bytes in length. No space or newlines for ASCII mode)
  • A 32bit "flag" value
  • An expiration time, in seconds. Can be up to 30 days. After 30 days, is treated as a unix timestamp of an exact date.
  • A 64bit "CAS" value, which is kept unique.
  • Arbitrary data

CAS is optional (can be disabled entirely with -C, and there are more fields that internally make up an item, but these are what your client interacts with.

No Reply

Most ASCII commands allow a "noreply" version. One should not normally use this with the ASCII protocol, as it is impossible to align errors with requests. The intent is to avoid having to wait for a return packet after executing a mutation command (such as a set or add).

The binary protocol properly implements noreply (quiet) statements. If you have a client which supports or uses the binary protocol, odds are good you may take advantage of this.

Storage Commands

set

Most common command. Store this data, possibly overwriting any existing data. New items are at the top of the LRU.

add

Store this data, only if it does not already exist. New items are at the top of the LRU. If an item already exists and an add fails, it promotes the item to the front of the LRU anyway.

replace

Store this data, but only if the data already exists. Almost never used, and exists for protocol completeness (set, add, replace, etc)

append

Add this data after the last byte in an existing item. This does not allow you to extend past the item limit. Useful for managing lists.

prepend

Same as append, but adding new data before existing data.

cas

Check And Set (or Compare And Swap). An operation that stores data, but only if no one else has updated the data since you read it last. Useful for resolving race conditions on updating cache data.

Retrieval Commands

get

Command for retrieving data. Takes one or more keys and returns all found items.

gets

An alternative get command for using with CAS. Returns a CAS identifier (a unique 64bit number) with the item. Return this value with the cascommand. If the item's CAS value has changed since you gets'ed it, it will not be stored.

delete

Removes an item from the cache, if it exists.

incr/decr

Increment and Decrement. If an item stored is the string representation of a 64bit integer, you may run incr or decr commands to modify that number. You may only incr by positive values, or decr by positive values. They does not accept negative values.

If a value does not already exist, incr/decr will fail.

Statistics

There're a handful of commands that return counters and settings of the memcached server. These can be inspected via a large array of tools or simply by telnet or netcat. These are further explained in the protocol docs.

stats

ye 'ole basic stats command.

stats items

Returns some information, broken down by slab, about items stored in memcached.

stats slabs

Returns more information, broken down by slab, about items stored in memcached. More centered to performance of a slab rather than counts of particular items.

stats sizes

A special command that shows you how items would be distributed if slabs were broken into 32byte buckets instead of your current number of slabs. Useful for determining how efficient your slab sizing is.

WARNING this is a development command. As of 1.4 it is still the only command which will lock your memcached instance for some time. If you have many millions of stored items, it can become unresponsive for several minutes. Run this at your own risk. It is roadmapped to either make this feature optional or at least speed it up.

flush_all

Invalidate all existing cache items. Optionally takes a parameter, which means to invalidate all items after N seconds have passed.

This command does not pause the server, as it returns immediately. It does not free up or flush memory at all, it just causes all items to expire.


Connection Limit

By default the max number of concurrent connections is set to 1024. Configuring this correctly is important. Extra connections to memcached may hang while waiting for slots to free up. You may detect if your instance has been running out of connections by issuing a statscommand and looking at "listen_disabled_num". That value should be zero or close to zero.

Memcached can scale with a large number of connections very simply. The amount of memory overhead per connection is low (even lower if the connection is idle), so don't sweat setting it very high.

Lets say you have 5 webservers, each running apache. Each apache process has a MaxClients setting of 12. This means that the maximum number of concurrent connections you may receive is 5 x 12 (60). Always leave a few extra slots open if you can, for administrative tasks, adding more webservers, crons/scripts/etc.

Threading

Threading is used to scale memcached across CPU's. Its model is by "worker threads", meaning that each thread makes itself available to process as much as possible. Since using libevent allows good scalability with concurrent connections, each thread is able to handle many clients.

This is different from some webservers, such as apache, which use one process or one thread per active client connection. Since memcached is highly efficient, low numbers of threads are fine. In webserver land, it means it's more like nginx than apache.

By default 4 threads are allocated. Unless you are running memcached extremely hard, you should not set this number to be any higher. Setting it to very large values (80+) will not make it run any faster.

Inspecting Running Configuration

$ echo "stats settings" | nc localhost 11211STAT maxbytes 67108864STAT maxconns 1024STAT tcpport 11211STAT udpport 11211STAT inter NULLSTAT verbosity 0STAT oldest 0STAT evictions onSTAT domain_socket NULLSTAT umask 700STAT growth_factor 1.25STAT chunk_size 48STAT num_threads 4STAT stat_key_prefix :STAT detail_enabled noSTAT reqs_per_event 20STAT cas_enabled yesSTAT tcp_backlog 1024STAT binding_protocol auto-negotiateSTAT auth_enabled_sasl noSTAT item_size_max 1048576END

0 0
原创粉丝点击