springCache——jetcache缓存

文章目录

    • jetcache远程、本地缓存方案
    • jetcache方法注解使用方式

jetcache远程、本地缓存方案

springCache——jetcache缓存_第1张图片
springCache——jetcache缓存_第2张图片

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

springCache——jetcache缓存_第3张图片
springCache——jetcache缓存_第4张图片

jetcache:
  local:
    default:
      type: linkedhashmap
      keyConvertor: fastjson
  remote:
    default:
      type: redis
      host: localhost
      port: 6379
      password: 123456
      poolConfig:
        maxTotal: 50
    sms:
      type: redis
      host: localhost
      port: 6379
      poolConfig:
        maxTotal: 50

springCache——jetcache缓存_第5张图片
开启缓存
springCache——jetcache缓存_第6张图片
springCache——jetcache缓存_第7张图片

springCache——jetcache缓存_第8张图片

    //remote
//    @CreateCache(area="sms",name = "jetCache_",expire = 3600,timeUnit = TimeUnit.SECONDS)
    @CreateCache(name = "jetCache_",expire = 3600,timeUnit = TimeUnit.SECONDS,cacheType = CacheType.LOCAL)
    private Cache<String,String> jetCache;

    @Override
    public String sendCodeToSMS(String tele) {
        String code = codeUtils.generator(tele);
        jetCache.put(tele,code);
        return code;
    }

    @Override
    public Boolean checkCode(SMSCode smsCode) {
        String code = jetCache.get(smsCode.getTele());
        return smsCode.getCode().equals(code);
    }

jetcache方法注解使用方式

springCache——jetcache缓存_第9张图片

@SpringBootApplication
// Jetcache启用缓存的主开关
@EnableCreateCacheAnnotation
// 开启方法注解缓存
@EnableMethodCache(basePackages = "com.itheima")
public class Springboot20JetcacheApplication {

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

}

springCache——jetcache缓存_第10张图片
springCache——jetcache缓存_第11张图片

@Override
    @Cached(name = "book_",key = "#id",expire = 3600,cacheType = CacheType.REMOTE)
//    @CacheRefresh(refresh = 10)
    public Book getById(Integer id) {
        return bookDao.selectById(id);
    }

    @Override
    public Boolean save(Book book) {
        return bookDao.insert(book)>0;
    }

    @Override
    @CacheUpdate(name = "book_",key = "#book.id",value = "#book")
    public Boolean update(Book book) {
        return bookDao.updateById(book) > 0;
    }

    @Override
    @CacheInvalidate(name = "book_",key = "#id")
    public Boolean delete(Integer id) {
        return bookDao.deleteById(id) > 0;
    }

springCache——jetcache缓存_第12张图片

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Book implements Serializable {
    private Integer id;
    private String type;
    private String name;
    private String description;
}

springCache——jetcache缓存_第13张图片

jetcache:
  statIntervalMinutes: 1
  local:
    default:
      type: linkedhashmap
      keyConvertor: fastjson
  remote:
    default:
      type: redis
      host: localhost
      port: 6379
      password: 123456
      keyConvertor: fastjson
      valueEncode: java
      valueDecode: java
      poolConfig:
        maxTotal: 50
    sms:
      type: redis
      host: localhost
      port: 6379
      poolConfig:
        maxTotal: 50

你可能感兴趣的:(缓存,spring,java)