国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Java javaTutorial Detailed explanation of common memcached commands in Java

Detailed explanation of common memcached commands in Java

Oct 15, 2017 pm 03:14 PM
java memcached Order

This article mainly introduces the common commands of memcached. The editor thinks it is quite good. Now I will share it with you and give you a reference. Let’s follow the editor and take a look.

1. Common parameters for starting Memcache


-p <num>   設置TCP端口號(默認設置為: 11211)
-U <num>   UDP監(jiān)聽端口(默認: 11211, 0 時關閉) 
-l <ip_addr> 綁定地址(默認:所有都允許,無論內外網或者本機更換IP,有安全隱患,若設置為127.0.0.1就只能本機訪問)
-c <num>   max simultaneous connections (default: 1024)
-d      以daemon方式運行
-u <username> 綁定使用指定用于運行進程<username>
-m <num>   允許最大內存用量,單位M (默認: 64 MB)
-P <file>   將PID寫入文件<file>,這樣可以使得后邊進行快速進程終止, 需要與-d 一起使用

For more, you 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= auto
net start Memcached

2. Connection and exit


##

telnet 127.0.0.1 11211
quit

3. Basic commands

Five basic memcached commands perform the simplest operations. These commands and operations include:

  • set

  • ##add
  • replace
  • get
  • delete
  • The first three commands are used to operate key-value pairs stored in memcached Standard modification commands. They are both very simple and easy to use, and all use the syntax shown below:

command <key> <flags> <expiration time> <bytes>
<value>

The parameter description is as follows:


command set/add/ replace

key key is used to look up cached values

flags Can include integer parameters of key-value pairs, which are used by the client to store additional information about the key-value pair
expiration time Save the key-value pair in the cache Length of time (in seconds, 0 means forever)
bytes The byte points stored in the cache
value The value stored (always on the second line)

Now, let’s take a look Practical usage of these commands.


3.1 The set


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 whose key is userId and whose value is 12345. and setting the expiration time to 0, this will notify memcached that you want this value stored in the cache until you delete it.


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 be the same and you will get the response NOT_STORED.


The following 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 will replace 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 the 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 similar syntax, as shown below:


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 a previously added key-value pair. You'll use get to perform most retrieval operations.

Here 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, 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 deletes any existing value in memcached. You would call delete with a key, and if the key exists in the cache, delete the value. If it does not exist, a NOT_FOUND message is returned.


The following 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

The 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 a new value if the value has already been updated. Let's look at these commands individually.


3.6 gets


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


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 returns an additional value—in this case, the integer value 4—that identifies the name/value pair. If another set command is executed 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

您看到 gets 返回的值了嗎?它已經更新為 5。您每次修改名稱/值對時,該值都會發(fā)生更改。

3.7 cas

cas(check 和 set)是一個非常便捷的 memcached 命令,用于設置名稱/值對的值(如果該名稱/值對在您上次執(zhí)行 gets 后沒有更新過)。它使用與 set 命令相類似的語法,但包括一個額外的值:gets 返回的額外值。

注意以下使用 cas 命令的交互:


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

如您所見,我使用額外的整型值 6 來調用 gets 命令,并且操作運行非常順序?,F在,我們來看看中的一系列命令:

使用舊版本指示符的 cas 命令


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

注意,我并未使用 gets 最近返回的整型值,并且 cas 命令返回 EXISTS 值以示失敗。從本質上說,同時使用gets 和cas 命令可以防止您使用自上次讀取后經過更新的名稱/值對。

緩存管理命令

最后兩個 memcached 命令用于監(jiān)控和清理 memcached 實例。它們是 stats 和 flush_all 命令。

3.8 stats

stats 命令的功能正如其名:轉儲所連接的 memcached 實例的當前統計數據。在下例中,執(zhí)行 stats 命令顯示了關于當前 memcached 實例的信息:


STAT pid 22459               進程ID
STAT uptime 1027046            服務器運行秒數
STAT time 1273043062            服務器當前unix時間戳
STAT version 1.4.4             服務器版本
STAT libevent 2.0.21-stable
STAT pointer_size 64            操作系統字大小(這臺服務器是64位的)
STAT rusage_user 0.040000         進程累計用戶時間
STAT rusage_system 0.260000        進程累計系統時間
STAT curr_connections 10          當前打開連接數
STAT total_connections 82         曾打開的連接總數
STAT connection_structures 13       服務器分配的連接結構數
STAT reserved_fds 20
STAT cmd_get 54              執(zhí)行get命令總數
STAT cmd_set 34              執(zhí)行set命令總數
STAT cmd_flush 3              指向flush_all命令總數
STAT get_hits 9              get命中次數
STAT get_misses 45             get未命中次數
STAT delete_misses 5            delete未命中次數
STAT delete_hits 1             delete命中次數
STAT incr_misses 0             incr未命中次數
STAT incr_hits 0              incr命中次數
STAT decr_misses 0             decr未命中次數
STAT decr_hits 0              decr命中次數
STAT cas_misses 0             cas未命中次數
STAT cas_hits 0              cas命中次數
STAT cas_badval 0             使用擦拭次數
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 15785           讀取字節(jié)總數
STAT bytes_written 15222          寫入字節(jié)總數
STAT limit_maxbytes 67108864        分配的內存數(字節(jié))
STAT accepting_conns 1           目前接受的鏈接數
STAT listen_disabled_num 0        
STAT time_in_listen_disabled_us 0
STAT threads 4               線程數
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                存儲item字節(jié)數
STAT curr_items 0             item個數
STAT total_items 34            item總數
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evictions 0              為獲取空間刪除item的總數
STAT reclaimed 0
STAT crawler_reclaimed 0
STAT crawler_items_checked 0
STAT lrutail_reflocked 0

此處的大多數輸出都非常容易理解。我們先來看看輸出,然后再使用新的鍵來運行一些 set 命令,并再次運行stats 命令,注意發(fā)生了哪些變化。

stats items

執(zhí)行stats items,可以看到STAT items行,如果memcached存儲內容很多,那么這里也會列出很多的STAT items行。


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:由stats items返回的結果(STAT items后面的數字)決定的

limit_num:返回的記錄數,0表示返回所有記錄

通過stats items、stats cachedump slab_id limit_num配合get命令可以遍歷memcached的記錄。


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 顯示各個slab的信息,包括chunk的大小、數目、使用情況等


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 輸出所有item的大小和個數

STAT 96 3

stats reset 清空統計數據

stats reset

RESET

3.9 flush_all

flush_all 是最后一個要介紹的命令。這個最簡單的命令僅用于清理緩存中的所有名稱/值對。如果您需要將緩存重置到干凈的狀態(tài),則 flush_all 能提供很大的用處。下面是一個使用 flush_all 的例子:


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

追加與清除命令

3.10 append

append 將數據追加到當前緩存數據的之后,當緩存數據存在時才存儲。


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 將數據追加到當前緩存數據的之前,當緩存數據存在時才存儲。


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
name_wayne173
END

memcached還有很多命令,比如對于存儲為數字型的可以通過incr/decr命令進行增減操作等等,這里只列出開發(fā)和運維中經常使用的命令,其他的不再一一舉例說明。

The above is the detailed content of Detailed explanation of common memcached commands in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to iterate over a Map in Java? How to iterate over a Map in Java? Jul 13, 2025 am 02:54 AM

There are three common methods to traverse Map in Java: 1. Use entrySet to obtain keys and values at the same time, which is suitable for most scenarios; 2. Use keySet or values to traverse keys or values respectively; 3. Use Java8's forEach to simplify the code structure. entrySet returns a Set set containing all key-value pairs, and each loop gets the Map.Entry object, suitable for frequent access to keys and values; if only keys or values are required, you can call keySet() or values() respectively, or you can get the value through map.get(key) when traversing the keys; Java 8 can use forEach((key,value)-&gt

Comparable vs Comparator in Java Comparable vs Comparator in Java Jul 13, 2025 am 02:31 AM

In Java, Comparable is used to define default sorting rules internally, and Comparator is used to define multiple sorting logic externally. 1.Comparable is an interface implemented by the class itself. It defines the natural order by rewriting the compareTo() method. It is suitable for classes with fixed and most commonly used sorting methods, such as String or Integer. 2. Comparator is an externally defined functional interface, implemented through the compare() method, suitable for situations where multiple sorting methods are required for the same class, the class source code cannot be modified, or the sorting logic is often changed. The difference between the two is that Comparable can only define a sorting logic and needs to modify the class itself, while Compar

How to handle character encoding issues in Java? How to handle character encoding issues in Java? Jul 13, 2025 am 02:46 AM

To deal with character encoding problems in Java, the key is to clearly specify the encoding used at each step. 1. Always specify encoding when reading and writing text, use InputStreamReader and OutputStreamWriter and pass in an explicit character set to avoid relying on system default encoding. 2. Make sure both ends are consistent when processing strings on the network boundary, set the correct Content-Type header and explicitly specify the encoding with the library. 3. Use String.getBytes() and newString(byte[]) with caution, and always manually specify StandardCharsets.UTF_8 to avoid data corruption caused by platform differences. In short, by

JavaScript Data Types: Primitive vs Reference JavaScript Data Types: Primitive vs Reference Jul 13, 2025 am 02:43 AM

JavaScript data types are divided into primitive types and reference types. Primitive types include string, number, boolean, null, undefined, and symbol. The values are immutable and copies are copied when assigning values, so they do not affect each other; reference types such as objects, arrays and functions store memory addresses, and variables pointing to the same object will affect each other. Typeof and instanceof can be used to determine types, but pay attention to the historical issues of typeofnull. Understanding these two types of differences can help write more stable and reliable code.

How does a HashMap work internally in Java? How does a HashMap work internally in Java? Jul 15, 2025 am 03:10 AM

HashMap implements key-value pair storage through hash tables in Java, and its core lies in quickly positioning data locations. 1. First use the hashCode() method of the key to generate a hash value and convert it into an array index through bit operations; 2. Different objects may generate the same hash value, resulting in conflicts. At this time, the node is mounted in the form of a linked list. After JDK8, the linked list is too long (default length 8) and it will be converted to a red and black tree to improve efficiency; 3. When using a custom class as a key, the equals() and hashCode() methods must be rewritten; 4. HashMap dynamically expands capacity. When the number of elements exceeds the capacity and multiplies by the load factor (default 0.75), expand and rehash; 5. HashMap is not thread-safe, and Concu should be used in multithreaded

What is the 'static' keyword in Java? What is the 'static' keyword in Java? Jul 13, 2025 am 02:51 AM

InJava,thestatickeywordmeansamemberbelongstotheclassitself,nottoinstances.Staticvariablesaresharedacrossallinstancesandaccessedwithoutobjectcreation,usefulforglobaltrackingorconstants.Staticmethodsoperateattheclasslevel,cannotaccessnon-staticmembers,

Using std::chrono in C Using std::chrono in C Jul 15, 2025 am 01:30 AM

std::chrono is used in C to process time, including obtaining the current time, measuring execution time, operation time point and duration, and formatting analysis time. 1. Use std::chrono::system_clock::now() to obtain the current time, which can be converted into a readable string, but the system clock may not be monotonous; 2. Use std::chrono::steady_clock to measure the execution time to ensure monotony, and convert it into milliseconds, seconds and other units through duration_cast; 3. Time point (time_point) and duration (duration) can be interoperable, but attention should be paid to unit compatibility and clock epoch (epoch)

What is a ReentrantLock in Java? What is a ReentrantLock in Java? Jul 13, 2025 am 02:14 AM

ReentrantLock provides more flexible thread control in Java than synchronized. 1. It supports non-blocking acquisition locks (tryLock()), lock acquisition with timeout (tryLock(longtimeout, TimeUnitunit)) and interruptible wait locks; 2. Allows fair locks to avoid thread hunger; 3. Supports multiple condition variables to achieve a more refined wait/notification mechanism; 4. Need to manually release the lock, unlock() must be called in finally blocks to avoid resource leakage; 5. It is suitable for scenarios that require advanced synchronization control, such as custom synchronization tools or complex concurrent structures, but synchro is still recommended for simple mutual exclusion requirements.

See all articles