redis做springboot2.x的缓存

1.首先是引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 缓存依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!--spring2.0集成redis所需common-pool2-->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.4.2</version>
</dependency>
//下面的可要可不要
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>

2. 在配置文件的东西(yaml or properties)

spring.redis.database=2 //第几个数据库,由于redis中数据库不止一个
spring.redis.host=localhost // 也可指定为127.0.0.1
spring.redis.port=6379 // 默认端口
spring.redis.password= // 默认为空

# springboot2.x以上如此配置,由于2.x的客户端是lettuce
# 单位要带上
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.min-idle=0
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.max-wait=10000ms
spring.redis.lettuce.shutdown-timeout=100ms

# springboot1.x如此配置,由于1.x的客户端是jedis
#spring.redis.jedis.pool.max-active=8
#spring.redis.jedis.pool.min-idle=0
#spring.redis.jedis.pool.max-idle=8
#spring.redis.jedis.pool.max-wait=-1
#spring.redis.timeout=500

3. redisConfig里面的配置

/**
 * Author:delay_boy
 * Date:2019-08-07 18:43
 * Description:(描述)
 */
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 这个方法是用来配置key的生成策略的
     * @return KeyGenerator
     */
    @Override
    @Bean
    public KeyGenerator keyGenerator() {

        return (o, method, params) -> {
            StringBuilder sb = new StringBuilder();
            sb.append(o.getClass().getName()); // 类名
            sb.append(method.getName()); // 方法名
            for (Object param: params) {
                sb.append(param.toString()); // 参数名
            }
            logger.info("这里面的内容是:{}",sb.toString());
            return sb.toString();
        };

    }

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
            .entryTtl(Duration.ofSeconds(3600)) // 60s缓存失效
            // 设置key的序列化方式
            .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer()))
            // 设置value的序列化方式
            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer()))
            // 不缓存null值
            .disableCachingNullValues()
            .prefixKeysWith("SysUser");

        RedisCacheManager redisCacheManager = RedisCacheManager.builder(redisConnectionFactory)
            .cacheDefaults(config)
            .transactionAware()
            .build();

        logger.info("自定义RedisCacheManager加载完成");
        return redisCacheManager;
    }

    // key键序列化方式
    private RedisSerializer<String> keySerializer() {
        return new StringRedisSerializer();
    }

    // value值序列化方式
    private GenericJackson2JsonRedisSerializer valueSerializer() {
        return new GenericJackson2JsonRedisSerializer();
    }
}

4. 在service类里里面的设置

/**
 * Author:delay_boy
 * Date:2019-08-07 19:53
 * Description:(描述)
 */
@Service
public class SysUserService {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private SysUserMapper sysUserMapper;

    @Cacheable(value = "user")
    public List<SysUser> findAllSysUser() {
        logger.info(sysUserMapper.selectAll().toString());
        return sysUserMapper.selectAll();
    }

    @Cacheable(value = "user")
    public SysUser findById(int id) {
        logger.info(sysUserMapper.selectByPrimaryKey(id).toString());
        return sysUserMapper.selectByPrimaryKey(id);
    }

    @CacheEvict("user")
    public void dropById(int id) {
        sysUserMapper.deleteByPrimaryKey(id);
    }

    @CachePut("user")
    public void updateUser(SysUser user) {
        sysUserMapper.updateByPrimaryKey(user);
    }

}

5.在controller里面的配置

@RestController
public class UserController {

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("/findall")
    public List<SysUser> findall() {
        return sysUserService.findAllSysUser();
    }
    @RequestMapping("/findbyid/{id}")
    public SysUser findbyid(@PathVariable("id") int id) {
        return sysUserService.findById(id);
    }
    @RequestMapping("/deletebyid/{id}")
    public String deletebyid(@PathVariable("id") int id) {
        sysUserService.dropById(id);
        return "ok";
    }

}

项目的demo已经上传到github中,可以自行下载测试

https://github.com/lkr-china/springboot-redis

最新文章

  1. React学习笔记-7-销毁阶段
  2. AndroidLinker与SO加壳技术之上篇
  3. Winjs – 微软开源技术发布的 JavaScript 组件集
  4. UE4 异步资源加载
  5. source insight 相对路径
  6. 注册页面的简单搭建(H5)
  7. SQL技巧
  8. curl伪造ip
  9. [转] 有趣的JavaScript原生数组函数
  10. iOS CoreBluetooth 教程
  11. 手机不支持onchange事件
  12. 不定高度实现垂直居中(兼容低版本ie)
  13. IO的五种模型
  14. windows下使用 Secure Shell Client工具操作linux常用命令
  15. andriod webview和h5
  16. 【ASP.NET Core快速入门】(十四)MVC开发:UI、 EF + Identity实现、注册实现、登陆实现
  17. Windows自定义运行命令
  18. Codeforces 1083C Max Mex
  19. jQuery代码优化的9种方法
  20. 转 Fira Code | 为写程序而生的字体

热门文章

  1. docker配置搭建gogs
  2. 使用shell程序备份crontab中的.sh脚本文件
  3. 剑指offer-面试题29-顺时针打印矩阵-矩阵
  4. 了解SIT和UAT的基本内涵
  5. “/Reports”应用程序中的服务器错误。
  6. ACM-ICPC 2018 徐州赛区网络预赛 Ryuji doesn't want to study
  7. jQuery---内容复习
  8. Go-结构体,结构体指针和方法
  9. CSRF漏洞原理
  10. window10安装nginx及请求转发到tomcat服务器访问项目及开机自启