我是靠谱客的博主 光亮金鱼,这篇文章主要介绍LoadingCache 本地缓存 定时清除数据,现在分享给大家,希望可以做个参考。

最近工作中碰到要将一些数据缓存到本地一段时间,然后清除掉

  • 先上依赖

    复制代码
    1
    2
    3
    4
    5
    6
    <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>27.1-jre</version> </dependency>
  • 编写CacheManager缓存管理类(偷懒,直接写在一起的,忽略 =.= =.= =.=)

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    package com.xwfu.token.util; import com.google.common.cache.*; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Map; import java.util.Random; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @RestController public class CacheManager { private static Logger log = LoggerFactory.getLogger(CacheManager.class); /** 缓存项最大数量 */ private static final long GUAVA_CACHE_SIZE = 100000; /** 缓存时间:天 */ private static final long GUAVA_CACHE_DAY = 10; /** 缓存操作对象 */ private static LoadingCache<String, String> GLOBAL_CACHE = null; static { try { GLOBAL_CACHE = loadCache(new CacheLoader<String, String>() { @Override public String load(String key) throws Exception { // 处理缓存键不存在缓存值时的处理逻辑 return ""; } }); } catch (Exception e) { log.error("初始化Guava Cache出错", e); } } /** * 全局缓存设置 * * 缓存项最大数量:100000 * 缓存有效时间(天):1 * * * @param cacheLoader * @return * @throws Exception */ private static LoadingCache<String, String> loadCache(CacheLoader<String, String> cacheLoader) throws Exception { LoadingCache<String, String> cache = CacheBuilder.newBuilder() //缓存池大小,在缓存项接近该大小时, Guava开始回收旧的缓存项 .maximumSize(GUAVA_CACHE_SIZE) //设置时间对象没有被读/写访问则对象从内存中删除(在另外的线程里面不定期维护) .expireAfterAccess(GUAVA_CACHE_DAY, TimeUnit.SECONDS) //为了测试我这里设置的秒 // 设置缓存在写入之后 设定时间 后失效 .expireAfterWrite(GUAVA_CACHE_DAY, TimeUnit.SECONDS) //移除监听器,缓存项被移除时会触发 /*.removalListener(new RemovalListener<String, String>() { @Override public void onRemoval(RemovalNotification<String, String> rn) { //获取新的token try { if (StringUtils.isBlank(GLOBAL_CACHE.get("token"))) GLOBAL_CACHE.put("token", "sdfsd"); } catch (ExecutionException e) { e.printStackTrace(); } } })*/ //开启Guava Cache的统计功能 .recordStats() .build(cacheLoader); return cache; } /** * 设置缓存值 * 注: 若已有该key值,则会先移除(会触发removalListener移除监听器),再添加 * * @param key * @param value */ public static void put(String key, String value) { try { GLOBAL_CACHE.put(key, value); } catch (Exception e) { log.error("设置缓存值出错", e); } } /** * 批量设置缓存值 * * @param map */ public static void putAll(Map<? extends String, ? extends String> map) { try { GLOBAL_CACHE.putAll(map); } catch (Exception e) { log.error("批量设置缓存值出错", e); } } /** * 获取缓存值 * 注:如果键不存在值,将调用CacheLoader的load方法加载新值到该键中 * * @param key * @return */ public static String get(String key) { String token = ""; try { token = GLOBAL_CACHE.get(key); } catch (Exception e) { log.error("获取缓存值出错", e); } return token; } /** * 移除缓存 * * @param key */ public static void remove(String key) { try { GLOBAL_CACHE.invalidate(key); } catch (Exception e) { log.error("移除缓存出错", e); } } /** * 批量移除缓存 * * @param keys */ public static void removeAll(Iterable<String> keys) { try { GLOBAL_CACHE.invalidateAll(keys); } catch (Exception e) { log.error("批量移除缓存出错", e); } } /** * 清空所有缓存 */ public static void removeAll() { try { GLOBAL_CACHE.invalidateAll(); } catch (Exception e) { log.error("清空所有缓存出错", e); } } /** * 获取缓存项数量 * * @return */ public static long size() { long size = 0; try { size = GLOBAL_CACHE.size(); } catch (Exception e) { log.error("获取缓存项数量出错", e); } return size; } @GetMapping("/token") void getToken() throws ExecutionException, InterruptedException { if (StringUtils.isBlank(CacheManager.GLOBAL_CACHE.get("token"))){ Random rd = new Random(); int a = rd.nextInt(9); CacheManager.GLOBAL_CACHE.put("token", "sdfw12132" + a); System.out.println(CacheManager.GLOBAL_CACHE.get("token")); } else{ System.out.println(CacheManager.GLOBAL_CACHE.get("token")); } } }

最后

以上就是光亮金鱼最近收集整理的关于LoadingCache 本地缓存 定时清除数据的全部内容,更多相关LoadingCache内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部