Springboot使用redis,配置类、工具类、测试类
阿里
阅读:860
2021-04-01 11:07:57
评论:0
1.redis配置类设置序列化类
@Configuration
public class RedisConfig
{
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory)
{
RedisTemplate<String, Object> template;
StringRedisSerializer stringSerializer;
Jackson2JsonRedisSerializer<Object> jsonSerializer;
ObjectMapper objMapper = new ObjectMapper();
//配置String序列化器
stringSerializer = new StringRedisSerializer();
//配置Json序列化器
jsonSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
objMapper = new ObjectMapper();
objMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); //指定要序列化的范围。get/set..., private/public...
objMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); //指定序列化输入的类型。必须是非final类型,其它如String等会抛异常
jsonSerializer.setObjectMapper(objMapper);
//配置模板
template = new RedisTemplate<>();
template.setConnectionFactory(factory); //连接工厂
//设置的Key和Value序列化模式
template.setKeySerializer(stringSerializer);
template.setValueSerializer(jsonSerializer); //jsonSerializer
//设置Map的Key和Value序列化模式
template.setHashKeySerializer(stringSerializer);
template.setHashValueSerializer(jsonSerializer);
template.afterPropertiesSet();
return template;
}
}
2.redisUtil工具类
@Component
public class RedisUtil {
@Autowired
private RedisTemplate<String, Object> redisTemplate; // 必须与RedisConfig中的类型定义一致,否则注入失败,导致SB启动失败
// @Autowired
// StringRedisTemplate redisTemplate; //也可以使用SpringBoot提供的模板
public RedisUtil() {
// redisTemplate = SpringContextUtil.getBean(RedisTemplate.class);
}
// ======== 通用的方法 ================================================================
/** 读普通值(返回Object) */
public Object get(String key) {
return StringUtils.isEmpty(key) ? null : redisTemplate.opsForValue().get(key);
}
/** 读普通值(返回String) */
public String getStr(String key) {
Object val;
val = StringUtils.isEmpty(key) ? null : redisTemplate.opsForValue().get(key);
return (val == null) ? null : val.toString();
}
/**
* 按Key前缀获取一批Key及其对应的值
*
* @return [k]=key [v]=value
*/
public Map<String, Object> getByPrefix(String keyPrefix) {
Map<String, Object> result;
Set<String> keys;
result = new HashMap<String, Object>();
keys = redisTemplate.keys(keyPrefix + "");
for (String key : keys)
result.put(key, get(key));
return result;
}
/** 写普通值 */
public boolean set(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
return true;
}
/**
* 写普通值并设置失效时间
*
* @param timeout 失效时间(秒)。小于等于0表用不失效
*/
public boolean set(String key, Object value, long timeout) {
if (timeout > 0)
redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
else
set(key, value);
return true;
}
// 存储Hash操作
/**
* 往Hash中存入数据
*
* @param key Redis键
* @param hKey Hash键
* @param value 值
*/
public void hPut(String key, String hKey, Object value) {
redisTemplate.opsForHash().put(key, hKey, value);
}
/**
* 往Hash中存入多个数据
*
* @param key Redis键
* @param values Hash键值对
*/
public void hPutAll(String key, Map<String, Object> values) {
redisTemplate.opsForHash().putAll(key, values);
}
/**
* 获取Hash中的数据
*
* @param key Redis键
* @param hKey Hash键
* @return Hash中的对象
*/
public Object hGet(String key, String hKey) {
return redisTemplate.opsForHash().get(key, hKey);
}
/**
* 获取多个Hash中的数据
*
* @param key Redis键
* @param hKeys Hash键集合
* @return Hash对象集合
*/
public List<Object> hGetMulti(String key, Collection<Object> hKeys) {
return redisTemplate.opsForHash().multiGet(key, hKeys);
}
public Map<Object, Object> hGetEntries(String key) {
Map<Object, Object> entries = redisTemplate.opsForHash().entries(key);
return entries;
}
// 存储Set相关操作
/**
* 往Set中存入数据
*
* @param key Redis键
* @param values 值
* @return 存入的个数
*/
public long sSet(String key, Object... values) {
Long count = redisTemplate.opsForSet().add(key, values);
return count == null ? 0 : count;
}
/**
* 删除Set中的数据
*
* @param key Redis键
* @param values 值
* @return 移除的个数
*/
public long sDel(String key, Object... values) {
Long count = redisTemplate.opsForSet().remove(key, values);
return count == null ? 0 : count;
}
// 存储List相关操作
/**
* 往List右侧存入数据
*
* @param key Redis键
* @param value 数据
* @return 当前List中元素的个数
*/
public long lPushR(String key, Object value) {
Long count = redisTemplate.opsForList().rightPush(key, value);
return count == null ? 0 : count;
}
/**
* 往List左侧存入数据
*
* @param key Redis键
* @param value 数据
* @return 当前List中元素的个数
*/
public long lPushL(String key, Object value) {
Long count = redisTemplate.opsForList().leftPush(key, value);
return count == null ? 0 : count;
}
/**
* 往List右侧存入Collection
*
* @param key Redis键
* @param values Collection
* @return 当前List中元素的个数
*/
public long lPushRAll(String key, Collection<Object> values) {
Long count = redisTemplate.opsForList().rightPushAll(key, values);
return count == null ? 0 : count;
}
/**
* 往List左侧存入Collection
*
* @param key Redis键
* @param values Collection
* @return 当前List中元素的个数
*/
public long lPushLAll(String key, Collection<Object> values) {
Long count = redisTemplate.opsForList().leftPushAll(key, values);
return count == null ? 0 : count;
}
/**
* 往List右侧存入多个数据
*
* @param key Redis键
* @param values 多个数据
* @return 当前List中元素的个数
*/
public long lPushRAll(String key, Object... values) {
Long count = redisTemplate.opsForList().rightPushAll(key, values);
return count == null ? 0 : count;
}
/**
* 往List左侧存入多个数据
*
* @param key Redis键
* @param values 多个数据
* @return 当前List中元素的个数
*/
public long lPushLAll(String key, Object... values) {
Long count = redisTemplate.opsForList().leftPushAll(key, values);
return count == null ? 0 : count;
}
/**
* 从List中获取begin到end之间的元素
*
* @param key Redis键
* @param start 开始位置
* @param end 结束位置(start=0,end=-1表示获取全部元素),前后索引都包含
* @return List对象
*/
public List<Object> lGetRange(String key, int start, int end) {
return redisTemplate.opsForList().range(key, start, end);
}
/**
* 从List中获取指定索引数据
*
* @param key
* @param index
* @return
*/
public Object lGetByIndex(String key, long index) {
return redisTemplate.opsForList().index(key, index);
}
}
3.测试工具类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Starter.class)
public class RedisUtilsTest {
@Autowired
private RedisUtil redisUtil;
@Test
public void set() {
redisUtil.set("keyString", "12");
}
@Test
public void get() {
Object object = redisUtil.get("keyString");
System.out.println(object);
}
@Test
public void hPut() {
redisUtil.hPut("hAll", "e", "value");
}
@Test
public void hPutAll() {
Map<String, Object> map = new HashMap<>();
map.put("a", "aa");
map.put("b", 13);
map.put("c", 2.3);
Map<String, Object> map2 = new HashMap<>();
map2.put("ccc", "cValue");
map.put("c", map2);
map.put("d", true);
redisUtil.hPutAll("hAll", map);
}
@Test
public void hGet() {
String a = (String) redisUtil.hGet("hAll", "a");
Integer b = (Integer) redisUtil.hGet("hAll", "b");
@SuppressWarnings("unchecked")
Map<String, String> c = (Map<String, String>) redisUtil.hGet("hAll", "c");
Boolean d = (Boolean) redisUtil.hGet("hAll", "d");
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
}
@Test
public void hGetMulti() {
Collection<Object> keys = new ArrayList<>();
keys.add("a");
keys.add("b");
keys.add("c");
keys.add("d");
keys.add("e");
List<Object> hMultiGet = redisUtil.hGetMulti("hAll", keys);
System.out.println(JSONObject.toJSONString(hMultiGet));
}
@Test
public void hGetEntries() {
Map<Object, Object> hEntriesGet = redisUtil.hGetEntries("hAll");
System.out.println(hEntriesGet);
}
@Test
public void sSet() {
long SetString = redisUtil.sSet("set", "a", "b", "c", "d", "e");
System.out.println("SetString:" + SetString);
}
@Test
public void sDel() {
long SetString = redisUtil.sDel("set", "a", "b", "c");
System.out.println("SetString:" + SetString);
}
@Test
public void lPushL() {
long lPush = redisUtil.lPushL("list", "c");
System.out.println("list:" + lPush);
}
@Test
public void lPushR() {
long lPush = redisUtil.lPushR("list", "h");
System.out.println("list:" + lPush);
}
@Test
public void lPushRAllCollection() {
Collection<Object> list = new ArrayList<>();
list.add(1);
list.add(1);
list.add(1);
long rPushAllCollection = redisUtil.lPushRAll("list", list);
System.out.println("rPushAllCollection:" + rPushAllCollection);
}
@Test
public void lPushLAllCollection() {
Collection<Object> list = new ArrayList<>();
list.add(1);
list.add(1);
list.add(1);
long lPushAll = redisUtil.lPushLAll("list", list);
System.out.println("lPushAllCollection:" + lPushAll);
}
@Test
public void lPushRAll() {
Collection<Object> list = new ArrayList<>();
list.add(1);
list.add(1);
list.add(1);
Object[] array = list.toArray();
long rPush = redisUtil.lPushRAll("list", array);
long rPush2 = redisUtil.lPushRAll("list", 'a', 2, "c", true, 1.2);
System.out.println("list:" + rPush);
System.out.println("list:" + rPush2);
}
@Test
public void lPushLAll() {
Collection<Object> list = new ArrayList<>();
list.add(1);
list.add(1);
list.add(1);
Object[] array = list.toArray();
long lPush = redisUtil.lPushLAll("list", array);
long lPush2 = redisUtil.lPushLAll("list", 'a', 2, "c", true, 1.2);
System.out.println("list:" + lPush);
System.out.println("list:" + lPush2);
}
@Test
public void lGetRange() {
List<Object> range = redisUtil.lGetRange("list", 0, 100);
System.out.println("list:" + range);
}
@Test
public void lGetByIndex() {
Object index = redisUtil.lGetByIndex("list", 0);
Object index1 = redisUtil.lGetByIndex("list", 1);
Object index2 = redisUtil.lGetByIndex("list", 2);
Object index3 = redisUtil.lGetByIndex("list", 300);
System.out.println("list:" + index);
System.out.println("list:" + index1);
System.out.println("list:" + index2 + " 长度: " + index2.toString().length());
System.out.println("list:" + index3);
}
}
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。



