(九)SpringBoot整合redis框架
2021-06-15 18:03
标签:exception pack art lib config date fas pop example 在 RedisService RedisServiceImpl 创建RedisController (九)SpringBoot整合redis框架 标签:exception pack art lib config date fas pop example 原文地址:https://www.cnblogs.com/yui66/p/9632769.html二:添加Redis依赖
三:添加Redis配置信息
application.properties
中添加spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.timeout=0
spring.redis.password=
四:创建RedisConfigurer
package com.example.demo.core.configurer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.JedisPoolConfig;
/**
* @author
* @Description: redis配置
* @date 2018/4/30 10:28
*/
@Configuration
@EnableCaching //开启缓存
public class RedisConfigurer extends CachingConfigurerSupport {
@Bean
@ConfigurationProperties(prefix="spring.redis")
public JedisPoolConfig getRedisConfig(){
JedisPoolConfig config = new JedisPoolConfig();
return config;
}
@Bean
@ConfigurationProperties(prefix="spring.redis")
public JedisConnectionFactory getConnectionFactory(){
JedisConnectionFactory factory = new JedisConnectionFactory();
JedisPoolConfig config = getRedisConfig();
factory.setPoolConfig(config);
return factory;
}
@Bean
public RedisTemplate, ?> getRedisTemplate(){
RedisTemplate,?> template = new StringRedisTemplate(getConnectionFactory());
return template;
}
}
五:创建Redis常用方法
package com.example.demo.service;
import java.util.List;
/**
* @author 张瑶
* @Description: redis常用方法
* @date 2018/4/30 10:35
*/
public interface RedisService {
/**
* 设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型。
* @param key
* @param value
* @return
*/
boolean set(String key, String value);
/**
* 获取指定 key 的值。如果 key 不存在,返回 nil 。如果key 储存的值不是字符串类型,返回一个错误。
* @param key
* @return
*/
String get(String key);
/**
* 设置 key 的过期时间。key 过期后将不再可用。
* @param key
* @param expire
* @return
*/
boolean expire(String key, long expire);
/**
* 存集合
* @param key
* @param list
* @param
package com.example.demo.service.impl;
import com.alibaba.fastjson.JSON;
import com.example.demo.service.RedisService;
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.serializer.RedisSerializer;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* @author 张瑶
* @Description: redis配置
* @date 2018/4/30 10:38
*/
@Service
public class RedisServiceImpl implements RedisService {
@Resource
private RedisTemplate
六:接口测试
package com.example.demo.controller;
import com.example.demo.core.ret.RetResponse;
import com.example.demo.core.ret.RetResult;
import com.example.demo.service.RedisService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author 张瑶
* @Description:
* @date 2018/4/30 11:28
*/
@RestController
@RequestMapping("redis")
public class RedisController {
@Resource
private RedisService redisService;
@PostMapping("/setRedis")
public RetResult