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

io.ebean.cache.TenantAwareCache Maven / Gradle / Ivy

There is a newer version: 15.8.0
Show newest version
package io.ebean.cache;

import io.ebean.meta.MetricVisitor;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * A ServerCache proxy that is tenant aware.
 */
public final class TenantAwareCache implements ServerCache {

  private final ServerCache delegate;
  private final TenantAwareKey tenantAwareKey;

  /**
   * Create given the TenantAwareKey and delegate cache to proxy to.
   *
   * @param delegate       The cache to proxy to
   * @param tenantAwareKey Provides tenant aware keys to use in the cache
   */
  public TenantAwareCache(ServerCache delegate, TenantAwareKey tenantAwareKey) {
    this.delegate = delegate;
    this.tenantAwareKey = tenantAwareKey;
  }

  /**
   * Return the underlying ServerCache that is being delegated to.
   */
  @SuppressWarnings("unchecked")
  @Override
  public  T unwrap(Class cls) {
    return (T) delegate;
  }

  @Override
  public void visit(MetricVisitor visitor) {
    delegate.visit(visitor);
  }

  private Object key(Object key) {
    return tenantAwareKey.key(key);
  }

  @Override
  public Object get(Object id) {
    return delegate.get(key(id));
  }

  @Override
  public void put(Object id, Object value) {
    delegate.put(key(id), value);
  }

  @Override
  public void remove(Object id) {
    delegate.remove(key(id));
  }

  @Override
  public void clear() {
    delegate.clear();
  }

  @Override
  public int size() {
    return delegate.size();
  }

  @Override
  public int hitRatio() {
    return delegate.hitRatio();
  }

  @Override
  public ServerCacheStatistics statistics(boolean reset) {
    return delegate.statistics(reset);
  }

  @Override
  public Map getAll(Set keys) {
    Map keyMapping = new HashMap<>(keys.size());
    keys.forEach(k -> keyMapping.put(key(k), k));
    Map tmp = delegate.getAll(keyMapping.keySet());
    Map ret = new HashMap<>(keys.size());
    // unwrap tenant info here
    tmp.forEach((k,v)-> ret.put(((TenantAwareKey.CacheKey) k).key, v));
    return ret;
  }

  @Override
  public void putAll(Map keyValues) {
    Map tmp = new HashMap<>();
    keyValues.forEach((k, v) -> tmp.put(key(k), v));
    delegate.putAll(tmp);
  }

  @Override
  public void removeAll(Set keys) {
    delegate.removeAll(keys.stream().map(this::key).collect(Collectors.toSet()));
  }

}