美文网首页
redis缓存

redis缓存

作者: 云鲸鱼rain | 来源:发表于2019-04-09 09:41 被阅读0次

缓存我是写在service里了,
有两个方法,
一个是通过商品ID查询方法,一个是通过商品ID和其他商品信息的更新方法。
有三个注解,
@Cacheable:将返回对象加入缓存
@CachePur:更新redis中的缓存对象
@CacheEvict:删除redis中的缓存对象
代码如下

    @Override
    @Cacheable(cacheNames = "product", key = "#productId")
    public ProductInfo selectByProductId(Integer productId) {
        return productInfoMapper.selectByProductId(productId);
    }

    @Override
    @CachePut(cacheNames = "product", key = "#productId", condition = "#productId > 0")
    //@CacheEvict(cacheNames = "product", key = "123")
    public ProductInfo updateByProductId(Integer productId) {
         productInfoMapper.updateByProductId(productId, KeyUtil.genUniqueKey());
         return productInfoMapper.selectByProductId(productId);
    }

记得在启动类上加上@EnableCaching注解

@SpringBootApplication
@MapperScan(basePackages = "com.tianci.supereal.mapper")
@EnableCaching
public class SuperealApplication {

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

}

还有pom.xml文件

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

相关文章