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

猜你喜欢

转载自blog.csdn.net/qq_29312259/article/details/82829039