Java本地缓存工具之LoadingCache的使用详解

作者:剑客阿良_ALiang 时间:2023-06-24 11:28:11 

前言

在工作总常常需要用到缓存,而redis往往是首选,但是短期的数据缓存一般我们还是会用到本地缓存。本文提供一个我在工作中用到的缓存工具,该工具代码为了演示做了一些调整。如果拿去使用的话,可以考虑做成注入Bean对象,看具体需求了。

环境依赖

先添加maven依赖

<dependency>
           <groupId>com.google.guava</groupId>
           <artifactId>guava</artifactId>
           <version>30.1.1-jre</version>
       </dependency>
       <dependency>
           <groupId>cn.hutool</groupId>
           <artifactId>hutool-all</artifactId>
           <version>5.5.2</version>
       </dependency>
       <dependency>
           <groupId>org.projectlombok</groupId>
           <artifactId>lombok</artifactId>
           <optional>true</optional>
       </dependency>

代码

不废话,上代码了。

package ai.guiji.csdn.tools;

import cn.hutool.core.thread.ThreadUtil;
import com.google.common.cache.*;
import lombok.extern.slf4j.Slf4j;

import java.text.MessageFormat;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.LongStream;

/** @Author 剑客阿良_ALiang @Date 2021/12/30 17:57 @Description: 缓存工具 */
@Slf4j
public class CacheUtils {

private static LoadingCache<Long, String> cache;

/**
  * 初始化缓存方法
  *
  * @param totleCount 缓存池上限
  * @param overtime 超时时间
  * @param unit 时间单位
  * @param handleNotExist 处理不存在key方法
  * @param handleRemove 移除主键消费
  */
 private static void initCache(
     Integer totleCount,
     Integer overtime,
     TimeUnit unit,
     Function<Long, String> handleNotExist,
     Consumer<Long> handleRemove) {
   cache =
       CacheBuilder.newBuilder()
           // 缓存池大小
           .maximumSize(totleCount)
           // 设置时间对象没有被读/写访问则对象从内存中删除
           .expireAfterWrite(overtime, unit)
           // 移除 *
           .removalListener(
               new RemovalListener<Long, String>() {
                 @Override
                 public void onRemoval(RemovalNotification<Long, String> rn) {
                   handleRemove.accept(rn.getKey());
                 }
               })
           .recordStats()
           .build(
               new CacheLoader<Long, String>() {
                 @Override
                 public String load(Long aLong) throws Exception {
                   return handleNotExist.apply(aLong);
                 }
               });
   log.info("初始化缓存");
 }

/**
  * 存入缓存
  *
  * @param key 键
  * @param value 值
  */
 public static void put(Long key, String value) {
   try {
     log.info("缓存存入:[{}]-[{}]", key, value);
     cache.put(key, value);
   } catch (Exception exception) {
     log.error("存入缓存异常", exception);
   }
 }

/**
  * 批量存入缓存
  *
  * @param map 映射
  */
 public static void putMap(Map<Long, String> map) {
   try {
     log.info("批量缓存存入:[{}]", map);
     cache.putAll(map);
   } catch (Exception exception) {
     log.error("批量存入缓存异常", exception);
   }
 }

/**
  * 获取缓存
  *
  * @param key 键
  */
 public static String get(Long key) {
   try {
     return cache.get(key);
   } catch (Exception exception) {
     log.error("获取缓存异常", exception);
     return null;
   }
 }

/**
  * 删除缓存
  *
  * @param key 键
  */
 public static void removeKey(Long key) {
   try {
     cache.invalidate(key);
   } catch (Exception exception) {
     log.error("删除缓存异常", exception);
   }
 }

/**
  * 批量删除缓存
  *
  * @param keys 键
  */
 public static void removeAll(Iterable<Long> keys) {
   try {
     cache.invalidateAll(keys);
   } catch (Exception exception) {
     log.error("批量删除缓存异常", exception);
   }
 }

/** 清理缓存 */
 public static void clear() {
   try {
     cache.invalidateAll();
   } catch (Exception exception) {
     log.error("清理缓存异常", exception);
   }
 }

/**
  * 获取缓存大小
  *
  * @return 长度
  */
 public static long size() {
   return cache.size();
 }

public static void main(String[] args) {
   initCache(
       Integer.MAX_VALUE,
       10,
       TimeUnit.SECONDS,
       k -> {
         log.info("缓存:[{}],不存在", k);
         return "";
       },
       x -> log.info("缓存:[{}],已经移除", x));
   System.out.println(size());
   LongStream.range(0, 10).forEach(a -> put(a, MessageFormat.format("tt-{0}", a)));
   System.out.println(cache.asMap());
   ThreadUtil.sleep(5000);
   LongStream.range(0, 10)
       .forEach(
           a -> {
             System.out.println(get(a));
             ThreadUtil.sleep(1000);
           });
   System.out.println(cache.asMap());
   ThreadUtil.sleep(10000);
   System.out.println(cache.asMap());
 }
}

代码说明

1、在初始化loadingCache的时候,可以添加缓存的最大数量、消逝时间、消逝或者移除监听事件、不存在键处理等等。在上面的代码中,我初始化缓存大小为Integer的最大值,写入10秒后消逝,如不存在key返回空字符串等等。

2、该类也提供了put、putAll、get、remove、removeAll、clear、size方法,可以对缓存进行存、取、删、清理、大小等操作。

3、main演示方法中,先往缓存存入10个数据,然后过5秒后每秒取一个数据,并且打印一下缓存中的全部内容。

4、补充一句LoadingCache是线程安全的哦。

演示一下

15:31:53.495 [main] INFO ai.guiji.csdn.tools.CacheUtils - 初始化缓存
0
15:31:53.502 [main] INFO ai.guiji.csdn.tools.CacheUtils - 缓存存入:[0]-[tt-0]
15:31:53.508 [main] INFO ai.guiji.csdn.tools.CacheUtils - 缓存存入:[1]-[tt-1]
15:31:53.508 [main] INFO ai.guiji.csdn.tools.CacheUtils - 缓存存入:[2]-[tt-2]
15:31:53.508 [main] INFO ai.guiji.csdn.tools.CacheUtils - 缓存存入:[3]-[tt-3]
15:31:53.508 [main] INFO ai.guiji.csdn.tools.CacheUtils - 缓存存入:[4]-[tt-4]
15:31:53.508 [main] INFO ai.guiji.csdn.tools.CacheUtils - 缓存存入:[5]-[tt-5]
15:31:53.508 [main] INFO ai.guiji.csdn.tools.CacheUtils - 缓存存入:[6]-[tt-6]
15:31:53.508 [main] INFO ai.guiji.csdn.tools.CacheUtils - 缓存存入:[7]-[tt-7]
15:31:53.509 [main] INFO ai.guiji.csdn.tools.CacheUtils - 缓存存入:[8]-[tt-8]
15:31:53.509 [main] INFO ai.guiji.csdn.tools.CacheUtils - 缓存存入:[9]-[tt-9]
{6=tt-6, 5=tt-5, 0=tt-0, 8=tt-8, 7=tt-7, 2=tt-2, 1=tt-1, 9=tt-9, 3=tt-3, 4=tt-4}
tt-0
tt-1
tt-2
tt-3
tt-4
15:32:03.572 [main] INFO ai.guiji.csdn.tools.CacheUtils - 缓存:[5],已经移除
15:32:03.573 [main] INFO ai.guiji.csdn.tools.CacheUtils - 缓存:[6],已经移除
15:32:03.573 [main] INFO ai.guiji.csdn.tools.CacheUtils - 缓存:[5],不存在
15:32:04.581 [main] INFO ai.guiji.csdn.tools.CacheUtils - 缓存:[6],不存在
15:32:05.589 [main] INFO ai.guiji.csdn.tools.CacheUtils - 缓存:[0],已经移除
15:32:05.589 [main] INFO ai.guiji.csdn.tools.CacheUtils - 缓存:[7],已经移除
15:32:05.589 [main] INFO ai.guiji.csdn.tools.CacheUtils - 缓存:[8],已经移除
15:32:05.589 [main] INFO ai.guiji.csdn.tools.CacheUtils - 缓存:[7],不存在
15:32:06.589 [main] INFO ai.guiji.csdn.tools.CacheUtils - 缓存:[8],不存在
15:32:07.591 [main] INFO ai.guiji.csdn.tools.CacheUtils - 缓存:[1],已经移除
15:32:07.591 [main] INFO ai.guiji.csdn.tools.CacheUtils - 缓存:[2],已经移除
15:32:07.591 [main] INFO ai.guiji.csdn.tools.CacheUtils - 缓存:[9],已经移除
15:32:07.591 [main] INFO ai.guiji.csdn.tools.CacheUtils - 缓存:[9],不存在
{6=, 5=, 8=, 7=, 9=}
{}
Process finished with exit code 0

可以看到,后面的5-9在内存中已经不存在对应的值了。

来源:https://blog.csdn.net/zhiweihongyan1/article/details/122256607

标签:Java,LoadingCache,本地缓存
0
投稿

猜你喜欢

  • java9新特性Collection集合类的增强与优化方法示例

    2023-01-29 05:16:57
  • Spring JPA整合QueryDSL的示例代码

    2022-12-02 18:56:22
  • Mybatis图文并茂讲解分页插件

    2023-04-11 22:38:44
  • 浅谈Synchronized和Lock的区别

    2023-10-26 04:28:33
  • Android中GPS坐标转换为高德地图坐标详解

    2023-10-07 20:40:24
  • Java快速入门掌握类与对象及变量的使用

    2021-10-14 04:49:13
  • Android中TelephonyManager类的用法案例详解

    2022-04-30 20:18:32
  • java操作excel表格详解

    2021-08-20 14:35:46
  • C#利用时间和随即字符串创建唯一的订单编号

    2022-01-21 15:18:56
  • springboot前后台数据交互的示例代码

    2023-11-26 21:15:07
  • Android Studio实现仿微信APP门户界面详解及源码

    2022-06-29 20:08:52
  • 详解SpringBoot Start组件开发之记录接口日志信息

    2023-07-26 18:33:34
  • spring如何动态指定具体实现类

    2022-04-13 07:52:21
  • 关于Spring Boot内存泄露排查的记录

    2023-06-27 18:46:19
  • 详解android6.0版本下悬浮窗实现

    2023-09-04 18:43:34
  • Android编程设计模式之迭代器模式详解

    2023-06-07 01:25:42
  • Java并发之线程池Executor框架的深入理解

    2022-03-13 10:20:55
  • android绘制曲线和折线图的方法

    2021-11-19 02:26:02
  • FeignClient如何脱离eureka自定义URL

    2022-08-27 01:37:25
  • IDEA提高开发效率的7个插件(推荐)

    2021-10-16 15:28:35
  • asp之家 软件编程 m.aspxhome.com