spring-boot using the tools-redis implement a distributed cache

1 disposed in the pom

  <dependency>
            <groupId>cn.gjing</groupId>
            <artifactId>tools-redis</artifactId>
            <version>1.0.0</version>
        </dependency>

2. In the configuration application.properties

#redis数据库链接配置
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.database=0

3. Configure DemoApplication.java

@EnableSecondCache

4. Write DemoController

@RestController
public class DemoController {
    @Autowired
    private DemoService demoService;

    @GetMapping("demo")
    public boolean demo(@RequestParam("id")Integer id){
        return demoService.demo(id);
    }
}

5. Write DemoService

@Service
public class DemoService {
    /**
     * 当id大于10时使用缓存
     * 当id小于或等于10时不使用缓存
     * @param id
     * @return
     */

    @Cacheable(value = "id",key = "#id",condition = "#id>10")
    public boolean demo(Integer id){
        System.out.println("使用了数据库");
        if(id==20){
           return true;
        }
        return false;
    }

}

6. Test

http://127.0.0.1:1111/demo?id=20

Guess you like

Origin yq.aliyun.com/articles/706702