// MembaseTest1
package
tutorial
;
import
java.net.InetSocketAddress
;
import
net.spy.memcached.MemcachedClient
;
/**
* Write / Read from Membase
*
* @author sujee
*
*/
public
class
MembaseTest1
{
static
int
MAX
=
100
;
static
String
server
=
"localhost"
;
static
int
port
=
11211
;
public
static
void
main
(
String
[]
args
)
throws
Exception
{
MemcachedClient
cache
=
new
MemcachedClient
(
new
InetSocketAddress
(
server
,
port
));
cache
.
flush
();
// 清除所有
long
t1
=
System
.
currentTimeMillis
();
for
(
int
i
=
0
;
i
<
MAX
;
i
++)
{
String
s
=
new
Integer
(
i
).
toString
();
// key : integer converted to String (keys are always string)
// time to live : in seconds, 3600 seconds (1h), 0 means no expiration
// value : actual integer. This can be an object. Our integer will be converted to 'Integer'
// class by 'auto boxing' proess
Object
o
=
cache
.
set
(
s
,
0
,
i
);
System
.
out
.
println
(
"cache put : "
+
s
+
" : "
+
i
+
", result "
+
o
);
}
long
t2
=
System
.
currentTimeMillis
();
System
.
out
.
println
(
"Time for "
+
MAX
+
" puts is "
+
(
t2
-
t1
)
+
" ms"
);
t1
=
System
.
currentTimeMillis
();
int
nulls
=
0
;
for
(
int
i
=
0
;
i
<
MAX
;
i
++)
{
String
s
=
new
Integer
(
i
).
toString
();
Object
o
=
cache
.
get
(
s
);
System
.
out
.
println
(
"Cache get : "
+
s
+
" : "
+
o
);
if
(
o
==
null
)
nulls
++;
}
t2
=
System
.
currentTimeMillis
();
cache
.
shutdown
();
System
.
out
.
println
(
"Time for "
+
MAX
+
" gets is "
+
(
t2
-
t1
)
+
" ms. nulls "
+
nulls
);
}
}
你可以在eclipse中运行这个文件(MembaseTest1)。或者从命令行执行
sh compile.sh sh run.sh or java -cp classes/:lib/memcached-2.5.jar tutorial.MembaseTest1Membase运行在本地,端口为11211(默认的memcached端口)
我们的键是一串字符型的数字,我们的对象是是整数型对象。
下面是输出样例:
我们密切注意NULL值。我们这次不应该获得NULL,如我们所希望的null个数为0。
代码也记录了时间戳,我们可以看到操作是多么得快。
// MembaseTest2
package
tutorial
;
import
java.net.InetSocketAddress
;
import
net.spy.memcached.MemcachedClient
;
/**
* simulates writing / reading from two different clients
*/
public
class
MembaseTest2
{
static
int
MAX
=
1000
;
static
String
server
=
"localhost"
;
static
int
port
=
11211
;
public
static
void
main
(
String
[]
args
)
throws
Exception
{
MemcachedClient
cache
=
new
MemcachedClient
(
new
InetSocketAddress
(
server
,
port
));
cache
.
flush
();
// clear all
long
t1
=
System
.
currentTimeMillis
();
for
(
int
i
=
0
;
i
<
MAX
;
i
++)
{
String
s
=
new
Integer
(
i
).
toString
();
// key : integer converted to String (keys are always string)
// time to live : in seconds, 3600 seconds (1h), 0 means no expiration
// value : actual integer. This can be an object. Our integer will be converted to 'Integer'
// class by 'auto boxing' proess
Object
o
=
cache
.
set
(
s
,
0
,
i
);
System
.
out
.
println
(
"cache put : "
+
s
+
" : "
+
i
+
", result "
+
o
);
}
long
t2
=
System
.
currentTimeMillis
();
cache
.
shutdown
();
// close the client
System
.
out
.
println
(
"Time for "
+
MAX
+
" puts is "
+
(
t2
-
t1
)
+
" ms"
);
// open another connection
cache
=
new
MemcachedClient
(
new
InetSocketAddress
(
server
,
port
));
t1
=
System
.
currentTimeMillis
();
int
nulls
=
0
;
for
(
int
i
=
0
;
i
<
MAX
;
i
++)
{
String
s
=
new
Integer
(
i
).
toString
();
Object
o
=
cache
.
get
(
s
);
System
.
out
.
println
(
"Cache get : "
+
s
+
" : "
+
o
);
if
(
o
==
null
)
nulls
++;
}
t2
=
System
.
currentTimeMillis
();
cache
.
shutdown
();
System
.
out
.
println
(
"Time for "
+
MAX
+
" gets is "
+
(
t2
-
t1
)
+
" ms. nulls "
+
nulls
+
"\n"
);
}
}
...
...
long
t2
=
System
.
currentTimeMillis
();
cache
.
shutdown
(
10
,
TimeUnit
.
SECONDS
);
// graceful shutdown
System
.
out
.
println
(
"Time for "
+
MAX
+
" puts is "
+
(
t2
-
t1
)
+
" ms"
);
// open another connection
cache
=
new
MemcachedClient
(
new
InetSocketAddress
(
server
,
port
));
...
...