Memcached
Memcached
key-value dictionary of strings, objects, etc., stored in the memory, resulting from
now being used by Netlog, Facebook, Flickr, Wikipedia, Twitter, and YouTube among
others.
It is open source.
Memcached is not −
a persistent data store
a database
application-specific
commands −
below. This command shows that Memcached is running on the default port 11211.
To run Memcached server on a different port, execute the command given below. This
command starts the server on the TCP port 11111 and listens on the UDP port 11111
as a daemon process.
You can run multiple instances of Memcached server through a single installation.
PORT names.
Syntax
The basic syntax of Memcached telnet command is as shown below −
Here, HOST and PORT are machine IP and port number respectively, on which the
Example
The following example shows how to connect to a Memcached server and execute a
simple set and get command. Assume that the Memcached server is running on host
add the Memcached jar into your classpath as shown in the previous
Example
import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
public static void main(String[] args) {
output −
The terminal may show few informational messages too, those can be ignored.
noreply (optional) - It is a parameter that informs the server not to send any
reply.
Output
The output of the command is as shown below −
STORED
STORED indicates success.
Example
In the following example, we use tutorialspoint as the key and set value Memcached in
Example
import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
public static void main(String[] args) {
output −
Syntax
The basic syntax of Memcached add command is as shown below −
Output
The output of the command is as shown below −
STORED
Example
In the following example, we use ‘key’ as the key and add the value Memcached in it
Failure Output
add key 0 900 5
redis
NOT_STORED
Add Data Using Java Application
To add data in a Memcached server, you need to use the Memcached add method.
Example
import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
public static void main(String[] args) {
output −
Output
The output of the command is as shown below −
STORED
STORED indicates success.
Example
In the following example, we use ‘key’ as the key and store memcached in it with an
expiration time of 900 seconds. After this, the same key is replaced with the value
‘redis’.
method.
Example
import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
public static void main(String[] args) {
output −
Syntax
The basic syntax of Memcached append command is as shown below −
Output
The output of the command is as shown below −
STORED
NOT_STORED indicates the key does not exist in the Memcached server.
Example
In the following example, we try to add some data in a key that does not exist. Hence,
Memcached returns NOT_STORED. After this, we set one key and append data into it.
method.
Example
import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
public static void main(String[] args) {
output −
Syntax
The basic syntax of Memcached prepend command is as shown below −
retrieved in Memcached.
flags − It is the 32-bit unsigned integer that the server
Output
The output of the command is as shown below −
STORED
NOT_STORED indicates the key does not exist in the Memcached server.
Example
In the following example, we add some data in a key that does not exist. Hence,
Memcached returns NOT_STORED. After this, we set one key and prepend data into it.
method.
Example
import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
public static void main(String[] args) {
// Connecting to Memcached server on localhost
MemcachedClient mcc = new MemcachedClient(new
InetSocketAddress("127.0.0.1", 11211));
System.out.println("Connection to server successful");
System.out.println("set status:"+mcc.set("tutorialspoint", 900, "memcached").isDone());
output −
used to set the data if it is not updated since last fetch. If the key does not exist in
Syntax
The basic syntax of Memcached CAS command is as shown below −
above options.
Output
The output of the command is as shown below −
STORED
EXISTS indicates that someone has modified the CAS data since last fetch.
NOT_FOUND indicates that the key does not exist in the Memcached server.
Example
To execute a CAS command in Memcached, you need to get a CAS token from the
cas tp 0 900 9
ERROR
cas tp 0 900 9 2
memcached
set tp 0 900 9
memcached
STORED
gets tp
VALUE tp 0 9 1
memcached
END
cas tp 0 900 5 2
redis
EXISTS
cas tp 0 900 5 1
redis
STORED
get tp
VALUE tp 0 5
redis
END
Example
import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
public static void main(String[] args) {
output −
Syntax
The basic syntax of Memcached get command is as shown below −
get key
Example
In the following example, we use tutorialspoint as the key and store memcached in it
Example
import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
public static void main(String[] args) {
// Connecting to Memcached server on localhost
MemcachedClient mcc = new MemcachedClient(new
InetSocketAddress("127.0.0.1", 11211));
System.out.println("Connection to server sucessfully");
System.out.println("set status:"+mcc.set("tutorialspoint", 900, "memcached").done);
output −
Syntax
The basic syntax of Memcached gets command is as shown below −
get key
Example
set tutorialspoint 0 900 9
memcached
STORED
gets tutorialspoint
VALUE tutorialspoint 0 9 1
memcached
END
In this example, we use tutorialspoint as the key and store memcached in it with an
method.
Example
import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
public static void main(String[] args) {
output −
server.
Syntax
Output
CAS command may produce one of the following result −
NOT_FOUND indicates that the key does not exist in the Memcached server.
Example
In this example, we use tutorialspoint as a key and store memcached in it with an
expiration time of 900 seconds. After this, it deletes the stored key.
method.
Example
import java.net.InetSocketAddress;
import java.util.concurrent.Future;
import net.spy.memcached.MemcachedClient;
try{
}catch(Exception ex)
System.out.println(ex.getMessage());
}
}
Output
On compiling and executing the program, you get to see the following
output −
server.
Syntax
The basic syntax of Memcached delete command is as shown below −
delete key
If the key is successfully deleted, then it returns DELETED. If the key is not found, then
Example
In this example, we use tutorialspoint as a key and store memcached in it with an
expiration time of 900 seconds. After this, it deletes the stored key.
method.
Example
import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
public static void main(String[] args) {
output −
value of an existing key. If the key is not found, then it returns NOT_FOUND. If the key
Syntax - incr
The basic syntax of Memcached incr command is as shown below −
Example
In this example, we use visitors as key and set 10 initially into it, thereafter we
Syntax - decr
The basic syntax of Memcached decr command is as shown below
Example
set visitors 0 900 2
10
STORED
get visitors
VALUE visitors 0 2
10
END
decr visitors 5
5
get visitors
VALUE visitors 0 1
5
END
Example
import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
public static void main(String[] args) {
output −
connections, etc.
Syntax
stats
Example
stats
STAT pid 1162
STAT uptime 5022
STAT time 1415208270
STAT version 1.4.14
STAT libevent 2.0.19-stable
STAT pointer_size 64
STAT rusage_user 0.096006
STAT rusage_system 0.152009
STAT curr_connections 5
STAT total_connections 6
STAT connection_structures 6
STAT reserved_fds 20
STAT cmd_get 6
STAT cmd_set 4
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 4
STAT get_misses 2
STAT delete_misses 1
STAT delete_hits 1
STAT incr_misses 2
STAT incr_hits 1
STAT decr_misses 0
STAT decr_hits 1
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 262
STAT bytes_written 313
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
Example
import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
public static void main(String[] args) {
output −
Syntax
stats items
Example
stats items
STAT items:1:number 1
STAT items:1:age 7
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
END
Syntax
stats slabs
Example
stats slabs
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 1
STAT 1:free_chunks 10921
STAT 1:free_chunks_end 0
STAT 1:mem_requested 71
STAT 1:get_hits 0
STAT 1:cmd_set 1
STAT 1:delete_hits 0
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
END
items of each size within the cache. The information is returned in two columns. The
first column is the size of the item (rounded up to the nearest 32 byte boundary), and
the second column is the count of the number of items of that size within the cache.
Syntax
stats sizes
Example
stats sizes
STAT 96 1
END
The item size statistics are useful only to determine the sizes of the objects you are
storing. Since the actual memory allocation is relevant only in terms of the chunk size
and page size, the information is only useful during a careful debugging or diagnostic
session.
Memcached - Clear Data
Memcached flush_all command is used to delete all data (key-value pairs) from the
Memcached server. It accepts an optional parameter called time that sets a time after
Syntax
Example
In the following example, we store some data into the Memcached server and then
method.
Example
import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
public static void main(String[] args) {
output −
Discuss Memcached
Memcached is an open source, high-performance, distributed memory object caching
system. This tutorial provides a basic understanding of all the relevant concepts of
Memcached needed to create and deploy a highly scalable and performance-oriented
system.