• Ehcache(二次封装,每个元素可自定义过期时间)


    Ehcache

    官网: https://www.ehcache.org/

    优势: 持续维护

    作用: Java写的缓存工具

    
    <dependency>
        <groupId>org.ehcachegroupId>
        <artifactId>ehcacheartifactId>
        <version>3.10.1version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    概念

    官网文档: https://www.ehcache.org/documentation/3.10/expiry.html

    内置过期策略==ExpiryPolicyBuilder
    no expiry:永不过期
    time-to-live:固定持有时间(不会由于你访问而重新计算过期时间)- 类似session过期时间策略
    time-to-idle:访问元素之后重新开始计算元素过期时间 - 类似redis的过期时间策略


    官网文档: https://www.ehcache.org/documentation/3.10/cache-event-listeners.html

    事件监听==EventType
    EVICTED:
    EXPIRED:keyValue键值对过期
    REMOVED:删除keyValue键值对
    CREATED:首次设置keyValue键值对
    UPDATED:keyValue键值对的value值变更

    代码

    简单使用
    public class OtherTest {
        @Test
        public void test10() {
    
    
            CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
                    .withCache("preConfigured",
                            CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
                                            ResourcePoolsBuilder.heap(100))
                                    .build())
                    .build(true);
            
            //必须key、value的类型一致,否则也找不到,非常的严格
            Cache<Long, String> preConfigured
                    = cacheManager.getCache("preConfigured", Long.class, String.class);
    
            Cache<Long, String> myCache = cacheManager.createCache("myCache",
                    CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
                            ResourcePoolsBuilder.heap(100)).build());
    
            myCache.put(1L, "da one!");
            String value = myCache.get(1L);
            Console.log("=================");
            Console.log(value);
            Console.log("=================");
    
            cacheManager.close();
    
        }
    }
    
    • 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

    在这里插入图片描述

    工具封装
    /**
     * Java缓存(封装ehcache)
     * 官网:https://www.ehcache.org/documentation/3.10/getting-started.html
     *
     * @author LinRuChang
     * @version 1.0
     * @date 2022/08/30
     * @since 1.8
     **/
    public class EhcacheUtil {
    
        protected static final CacheManager CACHE_MANAGER = CacheManagerBuilder.newCacheManagerBuilder().build(true);
    
        /**
         * 默认缓存容器名
         */
        protected static final String DEFAULT_CACHE_NAME = "DEFAULT_CACHE";
    
        static {
            //默认缓存容器创建
            createCacheIfNot(DEFAULT_CACHE_NAME);
        }
    
    
        public static Cache<? super Serializable, ? super Serializable> getCache(String cacheName) {
            return CACHE_MANAGER.getCache(cacheName,Serializable.class,Serializable.class);
        }
    
        /**
         * 线程安全的创建缓存容器
         * @param cacheName 缓存容器名字
         * @return
         */
        public static Cache<? super Serializable, ? super Serializable> createCache(String cacheName) {
            Assert.notBlank(cacheName, "缓存名缺失");
            Cache<? super Serializable, ? super Serializable> cache = getCache(cacheName);
            if(cache == null) {
                synchronized (EhcacheUtil.class) {
                    cache = getCache(cacheName);
                    if(cache == null) {
                        //缓存过期策略
                        CustomExpiryPolicy<Object, Object> customExpiryPolicy = new CustomExpiryPolicy<>();
                        //事件监听
                        CacheEventListenerConfigurationBuilder cacheEventListenerConfiguration = CacheEventListenerConfigurationBuilder
                                .newEventListenerConfiguration(new CustomCacheEventListener(cacheName), EventType.CREATED, EventType.values())
                                .ordered().asynchronous();
    
                        return CACHE_MANAGER.createCache(cacheName, CacheConfigurationBuilder.newCacheConfigurationBuilder(
                                        Serializable.class,
                                        Serializable.class,
                                        ResourcePoolsBuilder.heap(Long.MAX_VALUE))
                                .withExpiry(customExpiryPolicy)
                                .withService(cacheEventListenerConfiguration));
                    }else {
                        return cache;
                    }
                }
            }else {
                return cache;
            }
        }
    
        /**
         * 如果不存在则创建缓存对象
         *
         * @param cacheName 缓存名
         * @return 缓存对象
         */
        public static Cache<? super Serializable, ? super Serializable> createCacheIfNot(String cacheName) {
            Cache<? super Serializable, ? super Serializable> cache = getCache(cacheName);
            return cache != null ? cache : createCache(cacheName);
        }
    
        /**
         * 获取键值
         *
         * @param cacheName 缓存容器名 不设置默认使用{@link EhcacheUtil#DEFAULT_CACHE_NAME}
         * @param key       键名
         */
        public static <K extends Serializable> Serializable get(String cacheName, K key) {
            if (key == null) {
                return null;
            }
            cacheName = StrUtil.blankToDefault(cacheName, DEFAULT_CACHE_NAME);
            return (Serializable) (createCacheIfNot(cacheName).get(key));
        }
    
    
        /**
         * 获取键值 【从默认缓存容器中取{@link EhcacheUtil#DEFAULT_CACHE_NAME}】
         *
         * @param key 键名
         */
        public static <K extends Serializable> Serializable get(K key) {
            return get(DEFAULT_CACHE_NAME, key);
        }
    
    
        public static List<? extends Cache.Entry<? super Serializable, ? super Serializable>> getAllEntry(String cacheName) {
            cacheName = StrUtil.blankToDefault(cacheName, DEFAULT_CACHE_NAME);
            Cache<? super Serializable, ? super Serializable> cache = getCache(cacheName);
            return CollUtil.newArrayList(cache);
        }
        public static List<? extends Cache.Entry<? super Serializable, ? super Serializable>> getAllEntry() {
            return getAllEntry(DEFAULT_CACHE_NAME);
        }
    
    
        /**
         * 获取容器{cacheName}所有键值
         * @param cacheName
         * @return
         */
        public static List<? super Serializable> getAllValues(String cacheName) {
            cacheName = StrUtil.blankToDefault(cacheName, DEFAULT_CACHE_NAME);
            return getAllEntry(cacheName).stream()
                    .map(entry -> entry.getValue())
                    .collect(Collectors.toList());
        }
    
        /**
         * 获取默认容器所有键值
         * @return
         */
        public static List<? super Serializable> getAllValues() {
            return getAllValues(DEFAULT_CACHE_NAME);
        }
    
        /**
         * 获取容器{cacheName}所有的键名
         * @return
         */
        public static List<? super Serializable> getAllKeys(String cacheName) {
            cacheName = StrUtil.blankToDefault(cacheName, DEFAULT_CACHE_NAME);
            return getAllEntry(cacheName).stream()
                    .map(entry -> entry.getKey())
                    .collect(Collectors.toList());
        }
    
    
        /**
         * 获取默认容器所有的键名
         * @return
         */
        public static List<? super Serializable> getAllKeys() {
            return getAllKeys(DEFAULT_CACHE_NAME);
        }
    
    
        /**
         * 获取键值 【从默认缓存容器中取{@link EhcacheUtil#DEFAULT_CACHE_NAME}】
         *
         * @param key 键名
         */
        public static <K extends Serializable, V> V getByType(K key, Class<V> resultType) {
            Serializable result = get(DEFAULT_CACHE_NAME, key);
            return Optional.ofNullable(result)
                    .map(elem -> Convert.convert(resultType, elem))
                    .orElse(null);
    
        }
    
        /**
         * 获取键值 【从默认缓存容器中取{@link EhcacheUtil#DEFAULT_CACHE_NAME}】
         *
         * @param key 键名
         */
        public static <K extends Serializable> String getStr(K key) {
            return getByType(key, String.class);
        }
    
    
        /**
         * 设置缓存
         *
         * @param cacheName 缓存容器名字,不设置默认使用{@link EhcacheUtil#DEFAULT_CACHE_NAME}
         * @param key       键名
         * @param value     键值
         * @param expireMs  键值对过期时间(单位毫秒)- 不设置、或负数默认为永不过期
         */
        public static <K extends Serializable, V extends Serializable> void set(String cacheName, K key, V value, Long expireMs) {
            cacheName = StrUtil.blankToDefault(cacheName, DEFAULT_CACHE_NAME);
    
            if (expireMs != null && expireMs >= 0) {
                CacheRuntimeConfiguration runtimeConfiguration = createCacheIfNot(cacheName).getRuntimeConfiguration();
                CustomExpiryPolicy expiryPolicy = (CustomExpiryPolicy) runtimeConfiguration.getExpiryPolicy();
                expiryPolicy.setExpire(key, Duration.ofMillis(expireMs));
            }
    
            createCacheIfNot(cacheName).put(key, value);
        }
    
        /**
         * 设置缓存(使用默认的缓存容器{@link EhcacheUtil#DEFAULT_CACHE_NAME)
         *
         * @param key      键名
         * @param value    键值
         * @param expireMs 键值对过期时间(单位毫秒)- 不设置、或负数默认为永不过期
         */
        public static <K extends Serializable, V extends Serializable> void set(String key, V value, Long expireMs) {
            set(DEFAULT_CACHE_NAME, key, value, expireMs);
        }
    
        /**
         * 设置缓存(使用默认的缓存容器{@link EhcacheUtil#DEFAULT_CACHE_NAME),键值对永不过期
         *
         * @param key   键名
         * @param value 键值
         */
        public static <V extends Serializable> void set(String key, V value) {
            set(key, value, null);
        }
    
    
    
        /**
         * 删除某个容器的某个键值对
         * @param cacheName 容器名
         * @param key 键名
         * @return
         */
        public static Serializable del(String cacheName, Serializable key) {
            cacheName  = StrUtil.blankToDefault(cacheName,DEFAULT_CACHE_NAME);
            Cache<? super Serializable, ? super Serializable> cache = getCache(cacheName);
    
            return Optional.ofNullable((Serializable)cache.get(key))
                    .map(value -> {
                        cache.remove(key);
                        return value;
                    })
                    .orElse(null);
        }
    
        /**
         * 删除某人容器的某个键值对
         * @param key 键名
         * @return
         */
        public static Serializable del(Serializable key) {
            return del(DEFAULT_CACHE_NAME,key);
        }
    
        /**
         * 清空某个缓存容器的内容
         * @param cacheName 缓存容器名
         */
        public static void clear(String cacheName) {
            Cache<? super Serializable, ? super Serializable> cache = getCache(cacheName);
            if(cache != null) {
                cache.clear();
            }
        }
    
        /**
         * 销毁全部容器
         */
        public static void close() {
            CACHE_MANAGER.close();
        }
    
    
        /**
         * keyValue自定义过期时间
         * 

    * 参考的实现{@link ExpiryPolicyBuilder#noExpiration(),ExpiryPolicyBuilder#timeToLiveExpiration(Duration),ExpiryPolicyBuilder#timeToIdleExpiration(Duration)} * 文档 * * @param * @param */ private static class CustomExpiryPolicy<K, V> implements ExpiryPolicy<K, V> { private final ConcurrentHashMap<K, Duration> keyExpireMap = new ConcurrentHashMap(); public Duration setExpire(K key, Duration duration) { return keyExpireMap.put(key, duration); } public Duration getExpireByKey(K key) { return Optional.ofNullable(keyExpireMap.get(key)) .orElse(null); } public Duration removeExpire(K key) { return keyExpireMap.remove(key); } @Override public Duration getExpiryForCreation(K key, V value) { return Optional.ofNullable(getExpireByKey(key)) .orElse(Duration.ofNanos(Long.MAX_VALUE)); } @Override public Duration getExpiryForAccess(K key, Supplier<? extends V> value) { return getExpireByKey(key); } @Override public Duration getExpiryForUpdate(K key, Supplier<? extends V> oldValue, V newValue) { return getExpireByKey(key); } } /** * 自定义事件处理 == 当前主要是用于去除自定义键值对的Map过期时间东西,防止内存溢出 *

    * 文档 * * @param * @param */ private static class CustomCacheEventListener<K, V> implements CacheEventListener<K, V> { String cacheName; Cache cache; CustomExpiryPolicy customExpiryPolicy; public CustomCacheEventListener(String cacheName) { this.cacheName = cacheName; } @Override public void onEvent(CacheEvent event) { Console.log("事件触发:{}======{}========{}", cacheName, event.getType(), event.getKey()); this.cache = ObjectUtil.defaultIfNull(cache, CACHE_MANAGER.getCache(cacheName, Serializable.class, Serializable.class)); this.customExpiryPolicy = ObjectUtil.defaultIfNull(customExpiryPolicy, (CustomExpiryPolicy) this.cache.getRuntimeConfiguration().getExpiryPolicy()); if (StrUtil.equalsAnyIgnoreCase(event.getType().name(), EventType.EXPIRED.name(), EventType.EVICTED.name(), EventType.REMOVED.name())) { customExpiryPolicy.removeExpire(event.getKey()); } } } }

    • 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
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343


    Demo测试

    class EhcacheUtilTest {
    
        @Test
        void test1() {
            Cache<? super Serializable, ? super Serializable> test = EhcacheUtil.createCacheIfNot("test");
            test.put("name", "lrc");
            Object name = test.get("name");
            Console.log(name);
        }
    
        @Test
        @SneakyThrows
        void test2() {
    
    
            EhcacheUtil.set("name", "lrc");
            EhcacheUtil.set("year", 20, 1000L);
            EhcacheUtil.set("sport", "篮球",2000L);
    
            Console.log("\n========初始值========");
            Console.log("name:{}",EhcacheUtil.get("name"));
            Console.log("year:{}",EhcacheUtil.get("year"));
            Console.log("sport:{}",EhcacheUtil.get("sport"));
    
            Thread.sleep(1000);
            Console.log("\n========休眠1s后========");
            Console.log("name:{}",EhcacheUtil.get("name"));
            Console.log("year:{}",EhcacheUtil.get("year"));
            Console.log("sport:{}",EhcacheUtil.get("sport"));
    
            Thread.sleep(2000);
            Console.log("\n========休眠2s后========");
            Console.log("name:{}",EhcacheUtil.get("name"));
            Console.log("year:{}",EhcacheUtil.get("year"));
            Console.log("sport:{}",EhcacheUtil.get("sport"));
    
        }
    
    
        @Test
        @SneakyThrows
        void test3() {
            EhcacheUtil.set("testCacheContainer", "name", "lrc", 1000L);
            EhcacheUtil.set("testCacheContainer", "year", "20", null);
    
            Console.log("\n========初始值========");
            Console.log("name:{}",EhcacheUtil.get("testCacheContainer","name"));
            Console.log("year:{}",EhcacheUtil.get("testCacheContainer","year"));
    
            Thread.sleep(1000);
            Console.log("\n========休眠1s后========");
            Console.log("name:{}",EhcacheUtil.get("testCacheContainer","name"));
            Console.log("year:{}",EhcacheUtil.get("testCacheContainer","year"));
    
        }
        
        @Test
        @SneakyThrows
        void test4() {
            EhcacheUtil.set("year", 18);
            Console.log(EhcacheUtil.get("year"));
    
            EhcacheUtil.set("year", 25);
            Console.log(EhcacheUtil.get("year"));
        }    
    
    }
    
    • 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

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

  • 相关阅读:
    Win11怎么查MAC地址?Win11电脑如何查看mac地址?
    讲真的!身为一个合格的码农,谁还没碰过索引失效呢
    软件测试/测试开发丨Web自动化 测试用例流程设计
    Conda详细介绍
    不定积分第一类换元法(凑微分法)
    堆友:阿里巴巴文生图工具又出新功能(局部重绘)
    Sentinel
    windows安装MySQL详细步骤
    一篇案例读懂国央企如何实现数字化管控
    在Vue里面使用v-for出现警告
  • 原文地址:https://blog.csdn.net/weixin_39651356/article/details/126576733