Jedis的简单封装,主要是去掉了类型。

==


@Service("redisMapServiceImpl1")
public class RedisMapServiceImpl  {

 @Autowired
 private ShardedJedisPool shardedJedisPool;

 private ShardedJedis shardedJedis;

 
 public void put(Object k, Object v) {
  getShardedJedis().set(this.ObjectToByte(k), this.ObjectToByte(v));
 }

 
 public <V> V get(Object key) {
  return this.ByteToObject(getShardedJedis().get(this.ObjectToByte(key)));
 }

 
 public void remove(Object key) {
  getShardedJedis().del(this.ObjectToByte(key));
 }

 private ShardedJedis getShardedJedis() {
  if (shardedJedis == null) {
   shardedJedis = shardedJedisPool.getResource();
  }
  return shardedJedis;
 }

 private <C> C ByteToObject(byte[] bytes) {
  C obj = null;
  try {
   ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
   ObjectInputStream oi = new ObjectInputStream(bi);
   obj = (C) oi.readObject();
   bi.close();
   oi.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return obj;
 }

 public byte[] ObjectToByte(Object obj) {
  byte[] bytes = null;
  try {
   ByteArrayOutputStream bo = new ByteArrayOutputStream();
   ObjectOutputStream oo = new ObjectOutputStream(bo);
   oo.writeObject(obj);
   bytes = bo.toByteArray();
   bo.close();
   oo.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return bytes;
 }

}


==


这个代码不够好,还在持续想办法优化中。。。。。(*^__^*) 嘻嘻……



上面的代码是流  然后是字符串的。

==

@Service("redisJsonMapServiceImpl")
public class RedisJsonMapServiceImpl implements RedisMapService {

	@Autowired
	private ShardedJedisPool shardedJedisPool;

	private Jedis jedis;

	public void put(String k, String vStr) {
		getShardedJedis().set(k, vStr);
	}

	public void put(String k, Object vObj) {
		ObjectMapper mapper = new ObjectMapper();
		String json = null;
		try {
			json = mapper.writeValueAsString(vObj);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		this.put(k, json);
	}

	public void put(String k, int seconds, String vStr) {
		getShardedJedis().setex(k, seconds, vStr);
	}

	public void put(String k, int seconds, Object vObj) {
		ObjectMapper mapper = new ObjectMapper();
		String json = null;
		try {
			json = mapper.writeValueAsString(vObj);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		this.put(k, seconds, json);
	}

	public <V> V get(String key, TypeReference<V> t) {
		String s = getShardedJedis().get(key);
		ObjectMapper mapper = new ObjectMapper();
		V vo = null;
		try {
			vo = mapper.readValue(s, t);
		} catch (Exception e) {

			e.printStackTrace();
		}
		return vo;
	}

	public String get(String key) {
		return getShardedJedis().get(key);
	}

	public void remove(String key) {
		getShardedJedis().del(key);
	}

	public Set<String> getMatch(String pattern) {
		return getShardedJedis().keys(pattern);
	}

	public Set<String> getKayAll() {
		return getShardedJedis().keys("*");
	}
	private Jedis getShardedJedis() {
		if (jedis == null) {
			jedis = shardedJedisPool.getResource().getAllShards().iterator()
					.next();
		}
		return jedis;
	}
}


==




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值