Memcached commonly used related instructions

1. Common parameters for starting Memcache
-p <num> Set the TCP port number (default: 11211)
-U <num> UDP listening port (default: 11211, 0 is closed)
-l <ip_addr> Binding address (default: All are allowed, no matter whether the IP is changed on the internal or external network or the local machine, there are security risks. If it is set to 127.0.0.1, it can only be accessed locally)
-c <num> max simultaneous connections (default: 1024)
-d Run in daemon
mode- u <username> bind use specified for running process <username>
-m <num> allow the maximum amount of memory in M ​​(default: 64 MB)
-P <file> write the PID to the file <file>, which can make For quick process termination later, it needs to be used together with -d

2. Connect and exit
telnet 127.0.0.1 11211
quit

3. Basic commands
set
add
replace
get
delete

The first three commands are the standard for manipulating key-value pairs stored in memcached Modify the command. They are all very simple to use, and they all use the syntax shown below:

command <key> <flags> <


The parameters are described as follows:
command set/add/replace
key key used to look up the cache value
flags an integer parameter that can include key-value pairs, which the client uses to store additional information about key-
value pairs Length of time (in seconds, 0 means forever)
bytes Bytes to store in the cache point
value The value to store (always on the second line)

Now, let's look at these commands in action.

3.1 set
The set command is used to add new key-value pairs to the cache. If the key already exists, the previous value will be replaced.
Note the following interaction, which uses the set command:

set userId 0 0 5
12345
STORED

If the key-value pair is correctly set using the set command, the server will respond with the word STORED. This example adds a key-value pair to the cache with a key of userId and a value of 12345. and set the expiration time to 0, which will inform memcached that you want this value to be stored in the cache until it is deleted.

3.2 add
The add command will add a key-value pair to the cache only if the key does not exist in the cache. If the key already exists in the cache, the previous value will still remain the same and you will get the response NOT_STORED.
Here is the standard interaction using the add command:

set userId 0 0 5
12345
STORED
add userId 0 0 5
55555
NOT_STORED
add companyId 0 0 3
564
STORED

3.3 replace
The replace command replaces a key in the cache only if the key already exists. If the key does not exist in the cache, you will receive a NOT_STORED response from the memcached server.
The following is a standard interaction using the replace command:

replace accountId 0 0 5
67890
NOT_STORED
set accountId 0 0 5
67890
STORED
replace accountId 0 0 5
55555
STORED


The last two basic commands are get and delete. These commands are fairly easy to understand and use a similar syntax, as follows:

command <key>

Let's look at the application of these commands.

3.4 get
The get command is used to retrieve the value associated with the previously added key-value pair. You will use get to perform most retrieval operations.
The following is a typical interaction using the get command:

set userId 0 0 5
12345
STORED
get userId
VALUE userId 0 5
12345
END
get bob
END

As you can see, the get command is fairly simple. You call get with a key, and if the key exists in the cache, it returns the corresponding value. If not present, nothing is returned.

3.5 delete
The last basic command is delete. The delete command is used to delete any existing value in memcached. You would call delete with a key, and if the key exists in the cache, delete the value. If not present, a NOT_FOUND message is returned.

Here is the client-server interaction using the delete command:

set userId 0 0 5
98765
STORED
delete bob
NOT_FOUND
delete userId
DELETED
get userId
END


Two high-level commands that can be used in memcached are gets and cas. The gets and cas commands need to be used together. You will use these two commands to ensure that an existing name/value pair is not set to the new value (if the value has already been updated). Let's look at these commands individually.

3.6 gets
The gets command functions like the basic get command. The difference between the two commands is that gets returns slightly more information: 64-bit integer values ​​are very much like "version" identifiers for name/value pairs.
Here is the client-server interaction using the gets command:

set userId 0 0 5
12345
STORED
get userId
VALUE userId 0 5
12345
END
gets userId
VALUE userId 0 5 4
12345
END

Consider the difference between the get and gets commands. The gets command will return an extra value—in this case the integer value 4—that identifies the name/value pair. If another set command is performed on this name/value pair, the extra value returned by gets will change to indicate that the name/value pair has been updated. An example is shown:

set userId 0 0 5
33333
STORED
gets userId
VALUE userId 0 5 5
33333
END

Do you see the value returned by gets? It has been updated to 5. The value changes every time you modify the name/value pair.

3.7 cas
cas (check and set) is a very convenient memcached command for setting the value of a name/value pair (if the name/value pair has not been updated since your last gets). It uses a similar syntax to the set command, but includes an extra value: the extra value returned by gets.
Note the following interaction using the cas command:

set userId 0 0 5
55555
STORED
gets userId
VALUE userId 0 5 6
55555
END
cas userId 0 0 5 6
33333
STORED

As you can see I call the gets command with an extra integer value of 6, And the operations run very sequentially. Now, let's take a look at the series of commands in:
cas command with old version indicator

set userId 0 0 5
55555
STORED
gets userId
VALUE userId 0 5 8
55555
END
cas userId 0 0 5 6
33333
EXISTS

Note that I'm not using the most recent integer value returned by gets, and the cas command returns an EXISTS value for failure. Essentially, using the gets and cas commands together prevents you from using name/value pairs that have been updated since the last read.

Cache Management Commands
The last two memcached commands are used to monitor and clean up memcached instances. They are the stats and flush_all commands.

3.8 stats
The stats command does exactly what it says: dumps the current statistics of the connected memcached instance. In the following example, executing the stats command displays information about the current memcached instance:

STAT pid 22459 Process ID
STAT uptime 1027046 Server uptime seconds
STAT time 1273043062 Server current unix timestamp
STAT version 1.4.4 Server version
STAT libevent 2.0.21-stable
STAT pointer_size 64 Operating system word size (this server is 64-bit)
STAT rusage_user 0.040000 Process accumulated user time
STAT rusage_system 0.260000 Process accumulated system time
STAT curr_connections 10 The current number of open connections
STAT total_connections 82 The total number of open connections
STAT connection_structures 13 The number of connection structures allocated by the server
STAT reserved_fds 20
STAT cmd_get 54 The total number of get commands executed
STAT cmd_set 34 The total number of set commands executed
STAT cmd_flush 3 The total number of flush_all commands
STAT get_hits 9 get hits
STAT get_misses 45 get misses
STAT delete_misses 5 delete misses
STAT delete_hits 1 delete hits
STAT incr_misses 0 incr misses
STAT incr_hits 0 incr hits
STAT decr_misses 0 decr misses
STAT decr_hits 0 decr hits
STAT cas_misses 0 cas misses STAT cas_hits 0
cas hits STAT cas_badval
0 usage wipes Total number of sections STAT bytes_written 15222 Total number of bytes written STAT limit_maxbytes 67108864 Number of allocated memory (bytes) STAT accepting_conns 1 Number of currently accepted connections STAT listen_disabled_num 0                STAT time_in_listen_disabled_us 0










STAT 4 number of threads
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT malloc_fails 0
STAT conn_yields 0
STAT bytes 0 number of stored item bytes
STAT curr_items 0 item number
STAT total_items 34 total item count
STAT expired_unfetched 0
STAT evicted_unfetched 0 STAT
threads 0 is the total number of items removed to gain space STAT reclaimed 0 STAT crawler_reclaimed 0
STAT crawler_items_checked 0 STAT lrutail_reflocked 0 Most of the output here is pretty easy to understand. Let's look at the output first, then run some set commands with the new keys, and run the stats command again, noting what changes.






stats items
Execute stats items, you can see the STAT items line, if memcached stores a lot of content, then a lot of STAT items lines will also be listed here.

STAT items: 1: number 3
STAT items: 1: age 1698
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 0
STAT items:1:expired_unfetched 0
STAT items:1:evicted_unfetched 0
STAT items:1:crawler_reclaimed 0
STAT items:1:crawler_items_checked 0
STAT items:1:lrutail_reflocked 0
END

stats cachedump slabs_id limit_num
slabs_id:by stats The result returned by items (the number after STAT items) determines
limit_num: the number of records returned, 0 means to return all records
You can traverse memcached records through stats items, stats cachedump slab_id limit_num and the get command.

stats cachedump 1 0
ITEM userId [5 b; 1467903379 s]
ITEM accountId [5 b; 1467903379 s]
ITEM companyId [3 b; 1467903379 s]
END
stats cachedump 1 2
ITEM userId [5 b; 1467903379 s]
ITEM accountId [5 b ] ; 1467903379 s]
END

stats slabs Displays information about each slab, including chunk size, number, usage, etc.

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 3
STAT 1 :free_chunks 10919
STAT 1:free_chunks_end 0
STAT 1:mem_requested 232
STAT 1:get_hits 9
STAT 1:cmd_set 14
STAT 1:delete_hits 1
STAT 1:incr_hits 0
STAT 1:decr_hits 0
STAT 1:cas_hits 0
STAT 1:cas_badval 0
STAT 1:touch_hits 0
STAT active_slabs 1
STAT total_malloced 1048512

stats sizes Output the size and number of all items

STAT 96 3

stats reset clears statistics

stats reset
RESET

3.9 flush_all
flush_all is the last command to be introduced. This simplest command just cleans the cache of all name/value pairs. flush_all can be very useful if you need to reset the cache to a clean state. Here is an example of using flush_all:

set userId 0 0 5
55555
STORED
get userId
VALUE userId 0 5
55555
END
flush_all
OK
get userId
END

append and clear commands

3.10 append
append appends data to the current cached data and stores it only when the cached data exists.

set username 0 0 8
wayne173
STORED
get username
VALUE username 0 8
wayne173
END
append username 0 0 5
_ages
STORED
get username
VALUE username 0 13
wayne173_ages
END

3.11 prepend
prepend appends data to the current cached data before storing it when the cached data exists .

set username 0 0 8
wayne173
STORED
get username
VALUE username 0 8
wayne173
END
prepend username 0 0 5
name_
STORED
get username
VALUE username 0 13
There are many commands in name_wayne173
END

memcached. For example, the incr/decr command can be used to increase or decrease the stored digital data. Only the commands frequently used in development and operation and maintenance are listed here, and the others will not be explained one by one. .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326213944&siteId=291194637