springboot整合redis

spring:
  redis:
    port: 6379
    host: 192.168.88.101
    password: 111111
    lettuce:
      pool:
        max-active: 8
        max-wait: -1ms
    database: 0

yml配置(单机版)

spring:
  redis:
    password: 111111
    lettuce:
      cluster:
        refresh:
          adaptive: true
          period: 2000
    cluster:
      max-redirects: 3
      nodes: ip:port,ip:port,ip:port,ip:port,ip:port,ip:port

yml配置(集群版),同时支持集群拓扑动态感应刷新

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
      <version>2.7.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
      <version>2.11.1</version>
    </dependency>

相关依赖

    @Autowired
    private StringRedisTemplate redisTemplate;
    //这里我使用的是stringredistemplate而不是redistemplate,主要是我懒得写那个配置类了。。。
    public void add(String key,String value){
        redisTemplate.opsForValue().set(key,value);
    }

    public String getById(Integer id){
        return redisTemplate.opsForValue().get(ORDER_KEY+id);
    }

service层实现

posted @ 2023-12-05 19:00  天启A  阅读(33)  评论(0)    收藏  举报