Code example for Cache

Methods: put

0
  @Override 
  public void put(String table, Object hashKey, Object rangeKey, String value, int ttl) {
    if (!enabled)
      return; 
    final String key = key(hashKey, rangeKey);
    getTableCache(table, ttl).put(key, value);
  } 
 
  @Override 
  public String get(String table, Object hashKey, Object rangeKey) {
    if (!enabled)
      return null; 
    Cache<String, String> tableCache = cache.get(table);
    if (tableCache == null)
      return null; 
    final String key = key(hashKey, rangeKey);
    return tableCache.getIfPresent(key);
  } 
 
  @Override 
  public void remove(String table, Object hashKey, Object rangeKey) {
    if (!enabled)
      return; 
    Cache<String, String> tableCache = cache.get(table);
    if (tableCache != null) {
      final String key = key(hashKey, rangeKey);
      tableCache.invalidate(key);
    } 
  } 
 
  @Override 
  public void remove(String table) {
    Cache<String, String> tableCache = cache.get(table);
    if (tableCache != null) {
      tableCache.invalidateAll();
    } 
  } 
 
  @Override 
  public void removeAll() { 
    if (!enabled)
      return; 
    cache.clear();
  } 
 
  @Override 
  public int count(String table) {
    if (!enabled)
      return -1; 
    Cache<String, String> tableCache = cache.get(table);
    if (tableCache == null)
      return 0; 
    else { 
      long size = tableCache.size();
      if (size > Integer.MAX_VALUE)
        throw new RuntimeException("Count out of bounds");
      else 
        return (int) size;
    } 
  } 
 
  @Override 
  public boolean isEnabled() { 
    return enabled;
  } 
 
  @Override 
  public void setEnabled(boolean enableCache) {
    enabled = enableCache;
  } 
 
  private Cache<String, String> getTableCache(String table, int ttl) {
    Cache<String, String> tableCache = cache.get(table);
    if (tableCache == null) {
      if (ttl == 0)
        tableCache = CacheBuilder.newBuilder().build();
      else 
        tableCache = CacheBuilder.newBuilder().expireAfterWrite(ttl, TimeUnit.SECONDS).build();
      cache.put(table, tableCache);
    } 
    return tableCache;
  } 
 
  private String key(Object hashKey, Object rangeKey) {
Stop searching for code, let great code find you!  Add Codota to your java IDE