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

完善RedisService

上级 f0347874
......@@ -15,12 +15,14 @@ public interface RedisMapService {
List<byte[]> hMGet(String redisKey, List<String> mapKeys);
byte[] hGet(String redisKey, String mapKey);
long hDel(String redisKey, List<String> mapKeys);
long hDel(String redisKey, byte[] mapKey);
boolean hSet(String redisKey, String mapKey, String mapValue);
String hGetString(String redisKey, String mapKey);
String hGet(String redisKey, byte[] mapKey);
}
package org.hongxi.whatsmars.redis.client.service;
import java.util.Collection;
import java.util.List;
import java.util.Set;
public interface RedisService<T> {
......@@ -110,4 +111,8 @@ public interface RedisService<T> {
void pubMsg(String channel, Object obj);
Set<String> getSet(String key);
long addSet(String key, String... values);
List<T> multiGet(Collection keys);
}
......@@ -17,9 +17,8 @@ public class RedisListServiceImpl implements RedisListService {
private RedisTemplate<String, List<Object>> redisTemplate;
@Override
public long rpush(final byte[] key, final byte[]...values) {
public long rpush(final byte[] key, final byte[]... values) {
return redisTemplate.execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection connection) throws DataAccessException {
return connection.rPush(key, values);
......@@ -38,7 +37,6 @@ public class RedisListServiceImpl implements RedisListService {
@Override
public List<byte[]> lrange(final byte[] key, final long begin, final long end) {
return redisTemplate.execute(new RedisCallback<List<byte[]>>() {
@Override
public List<byte[]> doInRedis(RedisConnection connection) throws DataAccessException {
return connection.lRange(key, begin, end);
......@@ -59,7 +57,6 @@ public class RedisListServiceImpl implements RedisListService {
@Override
public long del(final byte[] key) {
return redisTemplate.execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection connection) throws DataAccessException {
return connection.del(key);
......
......@@ -9,6 +9,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.Set;
......@@ -16,6 +17,8 @@ import java.util.Set;
@Service("redisMapService")
public class RedisMapServiceImpl implements RedisMapService {
private static final String CHARSET = "UTF8";
@Resource
private RedisTemplate<String, Map<byte[], byte[]>> redisTemplate;
......@@ -112,6 +115,17 @@ public class RedisMapServiceImpl implements RedisMapService {
});
}
@Override
public String hGetString(String redisKey, String mapKey) {
byte[] value = hGet(redisKey, mapKey);
try {
return value == null ? null : new String(value, CHARSET);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
@Override
public String hGet(final String redisKey, final byte[] mapKey) {
return redisTemplate.execute(new RedisCallback<String>() {
......
......@@ -20,6 +20,7 @@ import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
@Service("redisService")
......@@ -60,15 +61,16 @@ public class RedisServiceImpl<T> implements RedisService<T> {
public boolean set(byte[] key, byte[] value) {
return this.set(key, value, 0L);
}
@Override
public boolean set(byte[] key, T value, long activeTime){
ObjectMapper objectMapper = new ObjectMapper();
byte[] b = null;
try {
b = objectMapper.writeValueAsBytes(value);
} catch (JsonProcessingException e) {
e.printStackTrace();
return false;
}
return this.set(key, b, activeTime);
}
......@@ -79,11 +81,11 @@ public class RedisServiceImpl<T> implements RedisService<T> {
public String doInRedis(RedisConnection connection) throws DataAccessException {
try {
byte[] value = connection.get(key.getBytes());
return value == null ? "" : new String(value, CHARSET);
return value == null ? null : new String(value, CHARSET);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
return null;
}
});
}
......@@ -100,13 +102,10 @@ public class RedisServiceImpl<T> implements RedisService<T> {
try {
return objectMapper.readValue(value, c);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
......@@ -117,7 +116,6 @@ public class RedisServiceImpl<T> implements RedisService<T> {
@Override
public Set<String> matchKeys(String pattern) {
return redisTemplate.keys(pattern);
}
@Override
......@@ -145,7 +143,7 @@ public class RedisServiceImpl<T> implements RedisService<T> {
public Long doInRedis(RedisConnection connection) throws DataAccessException {
long result = 0;
for (String key : keys) {
result = connection.del(key.getBytes());
result += connection.del(key.getBytes());
}
return result;
}
......@@ -182,4 +180,14 @@ public class RedisServiceImpl<T> implements RedisService<T> {
return stringRedisTemplate.opsForSet().members(key);
}
@Override
public long addSet(String key, String... values) {
return stringRedisTemplate.opsForSet().add(key, values);
}
@Override
public List<T> multiGet(Collection keys) {
return redisTemplate.opsForValue().multiGet(keys);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册