最近在学习缓存技术,暂时了解到可做缓存的有Spring Cache,mybatis自带的缓存和JetCache等等。因为JetCache是阿里系的,而且教程看起来也相当容易上手和简便,因为就先学习了JetCache,来记录下学到的东西。

        JetCache源码地址:

JetCache: JetCache 是一个基于 Java 的缓存系统封装,提供统一的 API 和注解来简化缓存的使用

首先需要确保电脑安装了redis并开启了redis。安装教程请看其他网友的贴子啦。

pom引入:

<dependency>
    <groupId>com.alicp.jetcache</groupId>
    <artifactId>jetcache-starter-redis</artifactId>
    <version>2.6.0</version>
</dependency>

启动和配置:

@SpringBootApplication
@EnableMethodCache(basePackages = "com.hyz.provider")
@EnableCreateCacheAnnotation
public class ProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}

application.yml:

jetcache:
  statIntervalMinutes: 15
  areaInCacheName: false
  local:
    default:
      type: linkedhashmap
      keyConvertor: fastjson
      limit: 100
  remote:
    default:
      type: redis
      keyConvertor: fastjson
      valueEncoder: java
      valueDecoder: java
      poolConfig:
        minIdle: 5
        maxIdle: 20
        maxTotal: 50
      host: 127.0.0.1
      port: 6379

测试用例:

package com.hyz.provider.service;

import com.alicp.jetcache.anno.CacheInvalidate;
import com.alicp.jetcache.anno.CacheUpdate;
import com.alicp.jetcache.anno.Cached;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyz.provider.entity.IdCard;
import com.hyz.provider.mapper.IdCardMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;


@Service
public class IdCardService extends ServiceImpl<IdCardMapper, IdCard> {

    @Autowired
    IdCardMapper idCardMapper;

    @Cached(name="idCardCache-", key="#id", expire = 3600,postCondition = "result!=null")
    public IdCard getById(String id){
        System.out.println("--查询详情--");
        LambdaQueryWrapper<IdCard> query=new LambdaQueryWrapper<>();
        query.eq(IdCard::getId,id);
        query.isNull(IdCard::getDeleteAt);
        return this.getOne(query);
    }
    
    @CacheUpdate(name="idCardCache-", key="#idCard.id", value="#idCard" , condition = "result == true")
    public Boolean create(IdCard idCard){
        //有个小问题,createAt时间没有更新到缓存,因为idCard没有传createAt
        return this.saveOrUpdate(idCard);
    }

    @CacheInvalidate(name="idCardCache-", key="#id",condition = "result == true")
    public Boolean deleteIdCard(String id){
        LambdaUpdateWrapper<IdCard> updateWrapper=new UpdateWrapper<IdCard>().lambda();
        updateWrapper.set(IdCard::getDeleteAt, LocalDateTime.now())
                .eq(IdCard::getId,id);
        return this.update(new IdCard(),updateWrapper);
    }
}

由于我的代码是集成了mybatis-plus,所以实际测试还是以自己为准就行。

@Cached用于创建一个缓存,@CacheUpdate用于更新缓存,@CacheInvalidate用于使缓存失效

        这里说说我遇到的两个问题,第一个问题就是,condition是判断什么情况下才执行该缓存,但我当时不知道如何通过condition获取方法的返回值,再去判断,这里谢谢这位网友的分享,我才知道写法,能够用result来获取返回值。

         第二个问题就是,一开始,我是通过另外一个方法去调用这个带有缓存注解的方法,但是测试下都不生效,代码例子如下:

public void updateIdCard(IdCard idCard){
        this.create(idCard);
    }
    
    @CacheUpdate(name="idCardCache-", key="#idCard.id", value="#idCard" , condition = "result == true")
    public Boolean create(IdCard idCard){
        return this.saveOrUpdate(idCard);
    }

Controller调用了Service的updateIdCard方法的时候,缓存没有更新到,发现这种情况有两种解决办法,一种是在updateIdCard上也加上更新缓存的注解,一种是直接调用这个create方法。原因应该是和注解的特性有关,具体还要看看资料,这里就不多叙述了。

        以上就是一个比较基础的springboot集成JetCache方法,JetCache还有很多其他的用法,有兴趣的可以去其他网友的分享里看看了。