All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.litongjava.tio.utils.cache.caffeine.CaffeineCacheFactory Maven / Gradle / Ivy

There is a newer version: 3.7.3.v202400213-RELEASE
Show newest version
package com.litongjava.tio.utils.cache.caffeine;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.benmanes.caffeine.cache.LoadingCache;
import com.github.benmanes.caffeine.cache.RemovalListener;
import com.litongjava.tio.utils.cache.AbsCache;
import com.litongjava.tio.utils.cache.CacheFactory;
import com.litongjava.tio.utils.cache.CacheName;
import com.litongjava.tio.utils.cache.RemovalListenerWrapper;
import com.litongjava.tio.utils.caffeine.CaffeineUtils;

public enum CaffeineCacheFactory implements CacheFactory {
  INSTANCE;

  private Logger log = LoggerFactory.getLogger(CaffeineCacheFactory.class);
  private Map map = new HashMap<>();
  private Object lock = new Object();

  /**
   * timeToLiveSeconds和timeToIdleSeconds不允许同时为null
   * @param cacheName
   * @param timeToLiveSeconds
   * @param timeToIdleSeconds
   * @return
   * @author tanyaowu
   */
  public CaffeineCache register(String cacheName, Long timeToLiveSeconds, Long timeToIdleSeconds) {
    return register(cacheName, timeToLiveSeconds, timeToIdleSeconds, null);
  }

  @SuppressWarnings("unchecked")
  public  CaffeineCache register(String cacheName, Long timeToLiveSeconds, Long timeToIdleSeconds,
      RemovalListenerWrapper removalListenerWrapper) {

    RemovalListener removalListener = null;
    if (removalListenerWrapper != null) {
      // 检查 removalListenerWrapper 是否持有 RemovalListener 类型的监听器
      if (removalListenerWrapper.getListener() instanceof RemovalListener) {
        removalListener = (RemovalListener) removalListenerWrapper.getListener();
      }
    }

    CaffeineCache caffeineCache = map.get(cacheName);
    if (caffeineCache == null) {
      synchronized (lock) {
        caffeineCache = map.get(cacheName);
        if (caffeineCache == null) {
          Integer initialCapacity = 10;
          Integer maximumSize = 5000000;
          boolean recordStats = false;
          LoadingCache loadingCache = CaffeineUtils.createLoadingCache(cacheName,
              timeToLiveSeconds, timeToIdleSeconds, initialCapacity, maximumSize, recordStats, removalListener);

          Integer temporaryMaximumSize = 500000;
          LoadingCache temporaryLoadingCache = CaffeineUtils.createLoadingCache(cacheName, 10L,
              (Long) null, initialCapacity, temporaryMaximumSize, recordStats, removalListener);
          caffeineCache = new CaffeineCache(cacheName, loadingCache, temporaryLoadingCache);

          caffeineCache.setTimeToIdleSeconds(timeToIdleSeconds);
          caffeineCache.setTimeToLiveSeconds(timeToLiveSeconds);

          map.put(cacheName, caffeineCache);
        }
      }
    }
    return caffeineCache;
  }

  public CaffeineCache getCache(String cacheName, boolean skipNull) {
    CaffeineCache caffeineCache = map.get(cacheName);
    if (caffeineCache == null && !skipNull) {
      log.error("cacheName[{}] is not yet registered, please register first.", cacheName);
    }
    return caffeineCache;
  }

  public CaffeineCache getCache(String cacheName) {
    return getCache(cacheName, false);
  }

  @Override
  public Map getMap() {
    return map;
  }

  @Override
  public CaffeineCache register(CacheName cacheName) {
    return this.register(cacheName.getName(), cacheName.getTimeToLiveSeconds(), cacheName.getTimeToIdleSeconds(), null);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy