memcache与java:ubuntu下memcached服务安装与java客户端(xmemcached)编程

一、ubuntu下memcached服务安装

1.安装Memcache服务端

sudo apt-get install memcached

安装完Memcache服务端以后,我们需要启动该服务:

memcached -d -m 128 -p 11111 -u root

这里需要说明一下memcached服务的启动参数:

-p 监听的端口

-l 连接的IP地址, 默认是本机 

-d start 启动memcached服务 

-d restart 重起memcached服务

-d stop|shutdown 关闭正在运行的memcached服务

-d install 安装memcached服务

-d uninstall 卸载memcached服务 

-u 以的身份运行 (仅在以root运行的时候有效) 

-m 最大内存使用,单位MB。默认64MB 

-M 内存耗尽时返回错误,而不是删除项 

-c 最大同时连接数,默认是1024 

-f 块大小增长因子,默认是1.25

-n 最小分配空间,key+value+flags默认是48 

-h 显示帮助 

 2. 数据读写测试

[root@odb ~]# telnet 127.0.0.1 10000 
Trying 127.0.0.1… 
Connected to localhost.localdomain (127.0.0.1).
Escape character is ‘^]’. 
set key 0 0 8 <—在10000端口设置key的值
88888888 
STORED 
quit
Connection closed by foreign host.

[root@odb ~]# telnet 127.0.0.1 11211 
Trying 127.0.0.1… 
Connected to localhost.localdomain (127.0.0.1).
Escape character is ‘^]’. 
get key <—在11211端口获取key的值成功 
VALUE key 0 8 88888888 END 
quit
Connection closed by foreign host. 

3.修改memcached的配置文件

将默认的127.0.0.1地址改成外部可访问的地址。(与Mysql一样的道理) 

/etc/memcached.conf 修改该文件中将

 -l 127.0.0.1

 改为本机的IP地址

 -l 192.168.1.100 


运行

service memcached restart

 重新启动后,从外部就可以访问了。

 二、java客户端(xmemcached)编程 

Xmemcached的官方网址是:http://code.google.com/p/xmemcached/,可以从其官网上下载最新版本的1.2.4来使用。

地址是:http://xmemcached.googlecode.com/files/xmemcached-1.2.4-src.tar.gz。

在pom.xml引入

<!-- cache client -->
<dependency>
	<groupId>com.googlecode.xmemcached</groupId>
	<artifactId>xmemcached</artifactId>
	<version>2.0.0</version>
</dependency>
<!-- cache client -->

java代码如下:

package com.ya.cache;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.exception.MemcachedException;
import net.rubyeye.xmemcached.utils.AddrUtil;

public class CacheTest {
	public static void main(String[] args) {
		MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil

		.getAddresses("139.196.18.90:11211"));

		MemcachedClient memcachedClient;

		try {
			memcachedClient = builder.build();

			memcachedClient.set("hello", 0, "Hello,xmemcached");

			String value = memcachedClient.get("hello");

			System.out.println("hello=" + value);

			memcachedClient.delete("hello");

			value = memcachedClient.get("hello");

			System.out.println("hello=" + value);

			// close memcached client

			memcachedClient.shutdown();
		} catch (IOException | TimeoutException | InterruptedException
				| MemcachedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

运行结果如下:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
hello=Hello,xmemcached
hello=null

© 2015, 李德涛博客. 版权所有.

发表评论

电子邮件地址不会被公开。 必填项已用*标注