SpringBoot中整合Redis(缓存篇)
注意:文末有最新Java实战项目和面试题
作者丨艾神一不小心
juejin.im/post/5b1f35ac6fb9a01e631e24fc
一、安装
二、整合到springboot
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
##默认密码为空
redis:
host: 127.0.0.1
# Redis服务器连接端口
port: 6379
jedis:
pool:
#连接池最大连接数(使用负值表示没有限制)
max-active: 100
# 连接池中的最小空闲连接
max-idle: 10
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: 100000
# 连接超时时间(毫秒)
timeout: 5000
#默认是索引为0的数据库
database: 0
@Configuration
@EnableCaching
public class RedisConfiguration extends CachingConfigurerSupport {
/**
* 自定义生成key的规则
*/
@Override
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object o, Method method, Object... objects) {
//格式化缓存key字符串
StringBuilder sb = new StringBuilder();
//追加类名
sb.append(o.getClass().getName());
//追加方法名
sb.append(method.getName());
//遍历参数并且追加
for (Object obj : objects) {
sb.append(obj.toString());
}
System.out.println("调用Redis缓存Key : " + sb.toString());
return sb.toString();
}
};
}
/**
* 采用RedisCacheManager作为缓存管理器
* @param connectionFactory
*/
@Bean
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
RedisCacheManager redisCacheManager = RedisCacheManager.create(connectionFactory);
return redisCacheManager;
}
@Bean
public RedisTemplateredisTemplate(RedisConnectionFactory factory) {
////解决键、值序列化问题
StringRedisTemplate template = new StringRedisTemplate(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
/**
* K 指以hash结构操作时 键类型
* T 为数据实体 应实现序列化接口,并定义serialVersionUID * RedisTemplate 提供了五种数据结构操作类型 hash / list / set / zset / value
* 方法命名格式为 数据操作类型 + 操作 如 hashPut 指以hash结构(也就是map)想key添加键值对
*/
public interface RedisHelper<HK, T> {
/**
* Hash结构 添加元素 * @param key key * @param hashKey hashKey * @param domain 元素
*/
void hashPut(String key, HK hashKey, T domain);
/**
* Hash结构 获取指定key所有键值对 * @param key * @return
*/
MaphashFindAll(String key) ;
/**
* Hash结构 获取单个元素 * @param key * @param hashKey * @return
*/
T hashGet(String key, HK hashKey);
void hashRemove(String key, HK hashKey);
/**
* List结构 向尾部(Right)添加元素 * @param key * @param domain * @return
*/
Long listPush(String key, T domain);
/**
* List结构 向头部(Left)添加元素 * @param key * @param domain * @return
*/
Long listUnshift(String key, T domain);
/**
* List结构 获取所有元素 * @param key * @return
*/
ListlistFindAll(String key) ;
/**
* List结构 移除并获取数组第一个元素 * @param key * @return
*/
T listLPop(String key);
/**
* 对象的实体类
* @param key
* @param domain
* @return
*/
void valuePut(String key, T domain);
/**
* 获取对象实体类
* @param key
* @return
*/
T getValue(String key);
void remove(String key);
/**
* 设置过期时间 * @param key 键 * @param timeout 时间 * @param timeUnit 时间单位
*/
boolean expirse(String key, long timeout, TimeUnit timeUnit);
}
@Service("RedisHelper")
public class RedisHelperImpl<HK, T> implements RedisHelper<HK, T> {
// 在构造器中获取redisTemplate实例, key(not hashKey) 默认使用String类型
private RedisTemplateredisTemplate;
// 在构造器中通过redisTemplate的工厂方法实例化操作对象
private HashOperationshashOperations;
private ListOperationslistOperations;
private ZSetOperationszSetOperations;
private SetOperationssetOperations;
private ValueOperationsvalueOperations;
// IDEA虽然报错,但是依然可以注入成功, 实例化操作对象后就可以直接调用方法操作Redis数据库
@Autowired
public RedisHelperImpl(RedisTemplateredisTemplate) {
this.redisTemplate = redisTemplate;
this.hashOperations = redisTemplate.opsForHash();
this.listOperations = redisTemplate.opsForList();
this.zSetOperations = redisTemplate.opsForZSet();
this.setOperations = redisTemplate.opsForSet();
this.valueOperations = redisTemplate.opsForValue();
}
@Override
public void hashPut(String key, HK hashKey, T domain) {
hashOperations.put(key, hashKey, domain);
}
@Override
public MaphashFindAll(String key) {
return hashOperations.entries(key);
}
@Override
public T hashGet(String key, HK hashKey) {
return hashOperations.get(key, hashKey);
}
@Override
public void hashRemove(String key, HK hashKey) {
hashOperations.delete(key, hashKey);
}
@Override
public Long listPush(String key, T domain) {
return listOperations.rightPush(key, domain);
}
@Override
public Long listUnshift(String key, T domain) {
return listOperations.leftPush(key, domain);
}
@Override
public ListlistFindAll(String key) {
if (!redisTemplate.hasKey(key)) {
return null;
}
return listOperations.range(key, 0, listOperations.size(key));
}
@Override
public T listLPop(String key) {
return listOperations.leftPop(key);
}
@Override
public void valuePut(String key, T domain) {
valueOperations.set(key, domain);
}
@Override
public T getValue(String key) {
return valueOperations.get(key);
}
@Override
public void remove(String key) {
redisTemplate.delete(key);
}
@Override
public boolean expirse(String key, long timeout, TimeUnit timeUnit) {
return redisTemplate.expire(key, timeout, timeUnit);
}
}
三、测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestRedis {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private RedisHelperImpl redisHelper;
@Test
public void test() throws Exception{
// 基本写法
// stringRedisTemplate.opsForValue().set("aaa","111");
// Assert.assertEquals("111",stringRedisTemplate.opsForValue().get("aaa"));
// System.out.println(stringRedisTemplate.opsForValue().get("aaa"));
Author user=new Author();
user.setName("Alex");
user.setIntro_l("不会打篮球的程序不是好男人");
redisHelper.valuePut("aaa",user);
System.out.println(redisHelper.getValue("aaa"));
}
@Test
public void testObj() throws Exception {
Author user=new Author();
user.setName("Jerry");
user.setIntro_l("不会打篮球的程序不是好男人!");
ValueOperationsoperations=redisTemplate.opsForValue();
operations.set("502", user);
Thread.sleep(500);
boolean exists=redisTemplate.hasKey("502");
if(exists){
System.out.println(redisTemplate.opsForValue().get("502"));
}else{
System.out.println("exists is false");
}
// Assert.assertEquals("aa", operations.get("com.neo.f").getUserName());
}
}
四、项目实战
@EnableCaching //开启缓存
@SpringBootApplication
public class PoetryApplication {
public static void main(String[] args) {
SpringApplication.run(PoetryApplication.class, args);
}
}
@RestController
@RequestMapping(value = "/poem")
public class AuthorController {
private final static Logger logger = LoggerFactory.getLogger(AuthorController.class);
@Autowired
private AuthorRepository authorRepository;
@Cacheable(value="poemInfo") //自动根据方法生成缓存
@PostMapping(value = "/poemInfo")
public Resultauthor(@RequestParam("author_id") int author_id, @RequestParam("author_name")String author_name) {
if(StringUtils.isEmpty(author_id) || StringUtils.isEmpty(author_name)){
return ResultUtils.error(ResultCode.INVALID_PARAM_EMPTY);
}
Author author;
Optionaloptional = authorRepository.getAuthorByIdAndName(author_id, author_name);
if (optional.isPresent()) {
author = optional.get();
//通过\n或者多个空格 进行过滤去重
if (!StringUtils.isEmpty(author.getIntro_l())) {
String s = author.getIntro_l();
String intro = s.split("\\s +")[0];
author.setIntro_l(intro);
}
} else {
return ResultUtils.error(ResultCode.NO_FIND_THINGS);
}
return ResultUtils.ok(author);
}
}
评论