首页 > 其他分享 >springboot 使用caffeine 并监控本地缓存

springboot 使用caffeine 并监控本地缓存

时间:2023-07-14 18:15:11浏览次数:33  
标签:缓存 springboot cache caffeine key import annotation

1、添加依赖

   <dependency>
            <groupId>com.github.ben-manes.caffeine</groupId>
            <artifactId>caffeine</artifactId>
        </dependency>

 

2、添加配置

package com.example.demo.config;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.stats.CacheStats;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

@Configuration
@Slf4j
@EnableScheduling
public class PageCacheConfig {

    public Cache<String, String> cache;


    @PostConstruct
    private Cache<String, String> caffeineCacheBuilder() {
        cache = Caffeine.newBuilder()
                .expireAfterWrite(10, TimeUnit.SECONDS)
                .maximumSize(100).recordStats().build();
        return cache;
    }

    @Scheduled(fixedRate = 1000) // 每秒执行一次
    public void printCacheHitRate() {
        /**
         * hitCount–缓存命中数
         *
         * missCount–缓存未命中数
         *
         * loadSuccessCount–成功加载缓存的次数
         *
         * loadFailureCount–缓存加载失败的次数
         *
         * totalLoadTime–总加载时间(成功和失败)
         *
         * evictionCount–从缓存中逐出的条目数
         *
         * ejectionWeight–从缓存中逐出的条目的权重之和
         */
        CacheStats stats = cache.stats();
        System.out.println("Cache hit rate: " + stats);
    }

}

 

 

 

 

3、测试controller

package com.example.demo.controller;

import com.example.demo.config.PageCacheConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LocalCacheTestController {
    @Autowired
    PageCacheConfig pageCacheConfig;

    @GetMapping("/saveCache")
    public String addCache(@RequestParam("key") String key, @RequestParam("value") String value) {
        pageCacheConfig.cache.put(key, value);
        return "ok";
    }

    @GetMapping("/getCache")
    public String addCache(@RequestParam("key") String key) {
        String value = pageCacheConfig.cache.getIfPresent(key);
        return value;
    }
}

 

4、测试

1、保存缓存
http://127.0.0.1:8080/saveCache?key=name&value=zhangshan
2、获取缓存
http://127.0.0.1:8080/getCache?key=name

5、监控

CacheStats{hitCount=1, missCount=0, loadSuccessCount=0, loadFailureCount=0, totalLoadTime=0, evictionCount=0, evictionWeight=0}

 

标签:缓存,springboot,cache,caffeine,key,import,annotation
From: https://www.cnblogs.com/niun/p/17554672.html

相关文章

  • SpringBoot 服务接口限流,搞定!
    来源:blog.csdn.net/qq_34217386/article/details/122100904  在开发高并发系统时有三把利器用来保护系统:缓存、降级和限流。限流可以认为服务降级的一种,限流通过限制请求的流量以达到保护系统的目的。  一般来说,系统的吞吐量是可以计算出一个阈值的,为了保证系统的稳定运......
  • 从零玩转系列之SpringBoot3-基础特性
    一、简介1.前置知识​ ●Java17​ ●Spring、SpringMVC、MyBatis​ ●Maven、IDEA2.环境要求环境&工具版本(orlater)SpringBoot3.1.xIDEA2023.xJava17+Maven3.5+Tomcat10.0+Servlet5.0+GraalVMCommunity22.3+NativeBuildTools0......
  • 从零玩转SpringBoot3-快速入门
    一、简介1.前置知识​ ●Java17​ ●Spring、SpringMVC、MyBatis​ ●Maven、IDEA2.环境要求环境&工具版本(orlater)SpringBoot3.1.xIDEA2023.xJava17+Maven3.5+Tomcat10.0+Servlet5.0+GraalVMCommunity22.3+NativeBuildTools0......
  • Springboot 实现QQ登录(界面跳转)
    Springboot实现QQ登录(界面跳转)现在第三方登录已经变成主流app的登录方式了今天记录一下如何给自己的网站实现第三方登录(这里以QQ登录为例)准备工作首先确保你准备好你自己网站的域名:如https://xxx.com以及有正常账号密码登录的方式有很多实现的方式,比如去微信开放平台和Q......
  • SpringBoot是接到一个http请求就开启一个线程处理吗?
    1、跳出一个误区:SpringBoot不处理任何请求Spring本身并不进行Web的处理,无论是TCP连接也好还是请求和响应也好,这些都是在Spring以外的部分完成的,例如Tomcat,所以默认的SpringBoot将会集成Tomcat内嵌容器。Controller中收到的请求,都是经过Tomcat容器解析后交给Ser......
  • springboot redis工具类之StringRedisTemplate 使用
    1、StringRedisTemplate是什么?StringRedisTemplate继承自RedisTemplate类,实现了BeanClassLoaderAware,Aware,InitializingBean,RedisOperations<K,V>接口。StringRedisTemplate是RedisTemplate以字符串为中心的扩展,由于针对Redis的大多数操作都是基于字符串的,因此此类提供了一个......
  • springboot中使用redis
    1、引入依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>2、配置spring:redis:host:localhost#Redis服务器地址port:6379......
  • javaSpringboot
    目前最流行的框架,JavaSpring的子项目,由它开发 Springboot依赖管理      配置注解  什么是Javabean 有get和set才叫Javabean springboot自定义配置   由MYconfig这个类进行组件myservice配置将其纳入到Spring中  JavaSpr......
  • Springboot实现注解判断权限
    Springboot实现注解判断权限今天记录一下使用springboot的注解来给方法加权限避免了每个方法都需要大量的权限判断超级好用√@目录Springboot实现注解判断权限1.创建权限注解2.定义一个权限的枚举类3.创建拦截器AOP校验权限poincut表达式介绍4.使用注解1.创建权限注解首先......
  • springboot整合kafka
    一、引入依赖(kafka的版本和springboot的版本对不上的话,启动会报错,包类不存在)<dependency><groupId>org.springframework.kafka</groupId><artifactId>spring-kafka</artifactId><version>2.5.1.RELEASE</version></de......