码迷,mamicode.com
首页 > 其他好文 > 详细

jedis简单使用

时间:2015-07-07 00:58:54      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:jedis

目前只用了单机版的jedis,集群搭建成功,但接口测试有问题


1.和jedis服务连接

public class RedisUtil {
	
	private static String JEDIS_SERVER="10.0.0.18";
	
	private static int JEDIS_PORT=6379;
	
	protected Jedis jedis = new Jedis(JEDIS_SERVER, JEDIS_PORT);

}

2.实现简单的增删改查,其中key为表名+索引,value为对象的json串

private static String SERVER_KEY="server";
public class ServerDaoImpl  extends RedisUtil implements IServerDao{
	
	private static String SERVER_KEY="server";

	@Override
	public void saveOrUpdateServer(Server server) {
		
		String key = SERVER_KEY + ":" + server.getIp() ;
		String value = JsonUtils.object2json(server);
		
		jedis.set(key, value);
	}


	@Override
	public void deleteServer(String ip) {
		String key =SERVER_KEY+":" +ip;
		jedis.del(key);
	}

	@Override
	public Server getServer(String ip) {
		String key =SERVER_KEY+":" +ip;
		Server server = (Server)JSONObject.toBean(JSONObject.fromObject(jedis.get(key)),Server.class);
		return server;
	}

	@Override
	public List<Server> getAllServer() {
		List<Server> servers = new ArrayList<Server>();
		
		String key =SERVER_KEY+"*";
		Set<String> set =jedis.keys(key);
		for(String s:set){
			System.out.println(jedis.get(s));
			
			Server server = (Server)JSONObject.toBean(JSONObject.fromObject(jedis.get(s)),Server.class);
			servers.add(server);
			
		}
		return servers;
				 
		
		
		
	}

	

}


版权声明:本文为博主原创文章,未经博主允许不得转载。

jedis简单使用

标签:jedis

原文地址:http://blog.csdn.net/liuwei063608/article/details/46781427

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!