使用 Java 程序连接 Memcached,需要在你的 classpath 中添加 Memcached jar 包。
成都创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站建设、成都做网站、余杭网络推广、重庆小程序开发、余杭网络营销、余杭企业策划、余杭品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联为所有大学生创业者提供余杭建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com
本站 jar 包下载地址:spymemcached-2.10.3.jar。
Google Code jar 包下载地址:spymemcached-2.10.3.jar(需要科学上网)。
以下程序假定 Memcached 服务的主机为 127.0.0.1,端口为 11211。
Java 连接 Memcached
import
net
.
spy
.
memcached
.
MemcachedClient
;
import
java
.
net
.*;
public
class
MemcachedJava
{
public
static
void
main
(
String
[
]
args
)
{
try
{
//
本地连接 Memcached 服务
MemcachedClient
mcc
=
new
MemcachedClient
(
new
InetSocketAddress
(
"
127.0.0.1
"
,
11211
)
)
;
System
.
out
.
println
(
"
Connection to server successful.
"
)
;
//
关闭连接
mcc
.
shutdown
(
)
;
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
(
)
)
;
}
}
}
该程序中我们使用 InetSocketAddress 连接 IP 为 127.0.0.1 端口 为 11211 的 memcached 服务。
执行以上代码,如果连接成功会输出以下信息:
Connection to server successful.
以下使用 java.util.concurrent.Future 来存储数据
import
java
.
net
.
InetSocketAddress
;
import
java
.
util
.
concurrent
.
Future
;
import
net
.
spy
.
memcached
.
MemcachedClient
;
public
class
MemcachedJava
{
public
static
void
main
(
String
[
]
args
)
{
try
{
//
连接本地的 Memcached 服务
MemcachedClient
mcc
=
new
MemcachedClient
(
new
InetSocketAddress
(
"
127.0.0.1
"
,
11211
)
)
;
System
.
out
.
println
(
"
Connection to server successful.
"
)
;
//
存储数据
Future
fo
=
mcc
.
set
(
"
runoob
"
,
900
,
"
Free Education
"
)
;
//
查看存储状态
System
.
out
.
println
(
"
set status:
"
+
fo
.
get
(
)
)
;
//
输出值
System
.
out
.
println
(
"
runoob value in cache -
"
+
mcc
.
get
(
"
runoob
"
)
)
;
//
关闭连接
mcc
.
shutdown
(
)
;
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
(
)
)
;
}
}
}
执行程序,输出结果为:
Connection to server successful. set status:true runoob value in cache - Free Education
import
java
.
net
.
InetSocketAddress
;
import
java
.
util
.
concurrent
.
Future
;
import
net
.
spy
.
memcached
.
MemcachedClient
;
public
class
MemcachedJava
{
public
static
void
main
(
String
[
]
args
)
{
try
{
//
连接本地的 Memcached 服务
MemcachedClient
mcc
=
new
MemcachedClient
(
new
InetSocketAddress
(
"
127.0.0.1
"
,
11211
)
)
;
System
.
out
.
println
(
"
Connection to server successful.
"
)
;
//
添加数据
Future
fo
=
mcc
.
set
(
"
runoob
"
,
900
,
"
Free Education
"
)
;
//
打印状态
System
.
out
.
println
(
"
set status:
"
+
fo
.
get
(
)
)
;
//
输出
System
.
out
.
println
(
"
runoob value in cache -
"
+
mcc
.
get
(
"
runoob
"
)
)
;
//
添加
fo
=
mcc
.
add
(
"
runoob
"
,
900
,
"
memcached
"
)
;
//
打印状态
System
.
out
.
println
(
"
add status:
"
+
fo
.
get
(
)
)
;
//
添加新key
fo
=
mcc
.
add
(
"
runoob
"
,
900
,
"
All Free Compilers
"
)
;
//
打印状态
System
.
out
.
println
(
"
add status:
"
+
fo
.
get
(
)
)
;
//
输出
System
.
out
.
println
(
"
runoob value in cache -
"
+
mcc
.
get
(
"
runoob
"
)
)
;
//
关闭连接
mcc
.
shutdown
(
)
;
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
(
)
)
;
}
}
}
import
java
.
net
.
InetSocketAddress
;
import
java
.
util
.
concurrent
.
Future
;
import
net
.
spy
.
memcached
.
MemcachedClient
;
public
class
MemcachedJava
{
public
static
void
main
(
String
[
]
args
)
{
try
{
//
连接本地的 Memcached 服务
MemcachedClient
mcc
=
new
MemcachedClient
(
new
InetSocketAddress
(
"
127.0.0.1
"
,
11211
)
)
;
System
.
out
.
println
(
"
Connection to server successful.
"
)
;
//
添加第一个 key=》value 对
Future
fo
=
mcc
.
set
(
"
runoob
"
,
900
,
"
Free Education
"
)
;
//
输出执行 add 方法后的状态
System
.
out
.
println
(
"
add status:
"
+
fo
.
get
(
)
)
;
//
获取键对应的值
System
.
out
.
println
(
"
runoob value in cache -
"
+
mcc
.
get
(
"
runoob
"
)
)
;
//
添加新的 key
fo
=
mcc
.
replace
(
"
runoob
"
,
900
,
"
Largest Tutorials' Library
"
)
;
//
输出执行 set 方法后的状态
System
.
out
.
println
(
"
replace status:
"
+
fo
.
get
(
)
)
;
//
获取键对应的值
System
.
out
.
println
(
"
runoob value in cache -
"
+
mcc
.
get
(
"
runoob
"
)
)
;
//
关闭连接
mcc
.
shutdown
(
)
;
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
(
)
)
;
}
}
}
import
java
.
net
.
InetSocketAddress
;
import
java
.
util
.
concurrent
.
Future
;
import
net
.
spy
.
memcached
.
MemcachedClient
;
public
class
MemcachedJava
{
public
static
void
main
(
String
[
]
args
)
{
try
{
//
连接本地的 Memcached 服务
MemcachedClient
mcc
=
new
MemcachedClient
(
new
InetSocketAddress
(
"
127.0.0.1
"
,
11211
)
)
;
System
.
out
.
println
(
"
Connection to server successful.
"
)
;
//
添加数据
Future
fo
=
mcc
.
set
(
"
runoob
"
,
900
,
"
Free Education
"
)
;
//
输出执行 set 方法后的状态
System
.
out
.
println
(
"
set status:
"
+
fo
.
get
(
)
)
;
//
获取键对应的值
System
.
out
.
println
(
"
runoob value in cache -
"
+
mcc
.
get
(
"
runoob
"
)
)
;
//
对存在的key进行数据添加操作
fo
=
mcc
.
append
(
"
runoob
"
,
900
,
"
for All
"
)
;
//
输出执行 set 方法后的状态
System
.
out
.
println
(
"
append status:
"
+
fo
.
get
(
)
)
;
//
获取键对应的值
System
.
out
.
println
(
"
runoob value in cache -
"
+
mcc
.
get
(
"
runoob
"
)
)
;
//
关闭连接
mcc
.
shutdown
(
)
;
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
(
)
)
; ]
}
}
import
java
.
net
.
InetSocketAddress
;
import
java
.
util
.
concurrent
.
Future
;
import
net
.
spy
.
memcached
.
MemcachedClient
;
public
class
MemcachedJava
{
public
static
void
main
(
String
[
]
args
)
{
try
{
//
连接本地的 Memcached 服务
MemcachedClient
mcc
=
new
MemcachedClient
(
new
InetSocketAddress
(
"
127.0.0.1
"
,
11211
)
)
;
System
.
out
.
println
(
"
Connection to server successful.
"
)
;
//
添加数据
Future
fo
=
mcc
.
set
(
"
runoob
"
,
900
,
"
Education for All
"
)
;
//
输出执行 set 方法后的状态
System
.
out
.
println
(
"
set status:
"
+
fo
.
get
(
)
)
;
//
获取键对应的值
System
.
out
.
println
(
"
runoob value in cache -
"
+
mcc
.
get
(
"
runoob
"
)
)
;
//
对存在的key进行数据添加操作
fo
=
mcc
.
prepend
(
"
runoob
"
,
900
,
"
Free
"
)
;
//
输出执行 set 方法后的状态
System
.
out
.
println
(
"
prepend status:
"
+
fo
.
get
(
)
)
;
//
获取键对应的值
System
.
out
.
println
(
"
runoob value in cache -
"
+
mcc
.
get
(
"
runoob
"
)
)
;
//
关闭连接
mcc
.
shutdown
(
)
;
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
(
)
)
;
}
}
}
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
{
//
连接本地的 Memcached 服务
MemcachedClient
mcc
=
new
MemcachedClient
(
new
InetSocketAddress
(
"
127.0.0.1
"
,
11211
)
)
;
System
.
out
.
println
(
"
Connection to server successful.
"
)
;
//
添加数据
Future
fo
=
mcc
.
set
(
"
runoob
"
,
900
,
"
Free Education
"
)
;
//
输出执行 set 方法后的状态
System
.
out
.
println
(
"
set status:
"
+
fo
.
get
(
)
)
;
//
使用 get 方法获取数据
System
.
out
.
println
(
"
runoob value in cache -
"
+
mcc
.
get
(
"
runoob
"
)
)
;
//
通过 gets 方法获取 CAS token(令牌)
CASValue
casValue
=
mcc
.
gets
(
"
runoob
"
)
;
//
输出 CAS token(令牌) 值
System
.
out
.
println
(
"
CAS token -
"
+
casValue
)
;
//
尝试使用cas方法来更新数据
CASResponse
casresp
=
mcc
.
cas
(
"
runoob
"
,
casValue
.
getCas
(
)
,
900
,
"
Largest Tutorials-Library
"
)
;
//
输出 CAS 响应信息
System
.
out
.
println
(
"
CAS Response -
"
+
casresp
)
;
//
输出值
System
.
out
.
println
(
"
runoob value in cache -
"
+
mcc
.
get
(
"
runoob
"
)
)
;
//
关闭连接
mcc
.
shutdown
(
)
;
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
(
)
)
;
}
}
}
import
java
.
net
.
InetSocketAddress
;
import
java
.
util
.
concurrent
.
Future
;
import
net
.
spy
.
memcached
.
MemcachedClient
;
public
class
MemcachedJava
{
public
static
void
main
(
String
[
]
args
)
{
try
{
//
连接本地的 Memcached 服务
MemcachedClient
mcc
=
new
MemcachedClient
(
new
InetSocketAddress
(
"
127.0.0.1
"
,
11211
)
)
;
System
.
out
.
println
(
"
Connection to server successful.
"
)
;
//
添加数据
Future
fo
=
mcc
.
set
(
"
runoob
"
,
900
,
"
Free Education
"
)
;
//
输出执行 set 方法后的状态
System
.
out
.
println
(
"
set status:
"
+
fo
.
get
(
)
)
;
//
使用 get 方法获取数据
System
.
out
.
println
(
"
runoob value in cache -
"
+
mcc
.
get
(
"
runoob
"
)
)
;
//
关闭连接
mcc
.
shutdown
(
)
;
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
(
)
)
;
}
}
}
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
{
//
连接本地的 Memcached 服务
MemcachedClient
mcc
=
new
MemcachedClient
(
new
InetSocketAddress
(
"
127.0.0.1
"
,
11211
)
)
;
System
.
out
.
println
(
"
Connection to server successful.
"
)
;
//
添加数据
Future
fo
=
mcc
.
set
(
"
runoob
"
,
900
,
"
Free Education
"
)
;
//
输出执行 set 方法后的状态
System
.
out
.
println
(
"
set status:
"
+
fo
.
get
(
)
)
;
//
从缓存中获取键为 runoob 的值
System
.
out
.
println
(
"
runoob value in cache -
"
+
mcc
.
get
(
"
runoob
"
)
)
;
//
通过 gets 方法获取 CAS token(令牌)
CASValue
casValue
=
mcc
.
gets
(
"
runoob
"
)
;
//
输出 CAS token(令牌) 值
System
.
out
.
println
(
"
CAS value in cache -
"
+
casValue
)
;
//
关闭连接
mcc
.
shutdown
(
)
;
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
(
)
)
;
}
}
}
import
java
.
net
.
InetSocketAddress
;
import
java
.
util
.
concurrent
.
Future
;
import
net
.
spy
.
memcached
.
MemcachedClient
;
public
class
MemcachedJava
{
public
static
void
main
(
String
[
]
args
)
{
try
{
//
连接本地的 Memcached 服务
MemcachedClient
mcc
=
new
MemcachedClient
(
new
InetSocketAddress
(
"
127.0.0.1
"
,
11211
)
)
;
System
.
out
.
println
(
"
Connection to server successful.
"
)
;
//
添加数据
Future
fo
=
mcc
.
set
(
"
runoob
"
,
900
,
"
World's largest online tutorials library
"
)
;
//
输出执行 set 方法后的状态
System
.
out
.
println
(
"
set status:
"
+
fo
.
get
(
)
)
;
//
获取键对应的值
System
.
out
.
println
(
"
runoob value in cache -
"
+
mcc
.
get
(
"
runoob
"
)
)
;
//
对存在的key进行数据添加操作
fo
=
mcc
.
delete
(
"
runoob
"
)
;
//
输出执行 delete 方法后的状态
System
.
out
.
println
(
"
delete status:
"
+
fo
.
get
(
)
)
;
//
获取键对应的值
System
.
out
.
println
(
"
runoob value in cache -
"
+
mcc
.
get
(
"
runoob
"
)
)
;
//
关闭连接
mcc
.
shutdown
(
)
;
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
(
)
)
;
}
}
}
import
java
.
net
.
InetSocketAddress
;
import
java
.
util
.
concurrent
.
Future
;
import
net
.
spy
.
memcached
.
MemcachedClient
;
public
class
MemcachedJava
{
public
static
void
main
(
String
[
]
args
)
{
try
{
//
连接本地的 Memcached 服务
MemcachedClient
mcc
=
new
MemcachedClient
(
new
InetSocketAddress
(
"
127.0.0.1
"
,
11211
)
)
;
System
.
out
.
println
(
"
Connection to server successful.
"
)
;
//
添加数字值
Future
fo
=
mcc
.
set
(
"
number
"
,
900
,
"
1000
"
)
;
//
输出执行 set 方法后的状态
System
.
out
.
println
(
"
set status:
"
+
fo
.
get
(
)
)
;
//
获取键对应的值
System
.
out
.
println
(
"
value in cache -
"
+
mcc
.
get
(
"
number
"
)
)
;
//
自增并输出
System
.
out
.
println
(
"
value in cache after increment -
"
+
mcc
.
incr
(
"
number
"
,
111
)
)
;
//
自减并输出
System
.
out
.
println
(
"
value in cache after decrement -
"
+
mcc
.
decr
(
"
number
"
,
112
)
)
;
//
关闭连接
mcc
.
shutdown
(
)
;
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
(
)
)
;
}
}
}
网站栏目:创新互联Memcached教程:Java连接Memcached服务
标题来源:http://www.csdahua.cn/qtweb/news26/492826.html
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网