com.google.gerrit.server.cache.CacheModule Maven / Gradle / Ivy
// 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.inject.AbstractModule;
import com.google.inject.Key;
import com.google.inject.Provider;
import com.google.inject.Scopes;
import com.google.inject.TypeLiteral;
import com.google.inject.internal.UniqueAnnotations;
import com.google.inject.name.Names;
import java.io.Serializable;
/**
* Miniature DSL to support binding {@link Cache} instances in Guice.
*/
public abstract class CacheModule extends AbstractModule {
/**
* Declare an unnamed in-memory cache.
*
* @param type of key used to lookup entries.
* @param type of value stored by the cache.
* @param type type literal for the cache, this literal will be used to match
* injection sites.
* @return binding to describe the cache. Caller must set at least the name on
* the returned binding.
*/
protected UnnamedCacheBinding core(
final TypeLiteral> type) {
return core(Key.get(type));
}
/**
* Declare a named in-memory cache.
*
* @param type of key used to lookup entries.
* @param type of value stored by the cache.
* @param type type literal for the cache, this literal will be used to match
* injection sites. Injection sites are matched by this type literal
* and with {@code @Named} annotations.
* @return binding to describe the cache.
*/
protected NamedCacheBinding core(
final TypeLiteral> type, final String name) {
return core(Key.get(type, Names.named(name))).name(name);
}
private UnnamedCacheBinding core(final Key> key) {
final boolean disk = false;
final CacheProvider b = new CacheProvider(disk, this);
bind(key).toProvider(b).in(Scopes.SINGLETON);
return b;
}
/**
* Declare an unnamed in-memory/on-disk cache.
*
* @param type of key used to find entries, must be {@link Serializable}.
* @param type of value stored by the cache, must be {@link Serializable}.
* @param type type literal for the cache, this literal will be used to match
* injection sites. Injection sites are matched by this type literal
* and with {@code @Named} annotations.
* @return binding to describe the cache. Caller must set at least the name on
* the returned binding.
*/
protected UnnamedCacheBinding disk(
final TypeLiteral> type) {
return disk(Key.get(type));
}
/**
* Declare a named in-memory/on-disk cache.
*
* @param type of key used to find entries, must be {@link Serializable}.
* @param type of value stored by the cache, must be {@link Serializable}.
* @param type type literal for the cache, this literal will be used to match
* injection sites. Injection sites are matched by this type literal
* and with {@code @Named} annotations.
* @return binding to describe the cache.
*/
protected NamedCacheBinding disk(
final TypeLiteral> type, final String name) {
return disk(Key.get(type, Names.named(name))).name(name);
}
private UnnamedCacheBinding disk(final Key> key) {
final boolean disk = true;
final CacheProvider b = new CacheProvider(disk, this);
bind(key).toProvider(b).in(Scopes.SINGLETON);
return b;
}
Provider> getEntryCreator(CacheProvider cp,
Class extends EntryCreator> type) {
Key> key = newKey();
bind(key).to(type).in(Scopes.SINGLETON);
return getProvider(key);
}
@SuppressWarnings("unchecked")
private static Key> newKey() {
return (Key>) newKeyImpl();
}
private static Key> newKeyImpl() {
return Key.get(EntryCreator.class, UniqueAnnotations.create());
}
}