Maven依赖:
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
package com.utils;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* Redis封装工具类
* By CHENYB Date 2019-05-22
*/
@Component
public class RedisUtil
{
@Resource
private RedisTemplate redisTemplate;
/**
* 断言键值对是否存在
* @param key
* @return
*/
@SuppressWarnings("unchecked")
public boolean exists(final String key)
{
return redisTemplate.hasKey(key);
}
/**
* 读取缓存
* @param key
* @return
*/
@SuppressWarnings("unchecked")
public Object get(final String key)
{
Object result = null;
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
result = operations.get(key);
return result;
}
/**
* 写入缓存
* @param key
* @param value
* @return
*/
@SuppressWarnings("unchecked")
public boolean set(final String key, Object value)
{
boolean result = false;
try
{
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, value);
result = true;
}
catch (Exception e)
{
e.printStackTrace();
}
return result;
}
/**
* 写入缓存
* @param key
* @param value
* @return
*/
@SuppressWarnings("unchecked")
public boolean set(final String key, Object value, Long expireTime)
{
boolean result = false;
try
{
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, value);
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
result = true;
}
catch (Exception e)
{
e.printStackTrace();
}
return result;
}
/**
* 字符串类型: 将key进行递增。如果key不存在,操作之前,key就会被置为0.
* 如果key的value类型错误或者是个不能表示成数字的字符串,就返回错误。
* 这个操作最多支持64位有符号的整形数字。
* @param key
* @param offset
* @return
*/
public Long incrby(final String key, final Long offset)
{
@SuppressWarnings("unchecked")
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
Object result = operations.get(key);
Long count = Long.valueOf(result.toString());
operations.set(key, count + 1);
return count + 1;
}
/**
* 缺席填充
* @param redisKey
* @param value
* @param seconds
* @return
*/
public boolean setIfAbsent(String redisKey, Object value, long seconds)
{
boolean result = false;
BoundValueOperations boundValueOperations = redisTemplate
.boundValueOps(redisKey);
result = boundValueOperations.setIfAbsent(value);
//保存成功,更新时间,否则不更新过期时间
if ((result && seconds != -1)||seconds == 0)
{
boundValueOperations.expire(seconds, TimeUnit.SECONDS);
}
return result;
}
/**
* 删除缓存
* @param key
* @return
*/
public boolean clearKey (final String key){
return redisTemplate.delete( key );
}
/**
* 删除缓存中所有数据
* @return
*/
public Long clearKeys (){
return redisTemplate.delete( redisTemplate.keys( "*" ) );
}
/**
* 获取结果集
* @param key
* @return
*/
public BoundHashOperations hgetList(String key) {
return redisTemplate.boundHashOps(key);
}
}
测试代码:
package com.example.demo;
import com.utils.RedisUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTests{
@Test
public void test() {
System.out.println("ok!");
}
@Resource
private RedisUtil redisUtil;
@Test
public void existsTest() {
boolean exists = redisUtil.exists("111");
System.out.println(exists);
}
@Test
public void insertTest() {
boolean chenyb = redisUtil.set("111", "CHENYB");
System.out.println(chenyb);
}
@Test
public void getTest() {
Object o = redisUtil.get("111");
System.out.println(String.valueOf(o));
}
}