示例#1
0
 /**
  * 如果该cache已经存在,则不再创建并返回已有cache
  *
  * @param groupId 如果cache和账号无关,置为null,创建全局cache
  * @param expireTime 小于0(比如-1)则忽略超时
  */
 public <K, V> Cache<K, V> createPersistedLruExpireCache(
     String groupId, CacheKey cacheID, int capacity, long expireTime) {
   if (groupId == null) {
     groupId = GLOBAL_CACHE_GROUP_ID;
   }
   Map<CacheKey, Cache> caches;
   synchronized (cacheMap) {
     caches = cacheMap.get(groupId);
     if (caches == null) {
       caches = new ConcurrentHashMap<>();
       cacheMap.put(groupId, caches);
       LogUtil.w(sTAG, groupId + " caches not exist, warning.");
     }
   }
   Cache<K, V> cache = caches.get(cacheID);
   if (cache == null) {
     try {
       cache = new PersistedLruExpireCache(cacheID.getKey(), groupId, capacity, 1, expireTime);
       caches.put(cacheID, cache);
     } catch (IOException e) {
       LogUtil.e(sTAG, e.getMessage(), e);
     }
   } else {
     cache.setCapacity(capacity);
   }
   return cache;
 }
示例#2
0
  /**
   * 给指定账号创建SimpleCache,注意如果该cache已经存在,则不再创建并返回已有cache
   *
   * @param groupId 输入null,创建全局性账号无关cache
   * @return cache
   */
  public <K, V> Cache<K, V> createSimpleCache(String groupId, CacheKey cacheID, int capacity) {
    if (groupId == null) {
      groupId = GLOBAL_CACHE_GROUP_ID;
    }
    Map<CacheKey, Cache> caches;
    synchronized (cacheMap) {
      caches = cacheMap.get(groupId);
      if (caches == null) {
        caches = new ConcurrentHashMap<>();
        cacheMap.put(groupId, caches);
        LogUtil.w(sTAG, groupId + " caches not exist, warning.");
      }
    }

    return addSimpleCache(groupId, caches, cacheID, capacity);
  }
示例#3
0
  /**
   * 给指定账号创建cache,注意如果该cache已经存在,则不再创建并返回已有cache cache支持超时功能,但容量capacity固定,超限后会丢失老数据
   *
   * @param groupId 输入null,创建全局性账号无关cache
   * @param expireTime 小于0(通常为-1)则不检查超时
   */
  public <K, V> Cache<K, V> createExpireCache(
      String groupId, CacheKey cacheID, int capacity, long expireTime) {
    if (groupId == null) {
      groupId = GLOBAL_CACHE_GROUP_ID;
    }

    Map<CacheKey, Cache> caches;
    synchronized (cacheMap) {
      caches = cacheMap.get(groupId);
      if (caches == null) {
        caches = new ConcurrentHashMap<>();
        cacheMap.put(groupId, caches);
        LogUtil.w(sTAG, groupId + " caches not exist, warning.");
      }
    }
    Cache<K, V> cache = caches.get(cacheID);
    if (cache == null) {
      cache = new ExpireCache<>(groupId, capacity, expireTime);
      caches.put(cacheID, cache);
    } else {
      cache.setCapacity(capacity);
    }
    return cache;
  }