springboot-cache + reds整合

版权声明:本文为博主原创文章,欢迎转载,转载请注明作者、原文超链接

maven:

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

启动类

@SpringBootApplication
@EnableCaching
public class CollectionApplication {

    public static void main(String[] args) {
        SpringApplication.run(CollectionApplication.class, args);
    }
}

service:

@Service
@CacheConfig(cacheNames = "myCache")
public class CacheServiceImpl implements CacheService {
    //操作字符串
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    //操作对象
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    @Cacheable(key = "targetClass + methodName +#p0")
    public User queryUser(String username) {
        User user = new User();
        user.setAddress("北京");
        user.setAge(26);
        user.setBirthday(new Date());
        user.setUserName(username);
        //stringRedisTemplate.opsForValue().set("username","ethan");
        return user;
    }
}

再次访问就会调用redis中的值。
redis-cli 里查看key

xxxx:8001> scan 0 match * count 10
1) "0"
2) 1) "myCache::class com.qjs.service.impl.CacheServiceImplqueryUserethan"
xxxx:8001>

详细学习参考 https://www.cnblogs.com/yueshutong/p/9381540.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值