Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Common commands of memcached

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "the common commands of memcached". In the daily operation, I believe that many people have doubts about the common commands of memcached. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "common commands of memcached"! Next, please follow the editor to study!

1. Common parameters for starting Memcache

-p set TCP port number (default: 11211)-U UDP listening port (default: 11211, closed when 0)-l bind address (default: all are allowed, no matter whether the internal or external network or local IP is changed, there is a security risk. If it is set to 127.0.0.1, it can only be accessed locally)-c max simultaneous connections (default: 1024)-d runs in daemon mode-u binding uses the specified maximum amount of memory allowed to run the process-m, and the unit M (default: 64 MB)-P writes PID to the file, which allows for quick process termination later, which needs to be used with-d

More users can use memcached-h

Under linux:. / usr/local/bin/memcached-d-u root-l 192.168.1.197-m 2048-p 12121

Under window: d:\ App_Serv\ memcached\ memcached.exe-d RunService-l 127.0.0.1-p 11211-m 500

Run after registering as a service under windows:

Sc.exe create Memcached_srv binpath= "d:\ App_Serv\ memcached\ memcached.exe-d RunService-p 11211-m 500" start= autonet start Memcached

2. Connect and exit

Telnet 127.0.0.1 11211quit

3. Basic commands

The five basic memcached commands perform the simplest operation. These commands and actions include:

Set

Add

Replace

Get

Delete

The first three commands are standard modification commands for manipulating key-value pairs stored in memcached. They are all very easy to use and use the syntax shown below:

Command

The parameters are described as follows:

Command set/add/replace

Key key is used to find cache values

Flags can include integer parameters for key-value pairs, which clients use to store additional information about key-value pairs

The length of time that expiration time saves key-value pairs in the cache (in seconds, 0 means forever)

Word nodes stored in the cache by bytes

The value stored by value (always on the second line)

Now, let's look at the actual use of these commands.

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.

Notice the following interaction, which uses the set command:

Set userId 0 0 512345STORED

If the key-value pair is set correctly using the set command, the server responds with the word STORED. This example adds a key-value pair to the cache whose key is userId and whose value is 12345. And set the expiration time to 0, which informs memcached that you want to store this value in the cache until it is deleted.

3.2 add

The add command adds 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 remain the same, and you will get the response NOT_STORED.

The following is a standard interaction using the add command:

Set userId 0 0 512345STOREDadd userId 0 0 555555NOT_STOREDadd companyId 0 0 3564STORED

3.3 replace

The replace command replaces the 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 567890NOT_STOREDset accountId 0 0 567890STOREDreplace accountId 0 0 555555STORED

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

Command

Let's take a look at the application of these commands.

3.4 get

The get command is used to retrieve values related to 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 512345STOREDget userIdVALUE userId 0 512345ENDget bobEND

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

3.5 delete

The last basic command is delete. The delete command is used to delete any existing values in memcached. You will call delete with a key and delete the value if the key exists in the cache. If it does not exist, a NOT_FOUND message is returned.

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

Set userId 0 0 598765STOREDdelete bobNOT_FOUNDdelete userIdDELETEDget userIdEND

The two advanced 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 the existing name / value pair is not set to the new value, if the value has been updated. Let's take a look at these commands separately.

3.6 gets

The function of the gets command is similar to the basic get command. The difference between the two commands is that gets returns a little more information: the 64-bit integer value is very much like the "version" identifier of the name / value pair.

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

Set userId 0 0 512345STOREDget userIdVALUE userId 0 512345ENDgets userIdVALUE userId 0 5 412345END

Consider the difference between the get and gets commands. The gets command returns an additional value-in this case, the integer value 4, which identifies the name / value pair. If you execute another set command 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 533333STOREDgets userIdVALUE userId 0 5 533333END

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

3.7 cas

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

Note the following interaction using the cas command:

Set userId 0 0 555555STOREDgets userIdVALUE userId 0 5 655555ENDcas userId 0 0 5 633333STORED

As you can see, I use the extra integer value of 6 to invoke the gets command, and the operations run sequentially. Now, let's look at a series of commands in:

Cas command that uses the old version indicator

Set userId 0 0 555555STOREDgets userIdVALUE userId 0 5 855555ENDcas userId 0 0 5 633333EXISTS

Notice that I did not use the integer value recently returned by gets, and the cas command returned a value of EXISTS as a sign of failure. In essence, using both the gets and cas commands prevents you from using name / value pairs that have been updated since the last read.

Cache management command

The last two memcached commands are used to monitor and clean up the memcached instance. They are the stats and flush_all commands.

3.8 stats

The function of the stats command is just like its name: dump 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 IDSTAT uptime 1027046 server run seconds STAT time 1273043062 server current unix timestamp STAT version 1.4.4 server version STAT libevent 2.0.21-stableSTAT pointer_size 64 operating system word size (this server is 64-bit) STAT rusage_user 0.040000 process cumulative user time STAT rusage_system 0.260000 process cumulative system time STAT curr_connections 10 currently open connections STAT total_connections 82 total number of connections opened STAT connection_structures 13 server assigned connection structures STAT reserved_fds 20STAT cmd_get 54 total number of get commands executed STAT cmd_set 34 total number of set commands STAT cmd_flush 3 Point to 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 use wipe times STAT touch_hits 0STAT touch_misses 0STAT auth_cmds 0STAT auth_errors 0STAT bytes_read 15785 read total bytes STAT bytes_written 15222 write Total number of bytes STAT limit_maxbytes 67108864 allocated memory (bytes) number of links currently accepted by STAT accepting_conns 1 number of STAT listen_disabled_num 0STAT time_in_listen_disabled_us 0STAT threads 4 threads STAT conn_yields 0STAT hash_power_level 16STAT hash_bytes 524288STAT hash_is_expanding 0STAT malloc_fails 0STAT conn_yields 0STAT bytes 0 Storage item bytes STAT Curr_items 0 item number STAT total_items 34 item Total STAT expired_unfetched 0STAT evicted_unfetched 0STAT evictions 0 is the total number of acquired space deleted item STAT reclaimed 0STAT crawler_reclaimed 0STAT crawler_items_checked 0STAT lrutail_reflocked 0

Most of the output here is very easy to understand. Let's take a look at the output, then use the new keys to run some set commands, and run the stats command again to notice what has changed.

Stats items

When you execute stats items, you can see the STAT items lines, and if the memcached stores a lot of content, then there are many STAT items lines listed here.

STAT items:1:number 3STAT items:1:age 1698STAT items:1:evicted 0STAT items:1:evicted_nonzero 0STAT items:1:evicted_time 0STAT items:1:outofmemory 0STAT items:1:tailrepairs 0STAT items:1:reclaimed 0STAT items:1:expired_unfetched 0STAT items:1:evicted_unfetched 0STAT items:1:crawler_reclaimed 0STAT items:1:crawler_items_checked 0STAT items:1:lrutail_reflocked 0END

Stats cachedump slabs_id limit_num

Slabs_id: determined by the result returned by stats items (the number after STAT items)

Limit_num: the number of records returned. 0 means all records are returned.

The records of memcached can be traversed through stats items, stats cachedump slab_id limit_num and get command.

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

Stats slabs displays information about each slab, including the size, number, usage of chunk, etc.

STAT 1:chunk_size 96STAT 1:chunks_per_page 10922STAT 1:total_pages 1STAT 1:total_chunks 10922STAT 1:used_chunks 3STAT 1:free_chunks 10919STAT 1:free_chunks_end 0STAT 1:mem_requested 232STAT 1:get_hits 9STAT 1:cmd_set 14STAT 1:delete_hits 1STAT 1:incr_hits 0STAT 1:decr_hits 0STAT 1:cas_hits 0STAT 1:cas_badval 0STAT 1:touch_hits 0STAT active_slabs 1STAT total_malloced 1048512

Stats sizes outputs the size and number of all item

STAT 96 3

Stats reset emptying statistics

Stats reset

RESET

3.9 flush_all

Flush_all is the last command to be introduced. This simplest command is only used to clean up all name / value pairs in the cache. Flush_all can be of great use if you need to reset the cache to a clean state. Here is an example of using flush_all:

Set userId 0 0 555555STOREDget userIdVALUE userId 0 555555ENDflush_allOKget userIdEND

Append and clear command

3.10 append

Append appends the data to the currently cached data and stores it when the cached data exists.

Set username 0 0 8wayne173STOREDget usernameVALUE username 0 8wayne173ENDappend username 0 0 5_agesSTOREDget usernameVALUE username 0 13wayne173_agesEND

3.11 prepend

Prepend appends the data to the currently cached data and stores it when the cached data exists.

Set username 0 0 8wayne173STOREDget usernameVALUE username 0 8wayne173ENDprepend username 0 5name_STOREDget usernameVALUE username 0 13name_wayne173END so far, the study of "common commands for memcached" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report