Java Connect to Memcached Service
To connect to Memcached using a Java program, you need to add the Memcached jar package to your classpath.
Download address for the jar package on this site:spymemcached-2.10.3.jar.
Google Code jar package download address:spymemcached-2.10.3.jar(requires a VPN/proxy to access).
The following program assumes the Memcached service host is 127.0.0.1 and the port is 11211.
Connection Example
Java Connect to Memcached
MemcachedJava.java file:
import
net
.
spy
.
memcached
.
MemcachedClient
;
import
java
.
net
.*;
public
class
MemcachedJava
{
public
static
void
main
(
String
[
]
args
)
{
try
{
//
Connect to the local Memcached service
MemcachedClient
mcc
=
new
MemcachedClient
(
new
InetSocketAddress
(
"
127.0.0.1
"
,
11211
)
)
;
System
.
out
.
println
(
"
Connection to server successful.
"
)
;
//
Close the Connection
mcc
.
shutdown
(
)
;
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
(
)
)
;
}
}
}
In this program we use InetSocketAddress to connect to the memcached service with IP 127.0.0.1 and port 11211.
Execute the above code; if the connection succeeds, the following information will be output:
Connection to server successful.
set operation example
The following uses java.util.concurrent.Future to store data
MemcachedJava.java file:
import
java
.
net
.
InetSocketAddress
;
import
java
.
util
.
concurrent
.
Future
;
import
net
.
spy
.
memcached
.
MemcachedClient
;
public
class
MemcachedJava
{
public
static
void
main
(
String
[
]
args
)
{
try
{
//
Connect to the local Memcached service
MemcachedClient
mcc
=
new
MemcachedClient
(
new
InetSocketAddress
(
"
127.0.0.1
"
,
11211
)
)
;
System
.
out
.
println
(
"
Connection to server successful.
"
)
;
//
Store data
Future
fo
=
mcc
.
set
(
"
runoob
"
,
900
,
"
Free Education
"
)
;
//
Check the storage status
System
.
out
.
println
(
"
set status:
"
fo
.
get
(
)
)
;
//
Output value
System
.
out
.
println
(
"
runoob value in cache -
"
mcc
.
get
(
"
runoob
"
)
)
;
//
Close the Connection
mcc
.
shutdown
(
)
;
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
(
)
)
;
}
}
}
Execute the program; the output result is:
Connection to server successful.
set status:true
runoob value in cache - Free Education
add operation example
MemcachedJava.java file:
import
java
.
net
.
InetSocketAddress
;
import
java
.
util
.
concurrent
.
Future
;
import
net
.
spy
.
memcached
.
MemcachedClient
;
public
class
MemcachedJava
{
public
static
void
main
(
String
[
]
args
)
{
try
{
//
Connect to the local Memcached service
MemcachedClient
mcc
=
new
MemcachedClient
(
new
InetSocketAddress
(
"
127.0.0.1
"
,
11211
)
)
;
System
.
out
.
println
(
"
Connection to server successful.
"
)
;
//
Add data
Future
fo
=
mcc
.
set
(
"
runoob
"
,
900
,
"
Free Education
"
)
;
//
Print status
System
.
out
.
println
(
"
set status:
"
fo
.
get
(
)
)
;
//
Output
System
.
out
.
println
(
"
runoob value in cache -
"
mcc
.
get
(
"
runoob
"
)
)
;
//
add
fo
=
mcc
.
add
(
"
runoob
"
,
900
,
"
memcached
"
)
;
//
Print status
System
.
out
.
println
(
"
add status:
"
fo
.
get
(
)
)
;
//
Add a new key
fo
=
mcc
.
add
(
"
runoob
"
,
900
,
"
All Free Compilers
"
)
;
//
Print status
System
.
out
.
println
(
"
add status:
"
fo
.
get
(
)
)
;
//
Output
System
.
out
.
println
(
"
runoob value in cache -
"
mcc
.
get
(
"
runoob
"
)
)
;
//
Close the Connection
mcc
.
shutdown
(
)
;
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
(
)
)
;
}
}
}
replace operation example
MemcachedJava.java file:
import
java
.
net
.
InetSocketAddress
;
import
java
.
util
.
concurrent
.
Future
;
import
net
.
spy
.
memcached
.
MemcachedClient
;
public
class
MemcachedJava
{
public
static
void
main
(
String
[
]
args
)
{
try
{
//
Connect to the local Memcached service
MemcachedClient
mcc
=
new
MemcachedClient
(
new
InetSocketAddress
(
"
127.0.0.1
"
,
11211
)
)
;
System
.
out
.
println
(
"
Connection to server successful.
"
)
;
//
Add the first key=>value pair
Future
fo
=
mcc
.
set
(
"
runoob
"
,
900
,
"
Free Education
"
)
;
//
Output the status after executing the add method
System
.
out
.
println
(
"
add status:
"
fo
.
get
(
)
)
;
//
Get the value corresponding to the key
System
.
out
.
println
(
"
runoob value in cache -
"
mcc
.
get
(
"
runoob
"
)
)
;
//
Add a new key
fo
=
mcc
.
replace
(
"
runoob
"
,
900
,
"
Largest Tutorials' Library
"
)
;
//
Output the status after executing the set method
System
.
out
.
println
(
"
replace status:
"
fo
.
get
(
)
)
;
//
Get the value corresponding to the key
System
.
out
.
println
(
"
runoob value in cache -
"
mcc
.
get
(
"
runoob
"
)
)
;
//
Close the Connection
mcc
.
shutdown
(
)
;
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
(
)
)
;
}
}
}
append operation example
MemcachedJava.java file:
import
java
.
net
.
InetSocketAddress
;
import
java
.
util
.
concurrent
.
Future
;
import
net
.
spy
.
memcached
.
MemcachedClient
;
public
class
MemcachedJava
{
public
static
void
main
(
String
[
]
args
)
{
try
{
//
Connect to the local Memcached service
MemcachedClient
mcc
=
new
MemcachedClient
(
new
InetSocketAddress
(
"
127.0.0.1
"
,
11211
)
)
;
System
.
out
.
println
(
"
Connection to server successful.
"
)
;
//
Add data
Future
fo
=
mcc
.
set
(
"
runoob
"
,
900
,
"
Free Education
"
)
;
//
Output the status after executing the set method
System
.
out
.
println
(
"
set status:
"
fo
.
get
(
)
)
;
//
Get the value corresponding to the key
System
.
out
.
println
(
"
runoob value in cache -
"
mcc
.
get
(
"
runoob
"
)
)
;
//
Perform a data append operation on an existing key
fo
=
mcc
.
append
(
"
runoob
"
,
900
,
"
for All
"
)
;
//
Output the status after executing the set method
System
.
out
.
println
(
"
append status:
"
fo
.
get
(
)
)
;
//
Get the value corresponding to the key
System
.
out
.
println
(
"
runoob value in cache -
"
mcc
.
get
(
"
runoob
"
)
)
;
//
Close the Connection
mcc
.
shutdown
(
)
;
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
(
)
)
; ]
}
}
prepend operation example
MemcachedJava.java file:
import
java
.
net
.
InetSocketAddress
;
import
java
.
util
.
concurrent
.
Future
;
import
net
.
spy
.
memcached
.
MemcachedClient
;
public
class
MemcachedJava
{
public
static
void
main
(
String
[
]
args
)
{
try
{
//
Connect to the local Memcached service
MemcachedClient
mcc
=
new
MemcachedClient
(
new
InetSocketAddress
(
"
127.0.0.1
"
,
11211
)
)
;
System
.
out
.
println
(
"
Connection to server successful.
"
)
;
//
Add data
Future
fo
=
mcc
.
set
(
"
runoob
"
,
900
,
"
Education for All
"
)
;
//
Output the status after executing the set method
System
.
out
.
println
(
"
set status:
"
fo
.
get
(
)
)
;
//
Get the value corresponding to the key
System
.
out
.
println
(
"
runoob value in cache -
"
mcc
.
get
(
"
runoob
"
)
)
;
//
Perform a data append operation on an existing key
fo
=
mcc
.
prepend
(
"
runoob
"
,
900
,
"
Free
"
)
;
//
Output the status after executing the set method
System
.
out
.
println
(
"
prepend status:
"
fo
.
get
(
)
)
;
//
Get the value corresponding to the key
System
.
out
.
println
(
"
runoob value in cache -
"
mcc
.
get
(
"
runoob
"
)
)
;
//
Close the Connection
mcc
.
shutdown
(
)
;
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
(
)
)
;
}
}
}
CAS operation example
MemcachedJava.java file:
import
java
.
net
.
InetSocketAddress
;
import
java
.
util
.
concurrent
.
Future
;
import
net
.
spy
.
memcached
.
CASValue
;
import
net
.
spy
.
memcached
.
CASResponse
;
import
net
.
spy
.
memcached
.
MemcachedClient
;
public
class
MemcachedJava
{
public
static
void
main
(
String
[
]
args
)
{
try
{
//
Connect to the local Memcached service
MemcachedClient
mcc
=
new
MemcachedClient
(
new
InetSocketAddress
(
"
127.0.0.1
"
,
11211
)
)
;
System
.
out
.
println
(
"
Connection to server successful.
"
)
;
//
Add data
Future
fo
=
mcc
.
set
(
"
runoob
"
,
900
,
"
Free Education
"
)
;
//
Output the status after executing the set method
System
.
out
.
println
(
"
set status:
"
fo
.
get
(
)
)
;
//
Use the get method to retrieve data
System
.
out
.
println
(
"
runoob value in cache -
"
mcc
.
get
(
"
runoob
"
)
)
;
//
Obtain the CAS token via the gets method
CASValue
casValue
=
mcc
.
gets
(
"
runoob
"
)
;
//
Output the CAS token value
System
.
out
.
println
(
"
CAS token -
"
casValue
)
;
//
Try to use the cas method to update data
CASResponse
casresp
=
mcc
.
cas
(
"
runoob
"
,
casValue
.
getCas
(
)
,
900
,
"
Largest Tutorials-Library
"
)
;
//
Output the CAS response information
System
.
out
.
println
(
"
CAS Response -
"
casresp
)
;
//
Output value
System
.
out
.
println
(
"
runoob value in cache -
"
mcc
.
get
(
"
runoob
"
)
)
;
//
Close the Connection
mcc
.
shutdown
(
)
;
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
(
)
)
;
}
}
}
get operation example
MemcachedJava.java file:
import
java
.
net
.
InetSocketAddress
;
import
java
.
util
.
concurrent
.
Future
;
import
net
.
spy
.
memcached
.
MemcachedClient
;
public
class
MemcachedJava
{
public
static
void
main
(
String
[
]
args
)
{
try
{
//
Connect to the local Memcached service
MemcachedClient
mcc
=
new
MemcachedClient
(
new
InetSocketAddress
(
"
127.0.0.1
"
,
11211
)
)
;
System
.
out
.
println
(
"
Connection to server successful.
"
)
;
//
Add data
Future
fo
=
mcc
.
set
(
"
runoob
"
,
900
,
"
Free Education
"
)
;
//
Output the status after executing the set method
System
.
out
.
println
(
"
set status:
"
fo
.
get
(
)
)
;
//
Use the get method to retrieve data
System
.
out
.
println
(
"
runoob value in cache -
"
mcc
.
get
(
"
runoob
"
)
)
;
//
Close the Connection
mcc
.
shutdown
(
)
;
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
(
)
)
;
}
}
}
gets operation example, CAS
MemcachedJava.java file:
import
java
.
net
.
InetSocketAddress
;
import
java
.
util
.
concurrent
.
Future
;
import
net
.
spy
.
memcached
.
CASValue
;
import
net
.
spy
.
memcached
.
CASResponse
;
import
net
.
spy
.
memcached
.
MemcachedClient
;
public
class
MemcachedJava
{
public
static
void
main
(
String
[
]
args
)
{
try
{
//
Connect to the local Memcached service
MemcachedClient
mcc
=
new
MemcachedClient
(
new
InetSocketAddress
(
"
127.0.0.1
"
,
11211
)
)
;
System
.
out
.
println
(
"
Connection to server successful.
"
)
;
//
Add data
Future
fo
=
mcc
.
set
(
"
runoob
"
,
900
,
"
Free Education
"
)
;
//
Output the status after executing the set method
System
.
out
.
println
(
"
set status:
"
fo
.
get
(
)
)
;
//
Retrieve the value of the key runoob from the cache
System
.
out
.
println
(
"
runoob value in cache -
"
mcc
.
get
(
"
runoob
"
)
)
;
//
Obtain the CAS token via the gets method
CASValue
casValue
=
mcc
.
gets
(
"
runoob
"
)
;
//
Output the CAS token value
System
.
out
.
println
(
"
CAS value in cache -
"
casValue
)
;
//
Close the Connection
mcc
.
shutdown
(
)
;
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
(
)
)
;
}
}
}
delete operation example
MemcachedJava.java file:
import
java
.
net
.
InetSocketAddress
;
import
java
.
util
.
concurrent
.
Future
;
import
net
.
spy
.
memcached
.
MemcachedClient
;
public
class
MemcachedJava
{
public
static
void
main
(
String
[
]
args
)
{
try
{
//
Connect to the local Memcached service
MemcachedClient
mcc
=
new
MemcachedClient
(
new
InetSocketAddress
(
"
127.0.0.1
"
,
11211
)
)
;
System
.
out
.
println
(
"
Connection to server successful.
"
)
;
//
Add data
Future
fo
=
mcc
.
set
(
"
runoob
"
,
900
,
"
World's largest online tutorials library
"
)
;
//
Output the status after executing the set method
System
.
out
.
println
(
"
set status:
"
fo
.
get
(
)
)
;
//
Get the value corresponding to the key
System
.
out
.
println
(
"
runoob value in cache -
"
mcc
.
get
(
"
runoob
"
)
)
;
//
Perform a data append operation on an existing key
fo
=
mcc
.
delete
(
"
runoob
"
)
;
//
Output the status after executing the delete method
System
.
out
.
println
(
"
delete status:
"
fo
.
get
(
)
)
;
//
Get the value corresponding to the key
System
.
out
.
println
(
"
runoob value in cache -
"
mcc
.
get
(
"
runoob
"
)
)
;
//
Close the Connection
mcc
.
shutdown
(
)
;
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
(
)
)
;
}
}
}
Incr/Decr operation example
MemcachedJava.java file:
import
java
.
net
.
InetSocketAddress
;
import
java
.
util
.
concurrent
.
Future
;
import
net
.
spy
.
memcached
.
MemcachedClient
;
public
class
MemcachedJava
{
public
static
void
main
(
String
[
]
args
)
{
try
{
//
Connect to the local Memcached service
MemcachedClient
mcc
=
new
MemcachedClient
(
new
InetSocketAddress
(
"
127.0.0.1
"
,
11211
)
)
;
System
.
out
.
println
(
"
Connection to server successful.
"
)
;
//
Add a numeric value
Future
fo
=
mcc
.
set
(
"
number
"
,
900
,
"
1000
"
)
;
//
Output the status after executing the set method
System
.
out
.
println
(
"
set status:
"
fo
.
get
(
)
)
;
//
Get the value corresponding to the key
System
.
out
.
println
(
"
value in cache -
"
mcc
.
get
(
"
number
"
)
)
;
//
Increment and output
System
.
out
.
println
(
"
value in cache after increment -
"
mcc
.
incr
(
"
number
"
,
111
)
)
;
//
Decrement and output
System
.
out
.
println
(
"
value in cache after decrement -
"
mcc
.
decr
(
"
number
"
,
112
)
)
;
//
Close the Connection
mcc
.
shutdown
(
)
;
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
(
)
)
;
}
}
}