summaryrefslogtreecommitdiffstats
path: root/gerrit-server/src/main/java/com/google/gerrit/server/cache/CacheModule.java
blob: 23c094e6269a064967afe4b2f3e4a6f4e280c633 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// 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.TypeLiteral;
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 <K> type of key used to lookup entries.
   * @param <V> 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 <K, V> UnnamedCacheBinding core(final TypeLiteral<Cache<K, V>> type) {
    return core(Key.get(type));
  }

  /**
   * Declare a named in-memory cache.
   *
   * @param <K> type of key used to lookup entries.
   * @param <V> 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 <K, V> NamedCacheBinding core(final TypeLiteral<Cache<K, V>> type,
      final String name) {
    return core(Key.get(type, Names.named(name))).name(name);
  }

  private <K, V> UnnamedCacheBinding core(final Key<Cache<K, V>> key) {
    final boolean disk = false;
    final CacheProvider<K, V> b = new CacheProvider<K, V>(disk);
    bind(key).toProvider(b);
    return b;
  }

  /**
   * Declare an unnamed in-memory/on-disk cache.
   *
   * @param <K> type of key used to find entries, must be {@link Serializable}.
   * @param <V> 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 <K extends Serializable, V extends Serializable> UnnamedCacheBinding disk(
      final TypeLiteral<Cache<K, V>> type) {
    return disk(Key.get(type));
  }

  /**
   * Declare a named in-memory/on-disk cache.
   *
   * @param <K> type of key used to find entries, must be {@link Serializable}.
   * @param <V> 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 <K extends Serializable, V extends Serializable> NamedCacheBinding disk(
      final TypeLiteral<Cache<K, V>> type, final String name) {
    return disk(Key.get(type, Names.named(name))).name(name);
  }

  private <K, V> UnnamedCacheBinding disk(final Key<Cache<K, V>> key) {
    final boolean disk = true;
    final CacheProvider<K, V> b = new CacheProvider<K, V>(disk);
    bind(key).toProvider(b);
    return b;
  }
}