提交 05b028e4 编写于 作者: 武汉红喜's avatar 武汉红喜

RedisService封装简化

上级 3fb0e985
package org.hongxi.whatsmars.redis.client;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
......@@ -10,10 +12,13 @@ import java.util.*;
* 单个redis模式作为sharding模式的特例(配一个host:port即可)
* JedisCluster不用封装,直接用即可
*/
@Slf4j
public class RedisTemplate {
private ShardedJedisPool shardedJedisPool;
private ObjectMapper objectMapper = new ObjectMapper();
public <T> T execute(RedisCallback<T> action) {
ShardedJedis jedis = fetchJedis();
try {
......@@ -155,6 +160,15 @@ public class RedisTemplate {
}
}
public boolean sismember(String key, String value) {
ShardedJedis jedis = fetchJedis();
try {
return jedis.sismember(key, value);
} finally {
release(jedis);
}
}
public long lpush(String key, String... values) {
ShardedJedis jedis = fetchJedis();
try {
......@@ -200,6 +214,56 @@ public class RedisTemplate {
}
}
public boolean hexists(String key, String field) {
ShardedJedis jedis = fetchJedis();
try {
return jedis.hexists(key, field);
} finally {
release(jedis);
}
}
public long hdel(String key, String... fields) {
ShardedJedis jedis = fetchJedis();
try {
return jedis.hdel(key, fields);
} finally {
release(jedis);
}
}
public <T> void set(String key, T value, int seconds) throws Exception {
ShardedJedis jedis = fetchJedis();
try {
jedis.setex(key.getBytes(), seconds, this.serialize(value));
} finally {
release(jedis);
}
}
public <T> void set(String key, T value) throws Exception {
ShardedJedis jedis = fetchJedis();
try {
jedis.set(key.getBytes(), this.serialize(value));
} finally {
release(jedis);
}
}
public <T> T get(String key, Class<T> clazz) {
byte[] value = this.getBytes(key);
return this.deserialize(value, clazz);
}
public byte[] getBytes(String key) {
ShardedJedis jedis = fetchJedis();
try {
return jedis.get(key.getBytes());
} finally {
release(jedis);
}
}
public void setShardedJedisPool(ShardedJedisPool shardedJedisPool) {
this.shardedJedisPool = shardedJedisPool;
}
......@@ -213,4 +277,21 @@ public class RedisTemplate {
jedis.close();
}
}
public void setObjectMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
private <T> byte[] serialize(T value) throws Exception {
return objectMapper.writeValueAsBytes(value);
}
private <T> T deserialize(byte[] value, Class<T> clazz) {
try {
return objectMapper.readValue(value, clazz);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
}
package org.hongxi.whatsmars.redis.client.service;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
......@@ -42,6 +43,22 @@ public interface RedisService {
Set<String> smembers(String key);
boolean sismember(String key, String value);
void hset(String key, String hashKey, String value);
String hget(String key, String hashKey);
boolean hexists(String key, String hashKey);
void hdel(String key, String... hashKeys);
long leftPush(String key, Serializable value);
Serializable rightPop(String key);
List<Serializable> range(String key, long start, long end);
void convertAndSend(String channel, Object obj);
void delete(String key);
......
......@@ -2,6 +2,7 @@ package org.hongxi.whatsmars.redis.client.service.impl;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.hongxi.whatsmars.redis.client.service.RedisService;
import org.springframework.dao.DataAccessException;
......@@ -30,6 +31,8 @@ public class RedisServiceImpl implements RedisService {
private RedisTemplate<String, Serializable> redisTemplate;
@Resource
private StringRedisTemplate stringRedisTemplate;
@Setter
private ObjectMapper objectMapper = new ObjectMapper();
@Override
public void set(String key, String value) {
......@@ -111,6 +114,49 @@ public class RedisServiceImpl implements RedisService {
return stringRedisTemplate.opsForSet().members(key);
}
@Override
public boolean sismember(String key, String value) {
return stringRedisTemplate.opsForSet().isMember(key, value);
}
@Override
public void hset(String key, String hashKey, String value) {
redisTemplate.opsForHash().put(key, hashKey, value);
}
@Override
public String hget(String key, String hashKey) {
if (hexists(key, hashKey)) {
return redisTemplate.opsForHash().get(key, hashKey).toString();
}
return null;
}
@Override
public boolean hexists(String key, String hashKey) {
return redisTemplate.opsForHash().hasKey(key, hashKey);
}
@Override
public void hdel(String key, String... hashKeys) {
redisTemplate.opsForHash().delete(key, hashKeys);
}
@Override
public long leftPush(String key, Serializable value) {
return redisTemplate.opsForList().leftPush(key, value);
}
@Override
public Serializable rightPop(String key) {
return redisTemplate.opsForList().rightPop(key);
}
@Override
public List<Serializable> range(String key, long start, long end) {
return redisTemplate.opsForList().range(key, start, end);
}
@Override
public void convertAndSend(String channel, Object obj) {
assert obj != null;
......@@ -150,22 +196,8 @@ public class RedisServiceImpl implements RedisService {
@Override
public <T> T get(String key, Class<T> clazz) {
return redisTemplate.execute(new RedisCallback<T>() {
@Override
public T doInRedis(RedisConnection connection) throws DataAccessException {
byte[] value = connection.get(key.getBytes());
if (value == null) {
return null;
}
ObjectMapper objectMapper = new ObjectMapper();
try {
return objectMapper.readValue(value, clazz);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
});
byte[] value = this.getBytes(key);
return this.deserialize(value, clazz);
}
@Override
......@@ -201,8 +233,16 @@ public class RedisServiceImpl implements RedisService {
}
private <T> byte[] serialize(T value) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsBytes(value);
}
private <T> T deserialize(byte[] value, Class<T> clazz) {
try {
return objectMapper.readValue(value, clazz);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
}
......@@ -55,5 +55,13 @@ public class InitRunner implements CommandLineRunner {
redisService.sadd("countries", "China", "America", "Japan");
System.out.println(redisTemplate.opsForSet().isMember("countries", "China")); // fasle
System.out.println(stringRedisTemplate.opsForSet().isMember("countries", "China")); // true
redisService.hset("menu", "A", "a");
System.out.println(redisService.hget("menu", "A"));
redisService.leftPush("message", "haha");
redisService.leftPush("message", user);
System.out.println(redisService.rightPop("message"));
System.out.println(redisService.rightPop("message"));
}
}
......@@ -2,12 +2,14 @@ package org.hongxi.whatsmars.spring.boot.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.Serializable;
import java.util.Date;
/**
* Created by shenhongxi on 2017/6/26.
*/
public class User {
public class User implements Serializable {
private static final long serialVersionUID = 6469790070035101683L;
@JsonProperty("user_id") // 有时候前端定义的字段名与后端不一致时采用此法处理
private Long id;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册