Home  >  Article  >  Java  >  Detailed explanation of common memcached commands in Java

Detailed explanation of common memcached commands in Java

ringa_lee
ringa_leeOriginal
2017-10-15 15:14:261440browse

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监听端口(默认: 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。您每次修改名称/值对时,该值都会发生更改。

3.7 cas

cas(check 和 set)是一个非常便捷的 memcached 命令,用于设置名称/值对的值(如果该名称/值对在您上次执行 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 命令,并且操作运行非常顺序。现在,我们来看看中的一系列命令:

使用旧版本指示符的 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 命令用于监控和清理 memcached 实例。它们是 stats 和 flush_all 命令。

3.8 stats

stats 命令的功能正如其名:转储所连接的 memcached 实例的当前统计数据。在下例中,执行 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              执行get命令总数
STAT cmd_set 34              执行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           读取字节总数
STAT bytes_written 15222          写入字节总数
STAT limit_maxbytes 67108864        分配的内存数(字节)
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字节数
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 命令,注意发生了哪些变化。

stats items

执行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 是最后一个要介绍的命令。这个最简单的命令仅用于清理缓存中的所有名称/值对。如果您需要将缓存重置到干净的状态,则 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命令进行增减操作等等,这里只列出开发和运维中经常使用的命令,其他的不再一一举例说明。

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:
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