springCache——jetcache缓存

文章目录

jetcache远程、本地缓存方案


java 复制代码
        <dependency>
            <groupId>com.alicp.jetcache</groupId>
            <artifactId>jetcache-starter-redis</artifactId>
            <version>2.6.4</version>
        </dependency>


java 复制代码
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

开启缓存

java 复制代码
    //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方法注解使用方式

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

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

}


java 复制代码
@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;
    }
java 复制代码
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Book implements Serializable {
    private Integer id;
    private String type;
    private String name;
    private String description;
}
java 复制代码
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
相关推荐
专注VB编程开发20年1 天前
C#全面超越JAVA,主要还是跨平台用的人少
java·c#·.net·跨平台
廋到被风吹走1 天前
【数据库】【Redis】定位、优势、场景与持久化机制解析
数据库·redis·缓存
南_山无梅落1 天前
9.Python3集合(set)增删改查和推导式
java·开发语言
爱笑的眼睛111 天前
超越MSE与交叉熵:深度解析损失函数的动态本质与高阶设计
java·人工智能·python·ai
全靠bug跑1 天前
Spring Cloud OpenFeign 实战三部曲:快速集成 · 连接池优化 · 客户端抽取
java·spring boot·openfeign
Evan芙1 天前
搭建nexus服务,实现本地仓库、代理仓库
java·nginx·tomcat
乂爻yiyao1 天前
Java LTS版本重要升级特性对照表
java·开发语言
原来是好奇心1 天前
深入Spring Boot源码(六):Actuator端点与监控机制深度解析
java·开发语言·源码·springboot
北城以北88881 天前
Spring定时任务与Spring MVC拦截器
spring boot·spring·mvc