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

com.litongjava.tio.utils.cache.guava.GuavaCache Maven / Gradle / Ivy

package com.litongjava.tio.utils.cache.guava;

import java.io.Serializable;
import java.util.Collection;
import java.util.concurrent.ConcurrentMap;

import com.google.common.cache.LoadingCache;
import com.litongjava.tio.utils.cache.AbsCache;
import com.litongjava.tio.utils.hutool.StrUtil;

/**
 *
 * @author tanyaowu
 * 2017年8月5日 上午10:16:26
 */
public class GuavaCache extends AbsCache {

  private LoadingCache loadingCache = null;

  private LoadingCache temporaryLoadingCache = null;

  public GuavaCache(String cacheName, LoadingCache loadingCache,
      LoadingCache temporaryLoadingCache) {
    super(cacheName);
    this.loadingCache = loadingCache;
    this.temporaryLoadingCache = temporaryLoadingCache;
  }

  @Override
  public void clear() {
    loadingCache.invalidateAll();
    temporaryLoadingCache.invalidateAll();
  }

  @Override
  public Serializable _get(String key) {
    if (StrUtil.isBlank(key)) {
      return null;
    }
    Serializable ret = loadingCache.getIfPresent(key);
    if (ret == null) {
      ret = temporaryLoadingCache.getIfPresent(key);
    }

    return ret;
  }

  @Override
  public Collection keys() {
    ConcurrentMap map = loadingCache.asMap();
    return map.keySet();
  }
  
  @Override
  public Collection keysCollection() {
    return  loadingCache.asMap().keySet();
  }

  @Override
  public void put(String key, Serializable value) {
    if (StrUtil.isBlank(key)) {
      return;
    }
    loadingCache.put(key, value);
  }

  @Override
  public void putTemporary(String key, Serializable value) {
    if (StrUtil.isBlank(key)) {
      return;
    }
    temporaryLoadingCache.put(key, value);
  }

  @Override
  public void remove(String key) {
    if (StrUtil.isBlank(key)) {
      return;
    }
    loadingCache.invalidate(key);
    temporaryLoadingCache.invalidate(key);
  }

  /**
   * 
   * @return
   * @author: tanyaowu
   */
  public ConcurrentMap asMap() {
    return loadingCache.asMap();
  }

  /**
   * 
   * @return
   * @author: tanyaowu
   */
  public long size() {
    return loadingCache.size();
  }

  @Override
  public long ttl(String key) {
    throw new RuntimeException("不支持ttl");
  }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy