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

com.google.gerrit.server.cache.CacheModule Maven / Gradle / Ivy

There is a newer version: 3.10.0-rc4
Show newest version
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.cache;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.Weigher;
import com.google.gerrit.extensions.annotations.Exports;
import com.google.gerrit.extensions.config.FactoryModule;
import com.google.inject.Key;
import com.google.inject.Provider;
import com.google.inject.Scopes;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Names;
import com.google.inject.util.Types;
import java.io.Serializable;
import java.lang.reflect.Type;

/** Miniature DSL to support binding {@link Cache} instances in Guice. */
public abstract class CacheModule extends FactoryModule {
  public static final String MEMORY_MODULE = "cache-memory";
  public static final String PERSISTENT_MODULE = "cache-persistent";

  private static final TypeLiteral> ANY_CACHE = new TypeLiteral>() {};

  /**
   * Declare a named in-memory cache.
   *
   * @param  type of key used to lookup entries.
   * @param  type of value stored by the cache.
   * @return binding to describe the cache.
   */
  protected  CacheBinding cache(String name, Class keyType, Class valType) {
    return cache(name, TypeLiteral.get(keyType), TypeLiteral.get(valType));
  }

  /**
   * Declare a named in-memory cache.
   *
   * @param  type of key used to lookup entries.
   * @param  type of value stored by the cache.
   * @return binding to describe the cache.
   */
  protected  CacheBinding cache(String name, Class keyType, TypeLiteral valType) {
    return cache(name, TypeLiteral.get(keyType), valType);
  }

  /**
   * Declare a named in-memory cache.
   *
   * @param  type of key used to lookup entries.
   * @param  type of value stored by the cache.
   * @return binding to describe the cache.
   */
  protected  CacheBinding cache(
      String name, TypeLiteral keyType, TypeLiteral valType) {
    Type type = Types.newParameterizedType(Cache.class, keyType.getType(), valType.getType());

    @SuppressWarnings("unchecked")
    Key> key = (Key>) Key.get(type, Names.named(name));

    CacheProvider m = new CacheProvider<>(this, name, keyType, valType);
    bind(key).toProvider(m).asEagerSingleton();
    bind(ANY_CACHE).annotatedWith(Exports.named(name)).to(key);
    return m.maximumWeight(1024);
  }

   Provider> bindCacheLoader(
      CacheProvider m, Class> impl) {
    Type type =
        Types.newParameterizedType(Cache.class, m.keyType().getType(), m.valueType().getType());

    Type loadingType =
        Types.newParameterizedType(
            LoadingCache.class, m.keyType().getType(), m.valueType().getType());

    Type loaderType =
        Types.newParameterizedType(
            CacheLoader.class, m.keyType().getType(), m.valueType().getType());

    @SuppressWarnings("unchecked")
    Key> key = (Key>) Key.get(type, Names.named(m.name));

    @SuppressWarnings("unchecked")
    Key> loadingKey =
        (Key>) Key.get(loadingType, Names.named(m.name));

    @SuppressWarnings("unchecked")
    Key> loaderKey =
        (Key>) Key.get(loaderType, Names.named(m.name));

    bind(loaderKey).to(impl).in(Scopes.SINGLETON);
    bind(loadingKey).to(key);
    return getProvider(loaderKey);
  }

   Provider> bindWeigher(
      CacheProvider m, Class> impl) {
    Type weigherType =
        Types.newParameterizedType(Weigher.class, m.keyType().getType(), m.valueType().getType());

    @SuppressWarnings("unchecked")
    Key> key = (Key>) Key.get(weigherType, Names.named(m.name));

    bind(key).to(impl).in(Scopes.SINGLETON);
    return getProvider(key);
  }

  /**
   * Declare a named in-memory/on-disk cache.
   *
   * @param  type of key used to lookup entries.
   * @param  type of value stored by the cache.
   * @return binding to describe the cache.
   */
  protected  CacheBinding persist(
      String name, Class keyType, Class valType) {
    return persist(name, TypeLiteral.get(keyType), TypeLiteral.get(valType));
  }

  /**
   * Declare a named in-memory/on-disk cache.
   *
   * @param  type of key used to lookup entries.
   * @param  type of value stored by the cache.
   * @return binding to describe the cache.
   */
  protected  CacheBinding persist(
      String name, Class keyType, TypeLiteral valType) {
    return persist(name, TypeLiteral.get(keyType), valType);
  }

  /**
   * Declare a named in-memory/on-disk cache.
   *
   * @param  type of key used to lookup entries.
   * @param  type of value stored by the cache.
   * @return binding to describe the cache.
   */
  protected  CacheBinding persist(
      String name, TypeLiteral keyType, TypeLiteral valType) {
    return ((CacheProvider) cache(name, keyType, valType)).persist(true);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy