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

RedisService封装简化

上级 f337413c
......@@ -32,10 +32,10 @@ public class RedisTemplate {
}
}
public void set(String key, String value, int activeTime) {
public void set(String key, String value, int seconds) {
ShardedJedis jedis = fetchJedis();
try {
jedis.setex(key, activeTime, value);
jedis.setex(key, seconds, value);
} finally {
release(jedis);
}
......
package org.hongxi.whatsmars.redis.client.service;
import java.util.List;
public interface RedisListService {
long rpush(byte[] key, byte[]... values);
long rpush(String key, byte[]... values);
List<byte[]> lrange(byte[] key, long begin, long end);
List<byte[]> lrange(String key, long begin, long end);
List<byte[]> lrange(String key);
}
package org.hongxi.whatsmars.redis.client.service;
import java.util.List;
import java.util.Map;
import java.util.Set;
public interface RedisMapService {
boolean hMSet(String redisKey, Map<byte[], byte[]> valueMap);
Set<byte[]> hKeys(String redisKey);
Map<byte[], byte[]> hGetAll(String redisKey);
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);
}
......@@ -2,122 +2,63 @@ package org.hongxi.whatsmars.redis.client.service;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
public interface RedisService<T> {
/**
* <pre>
*
* 通过key删除
* &#64;param keys
* &#64;return 被删除的记录数
* </pre>
*/
long delete(String... keys);
/**
* <pre>
*
* 通过keys删除
* &#64;param keys
* &#64;return 被删除的记录数
* </pre>
*/
long delete(Collection<String> keys);
/**
* <pre>
*
* &#64;param key
* &#64;param value
* &#64;param activeTime 秒
* &#64;return 添加key value 并且设置存活时间
* </pre>
*/
boolean set(byte[] key, byte[] value, long activeTime);
/**
* <pre>
*
* &#64;param key
* &#64;param value
* &#64;param activeTime 秒
* &#64;return 添加key value 并且设置存活时间
* </pre>
*/
boolean set(String key, String value, long activeTime);
/**
* <pre>
*
* &#64;param key
* &#64;param value
* &#64;return 添加key value
* </pre>
*/
boolean set(String key, String value);
/**
* <pre>
*
* &#64;param key
* &#64;param value
* &#64;return 添加key value
* </pre>
*/
boolean set(byte[] key, byte[] value);
boolean set(byte[] key, T value, long activeTime);
boolean set(String key, T value, long activeTime);
boolean set(String key, T value);
/**
* <pre>
*
* &#64;param key
* &#64;return 获得value
* </pre>
*/
String get(String key);
T getObject(String key, Class<T> c);
byte[] getBytes(String key);
/**
* <pre>
*
* &#64;param pattern
* &#64;return 通过正则匹配keys
* </pre>
*/
Set<String> matchKeys(String pattern);
/**
* <pre>
*
* &#64;param key
* &#64;return 检查key是否已经存在
* </pre>
*/
boolean exists(String key);
/**
* <pre>
*
* &#64;return 清空所有数据
* </pre>
*/
boolean flushDB();
/**
* <pre>
*
* &#64;param keys
* &#64;return 批量获取key的值
* </pre>
*/
List<T> multiGet(Collection keys);
/**
* Created by shenhongxi on 2018/12/24.
*/
public interface RedisService {
void set(String key, String value);
void set(String key, String value, long seconds);
void setIfAbsent(String key, String value);
String get(String key);
String getAndSet(String key, String value);
void multiSet(Map<String, String> map);
List<String> multiGet(Collection<String> keys);
Long increment(String key, long delta);
Double increment(String key, double delta);
Integer append(String key, String value);
String get(String key, long start, long end);
Long size(String key);
Boolean setBit(String key, long offset, boolean value);
Boolean getBit(String key, long offset);
long sadd(String key, String... values);
Set<String> smembers(String key);
void convertAndSend(String channel, Object obj);
void delete(String key);
boolean exists(String key);
Set<String> matchKeys(String pattern);
<T> void set(String key, T value, long seconds) throws Exception;
<T> void set(String key, T value) throws Exception;
<T> T get(String key, Class<T> clazz);
boolean set(byte[] key, byte[] value, long seconds);
boolean set(byte[] key, byte[] value);
byte[] getBytes(String key);
}
package org.hongxi.whatsmars.redis.client.service;
import org.springframework.data.redis.connection.RedisZSetCommands.Tuple;
import java.util.Set;
public interface RedisSortedSetService {
long zadd(String key, Set<Tuple> tuples);
long zadd(byte[] key, Set<Tuple> tuples);
Set<byte[]> zrange(byte[] key, int start, int end);
Set<byte[]> zrange(String key);
Set<byte[]> zrange(String key, int start, int end);
}
package org.hongxi.whatsmars.redis.client.service;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Created by shenhongxi on 2018/12/24.
*/
public interface RedisStringService {
void set(String key, String value);
void set(String key, String value, long activeTime);
void setIfAbsent(String key, String value);
String get(String key);
String getAndSet(String key, String value);
void multiSet(Map<String, String> map);
List<String> multiGet(Collection<String> keys);
Long increment(String key, long delta);
Double increment(String key, double delta);
Integer append(String key, String value);
String get(String key, long start, long end);
Long size(String key);
Boolean setBit(String key, long offset, boolean value);
Boolean getBit(String key, long offset);
long sadd(String key, String... values);
Set<String> smembers(String key);
void convertAndSend(String channel, Object obj);
void delete(String key);
}
package org.hongxi.whatsmars.redis.client.service.impl;
import org.hongxi.whatsmars.redis.client.service.RedisListService;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class RedisListServiceImpl implements RedisListService {
@Resource
private RedisTemplate<String, List<Object>> redisTemplate;
@Override
public long rpush(byte[] key, byte[]... values) {
return redisTemplate.execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection connection) throws DataAccessException {
return connection.rPush(key, values);
}
});
}
@Override
public long rpush(String key, byte[]... values) {
if(values == null || values.length <= 0){
return 0;
}
return this.rpush(key.getBytes(), values);
}
@Override
public List<byte[]> lrange(byte[] key, long begin, long end) {
return redisTemplate.execute(new RedisCallback<List<byte[]>>() {
@Override
public List<byte[]> doInRedis(RedisConnection connection) throws DataAccessException {
return connection.lRange(key, begin, end);
}
});
}
@Override
public List<byte[]> lrange(String key, long begin, long end) {
return this.lrange(key.getBytes(), begin, end);
}
@Override
public List<byte[]> lrange(String key) {
return this.lrange(key, 0, -1);
}
}
package org.hongxi.whatsmars.redis.client.service.impl;
import lombok.extern.slf4j.Slf4j;
import org.hongxi.whatsmars.redis.client.service.RedisMapService;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
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;
@Slf4j
@Service
public class RedisMapServiceImpl implements RedisMapService {
private static final String CHARSET = "UTF8";
@Resource
private RedisTemplate<String, Map<byte[], byte[]>> redisTemplate;
@Override
public boolean hMSet(String redisKey, Map<byte[], byte[]> valueMap) {
if(CollectionUtils.isEmpty(valueMap)){
return false;
}
redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
connection.hMSet(redisKey.getBytes(), valueMap);
return true;
}
});
return false;
}
@Override
public Set<byte[]> hKeys(String redisKey) {
return redisTemplate.execute(new RedisCallback<Set<byte[]>>() {
@Override
public Set<byte[]> doInRedis(RedisConnection connection) throws DataAccessException {
return connection.hKeys(redisKey.getBytes());
}
});
}
@Override
public Map<byte[], byte[]> hGetAll(String redisKey) {
return redisTemplate.execute(new RedisCallback<Map<byte[], byte[]>>() {
@Override
public Map<byte[], byte[]> doInRedis(RedisConnection connection) throws DataAccessException {
return connection.hGetAll(redisKey.getBytes());
}
});
}
@Override
public List<byte[]> hMGet(String redisKey, List<String> mapKeys) {
return redisTemplate.execute(new RedisCallback<List<byte[]>>() {
@Override
public List<byte[]> doInRedis(RedisConnection connection) throws DataAccessException {
byte[][] keys = new byte[mapKeys.size()][];
for (int i = 0; i < mapKeys.size(); i++) {
keys[i] = mapKeys.get(i).getBytes();
}
return connection.hMGet(redisKey.getBytes(), keys);
}
});
}
@Override
public byte[] hGet(String redisKey, String mapKey) {
return redisTemplate.execute(new RedisCallback<byte[]>() {
@Override
public byte[] doInRedis(RedisConnection connection) throws DataAccessException {
return connection.hGet(redisKey.getBytes(), mapKey.getBytes());
}
});
}
@Override
public long hDel(String redisKey, List<String> mapKeys) {
return redisTemplate.execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection connection) throws DataAccessException {
byte[][] keys = new byte[mapKeys.size()][];
for (int i = 0; i < mapKeys.size(); i++) {
keys[i] = mapKeys.get(i).getBytes();
}
return connection.hDel(redisKey.getBytes(), keys);
}
});
}
@Override
public boolean hSet(String redisKey, String mapKey, String mapValue) {
return redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
return connection.hSet(redisKey.getBytes(), mapKey.getBytes(), mapValue.getBytes());
}
});
}
@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) {
log.error(e.getMessage(), e);
}
return null;
}
@Override
public String hGet(String redisKey, byte[] mapKey) {
return redisTemplate.execute(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
byte[] value = connection.hGet(redisKey.getBytes(), mapKey);
if(null == value){
return null;
}
return new String(value);
}
});
}
@Override
public long hDel(String redisKey, byte[] mapKey) {
return redisTemplate.execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection connection) throws DataAccessException {
return connection.hDel(redisKey.getBytes(), mapKey);
}
});
}
}
package org.hongxi.whatsmars.redis.client.service.impl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.hongxi.whatsmars.redis.client.service.RedisService;
......@@ -8,169 +8,201 @@ import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* Created by shenhongxi on 2018/12/24.
*/
@Slf4j
@Service
public class RedisServiceImpl<T> implements RedisService<T> {
private static final String CHARSET = "UTF8";
@Resource
private RedisTemplate<String, Serializable> redisTemplate;
@Override
public boolean set(byte[] key, byte[] value, long activeTime) {
return redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
boolean rs = true;
connection.set(key, value);
if (activeTime > 0) {
rs = connection.expire(key, activeTime);
}
return rs;
}
});
}
@Override
public boolean set(String key, String value, long activeTime) {
return this.set(key.getBytes(), value.getBytes(), activeTime);
}
@Override
public boolean set(String key, String value) {
return this.set(key, value, 0L);
}
@Override
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) {
log.error(e.getMessage(), e);
return false;
}
return this.set(key, b, activeTime);
}
@Override
public boolean set(String key, T value, long activeTime) {
return this.set(key.getBytes(), value, activeTime);
}
@Override
public boolean set(String key, T value) {
return this.set(key, value, 0L);
}
@Override
public String get(String key) {
return redisTemplate.execute(new RedisCallback<String>() {
public String doInRedis(RedisConnection connection) throws DataAccessException {
try {
byte[] value = connection.get(key.getBytes());
return value == null ? null : new String(value, CHARSET);
} catch (UnsupportedEncodingException e) {
log.error(e.getMessage(), e);
}
return null;
}
});
}
@Override
public T getObject(String key, Class<T> c) {
return redisTemplate.execute(new RedisCallback<T>() {
public T doInRedis(RedisConnection connection) throws DataAccessException {
byte[] value = connection.get(key.getBytes());
if(null == value){
return null;
}
ObjectMapper objectMapper = new ObjectMapper();
try {
return objectMapper.readValue(value, c);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
});
}
@Override
public Set<String> matchKeys(String pattern) {
return redisTemplate.keys(pattern);
}
@Override
public boolean exists(String key) {
return redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
return connection.exists(key.getBytes());
}
});
}
@Override
public boolean flushDB() {
return redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
connection.flushDb();
return true;
}
});
}
@Override
public long delete(Collection<String> keys) {
return redisTemplate.execute(new RedisCallback<Long>() {
public Long doInRedis(RedisConnection connection) throws DataAccessException {
long result = 0;
for (String key : keys) {
result += connection.del(key.getBytes());
}
return result;
}
});
}
@Override
public long delete(String... keys) {
Collection<String> cols = new ArrayList<String>();
for (String key : keys) {
cols.add(key);
}
return this.delete(cols);
}
@Override
public byte[] getBytes(String key) {
return redisTemplate.execute(new RedisCallback<byte[]>() {
public byte[] doInRedis(RedisConnection connection) throws DataAccessException {
return connection.get(key.getBytes());
}
});
}
@Override
public List<T> multiGet(Collection keys) {
return redisTemplate.opsForValue().multiGet(keys);
}
public class RedisServiceImpl implements RedisService {
@Resource
private RedisTemplate<String, Serializable> redisTemplate;
@Resource
private StringRedisTemplate stringRedisTemplate;
@Override
public void set(String key, String value) {
stringRedisTemplate.opsForValue().set(key, value);
}
@Override
public void set(String key, String value, long seconds) {
stringRedisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
}
@Override
public void setIfAbsent(String key, String value) {
stringRedisTemplate.opsForValue().setIfAbsent(key, value);
}
@Override
public String get(String key) {
return stringRedisTemplate.opsForValue().get(key);
}
@Override
public String getAndSet(String key, String value) {
return stringRedisTemplate.opsForValue().getAndSet(key, value);
}
@Override
public void multiSet(Map<String, String> map) {
stringRedisTemplate.opsForValue().multiSet(map);
}
@Override
public List<String> multiGet(Collection<String> keys) {
return stringRedisTemplate.opsForValue().multiGet(keys);
}
@Override
public Long increment(String key, long delta) {
return stringRedisTemplate.opsForValue().increment(key, delta);
}
@Override
public Double increment(String key, double delta) {
return stringRedisTemplate.opsForValue().increment(key, delta);
}
@Override
public Integer append(String key, String value) {
return stringRedisTemplate.opsForValue().append(key, value);
}
@Override
public String get(String key, long start, long end) {
return stringRedisTemplate.opsForValue().get(key, start, end);
}
@Override
public Long size(String key) {
return stringRedisTemplate.opsForValue().size(key);
}
@Override
public Boolean setBit(String key, long offset, boolean value) {
return stringRedisTemplate.opsForValue().setBit(key, offset, value);
}
@Override
public Boolean getBit(String key, long offset) {
return stringRedisTemplate.opsForValue().getBit(key, offset);
}
@Override
public long sadd(String key, String... values) {
return stringRedisTemplate.opsForSet().add(key, values);
}
@Override
public Set<String> smembers(String key) {
return stringRedisTemplate.opsForSet().members(key);
}
@Override
public void convertAndSend(String channel, Object obj) {
assert obj != null;
String msg = obj instanceof String ? String.valueOf(obj) : JSON.toJSONString(obj);
stringRedisTemplate.convertAndSend(channel, msg);
}
@Override
public void delete(String key) {
stringRedisTemplate.delete(key);
}
@Override
public boolean exists(String key) {
return redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
return connection.exists(key.getBytes());
}
});
}
@Override
public Set<String> matchKeys(String pattern) {
return redisTemplate.keys(pattern);
}
@Override
public <T> void set(String key, T value, long seconds) throws Exception {
this.set(key.getBytes(), serialize(value), seconds);
}
@Override
public <T> void set(String key, T value) throws Exception {
this.set(key.getBytes(), serialize(value));
}
@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;
}
});
}
@Override
public boolean set(byte[] key, byte[] value, long seconds) {
return redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
connection.setEx(key, seconds, value);
return true;
}
});
}
@Override
public boolean set(byte[] key, byte[] value) {
return redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
connection.set(key, value);
return true;
}
});
}
@Override
public byte[] getBytes(String key) {
return redisTemplate.execute(new RedisCallback<byte[]>() {
@Override
public byte[] doInRedis(RedisConnection connection) throws DataAccessException {
return connection.get(key.getBytes());
}
});
}
private <T> byte[] serialize(T value) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsBytes(value);
}
}
package org.hongxi.whatsmars.redis.client.service.impl;
import org.hongxi.whatsmars.redis.client.service.RedisSortedSetService;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisZSetCommands.Tuple;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Set;
@Service
public class RedisSortedSetServiceImpl implements RedisSortedSetService {
@Resource
private RedisTemplate<String, Set<Object>> redisTemplate;
@Override
public long zadd(byte[] key, Set<Tuple> tuples) {
redisTemplate.execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection connection) throws DataAccessException {
return connection.zAdd(key, tuples);
}
});
return 0;
}
@Override
public Set<byte[]> zrange(byte[] key, int start, int end) {
return redisTemplate.execute(new RedisCallback<Set<byte[]>>() {
@Override
public Set<byte[]> doInRedis(RedisConnection connection) throws DataAccessException {
return connection.zRange(key, start, end);
}
});
}
@Override
public long zadd(String key, Set<Tuple> tuples) {
return this.zadd(key.getBytes(), tuples);
}
@Override
public Set<byte[]> zrange(String key) {
return this.zrange(key, 0, -1);
}
@Override
public Set<byte[]> zrange(String key, int start, int end) {
return this.zrange(key.getBytes(), start, end);
}
}
package org.hongxi.whatsmars.redis.client.service.impl;
import com.alibaba.fastjson.JSON;
import org.hongxi.whatsmars.redis.client.service.RedisStringService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* Created by shenhongxi on 2018/12/24.
*/
@Service
public class RedisStringServiceImpl implements RedisStringService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Override
public void set(String key, String value) {
stringRedisTemplate.opsForValue().set(key, value);
}
@Override
public void set(String key, String value, long activeTime) {
stringRedisTemplate.opsForValue().set(key, value, activeTime, TimeUnit.SECONDS);
}
@Override
public void setIfAbsent(String key, String value) {
stringRedisTemplate.opsForValue().setIfAbsent(key, value);
}
@Override
public String get(String key) {
return stringRedisTemplate.opsForValue().get(key);
}
@Override
public String getAndSet(String key, String value) {
return stringRedisTemplate.opsForValue().getAndSet(key, value);
}
@Override
public void multiSet(Map<String, String> map) {
stringRedisTemplate.opsForValue().multiSet(map);
}
@Override
public List<String> multiGet(Collection<String> keys) {
return stringRedisTemplate.opsForValue().multiGet(keys);
}
@Override
public Long increment(String key, long delta) {
return stringRedisTemplate.opsForValue().increment(key, delta);
}
@Override
public Double increment(String key, double delta) {
return stringRedisTemplate.opsForValue().increment(key, delta);
}
@Override
public Integer append(String key, String value) {
return stringRedisTemplate.opsForValue().append(key, value);
}
@Override
public String get(String key, long start, long end) {
return stringRedisTemplate.opsForValue().get(key, start, end);
}
@Override
public Long size(String key) {
return stringRedisTemplate.opsForValue().size(key);
}
@Override
public Boolean setBit(String key, long offset, boolean value) {
return stringRedisTemplate.opsForValue().setBit(key, offset, value);
}
@Override
public Boolean getBit(String key, long offset) {
return stringRedisTemplate.opsForValue().getBit(key, offset);
}
@Override
public long sadd(String key, String... values) {
return stringRedisTemplate.opsForSet().add(key, values);
}
@Override
public Set<String> smembers(String key) {
return stringRedisTemplate.opsForSet().members(key);
}
@Override
public void convertAndSend(String channel, Object obj) {
assert obj != null;
String msg = obj instanceof String ? String.valueOf(obj) : JSON.toJSONString(obj);
stringRedisTemplate.convertAndSend(channel, msg);
}
@Override
public void delete(String key) {
stringRedisTemplate.delete(key);
}
}
package org.hongxi.whatsmars.spring.boot.controller;
import org.hongxi.whatsmars.redis.client.service.RedisStringService;
import org.hongxi.whatsmars.redis.client.service.RedisService;
import org.hongxi.whatsmars.spring.boot.async.MessageService;
import org.hongxi.whatsmars.spring.boot.dao.UserMapper;
import org.hongxi.whatsmars.spring.boot.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
/**
......@@ -24,7 +27,11 @@ public class InitRunner implements CommandLineRunner {
@Autowired
private MessageService messageService;
@Autowired
private RedisStringService redisStringService;
private RedisService redisService;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Override
public void run(String... args) throws Exception {
......@@ -36,7 +43,17 @@ public class InitRunner implements CommandLineRunner {
}
String key = "domain";
redisStringService.set(key, "hongxi.org", 60); // 60s
System.out.println(redisStringService.get(key));
redisService.set(key, "hongxi.org", 60); // 60s
System.out.println(redisService.get(key));
User user = new User();
user.setUsername("javahongxi");
user.setNickname("红喜");
redisService.set("user", user);
System.out.println(redisService.get("user"));
System.out.println(redisService.get("user", User.class));
redisService.sadd("countries", "China", "America", "Japan");
System.out.println(redisTemplate.opsForSet().isMember("countries", "China")); // fasle
System.out.println(stringRedisTemplate.opsForSet().isMember("countries", "China")); // true
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册