SpringBoot整合Redis

 redis 的依赖引用:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

 

spring:
  redis:
    host: 192.168.8.108
    port: 6379
    timeout: 1000
    database: 0
    pool:
      max-active: 15
      max-idle: 8
      min-idle: 2
      max-wait: 20

 

1、作为缓存工具

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @RequestMapping("/test")
    public void test() {
        redisTemplate.opsForValue().set("test123", "test123");
        System.out.println(this.redisTemplate.opsForValue().get("test123"));
    }

 

2、作为查询缓存

启动类上加上注解:@EnableCaching

@SpringBootApplication
@EnableCaching
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}



具体调用类上加上注解:@Cacheable ,value:在redis中key


    @Cacheable(value="findUser")
	@Override
	public List<User> findUser() {
		System.out.println("========到数据库中查找======");
		return userMapper.findUser() ;
	}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值