Line data Source code
1 : // Copyright (C) 2012 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.cache; 16 : 17 : import static com.google.common.base.Preconditions.checkState; 18 : import static java.util.Objects.requireNonNull; 19 : 20 : import com.google.common.base.Strings; 21 : import com.google.common.cache.Cache; 22 : import com.google.common.cache.CacheLoader; 23 : import com.google.common.cache.Weigher; 24 : import com.google.gerrit.common.Nullable; 25 : import com.google.gerrit.extensions.annotations.PluginName; 26 : import com.google.inject.Inject; 27 : import com.google.inject.Provider; 28 : import com.google.inject.TypeLiteral; 29 : import java.time.Duration; 30 : 31 : class CacheProvider<K, V> implements Provider<Cache<K, V>>, CacheBinding<K, V>, CacheDef<K, V> { 32 : private final CacheModule module; 33 : final String name; 34 : private final TypeLiteral<K> keyType; 35 : private final TypeLiteral<V> valType; 36 : private String configKey; 37 : private long maximumWeight; 38 : private Duration expireAfterWrite; 39 : private Duration expireFromMemoryAfterAccess; 40 : private Duration refreshAfterWrite; 41 : private Provider<CacheLoader<K, V>> loader; 42 : private Provider<Weigher<K, V>> weigher; 43 : 44 : private String plugin; 45 : private MemoryCacheFactory memoryCacheFactory; 46 : private boolean frozen; 47 : 48 152 : CacheProvider(CacheModule module, String name, TypeLiteral<K> keyType, TypeLiteral<V> valType) { 49 152 : this.module = module; 50 152 : this.name = name; 51 152 : this.keyType = keyType; 52 152 : this.valType = valType; 53 152 : } 54 : 55 : @Inject(optional = true) 56 : void setPluginName(@PluginName String pluginName) { 57 0 : this.plugin = pluginName; 58 0 : } 59 : 60 : @Inject 61 : void setMemoryCacheFactory(MemoryCacheFactory factory) { 62 152 : this.memoryCacheFactory = factory; 63 152 : } 64 : 65 : @Override 66 : public CacheBinding<K, V> maximumWeight(long weight) { 67 152 : checkNotFrozen(); 68 152 : maximumWeight = weight; 69 152 : return this; 70 : } 71 : 72 : @Override 73 : public CacheBinding<K, V> expireAfterWrite(Duration duration) { 74 152 : checkNotFrozen(); 75 152 : expireAfterWrite = duration; 76 152 : return this; 77 : } 78 : 79 : @Override 80 : public CacheBinding<K, V> expireFromMemoryAfterAccess(Duration duration) { 81 152 : checkNotFrozen(); 82 152 : expireFromMemoryAfterAccess = duration; 83 152 : return this; 84 : } 85 : 86 : @Override 87 : public CacheBinding<K, V> refreshAfterWrite(Duration duration) { 88 152 : checkNotFrozen(); 89 152 : refreshAfterWrite = duration; 90 152 : return this; 91 : } 92 : 93 : @Override 94 : public CacheBinding<K, V> loader(Class<? extends CacheLoader<K, V>> impl) { 95 152 : checkNotFrozen(); 96 152 : loader = module.bindCacheLoader(this, impl); 97 152 : return this; 98 : } 99 : 100 : @Override 101 : public CacheBinding<K, V> weigher(Class<? extends Weigher<K, V>> impl) { 102 152 : checkNotFrozen(); 103 152 : weigher = module.bindWeigher(this, impl); 104 152 : return this; 105 : } 106 : 107 : @Override 108 : public CacheBinding<K, V> configKey(String name) { 109 0 : checkNotFrozen(); 110 0 : configKey = requireNonNull(name); 111 0 : return this; 112 : } 113 : 114 : @Override 115 : public String name() { 116 152 : if (!Strings.isNullOrEmpty(plugin)) { 117 0 : return plugin + "." + name; 118 : } 119 152 : return name; 120 : } 121 : 122 : @Override 123 : public String configKey() { 124 152 : return configKey != null ? configKey : name(); 125 : } 126 : 127 : @Override 128 : public TypeLiteral<K> keyType() { 129 152 : return keyType; 130 : } 131 : 132 : @Override 133 : public TypeLiteral<V> valueType() { 134 152 : return valType; 135 : } 136 : 137 : @Override 138 : public long maximumWeight() { 139 152 : return maximumWeight; 140 : } 141 : 142 : @Override 143 : @Nullable 144 : public Duration expireAfterWrite() { 145 152 : return expireAfterWrite; 146 : } 147 : 148 : @Override 149 : @Nullable 150 : public Duration expireFromMemoryAfterAccess() { 151 152 : return expireFromMemoryAfterAccess; 152 : } 153 : 154 : @Override 155 : public Duration refreshAfterWrite() { 156 152 : return refreshAfterWrite; 157 : } 158 : 159 : @Override 160 : @Nullable 161 : public Weigher<K, V> weigher() { 162 152 : return weigher != null ? weigher.get() : null; 163 : } 164 : 165 : @Override 166 : @Nullable 167 : public CacheLoader<K, V> loader() { 168 152 : return loader != null ? loader.get() : null; 169 : } 170 : 171 : @Override 172 : public Cache<K, V> get() { 173 152 : freeze(); 174 152 : CacheLoader<K, V> ldr = loader(); 175 152 : return ldr != null ? memoryCacheFactory.build(this, ldr) : memoryCacheFactory.build(this); 176 : } 177 : 178 : protected void checkNotFrozen() { 179 152 : checkState(!frozen, "binding frozen, cannot be modified"); 180 152 : } 181 : 182 : protected void freeze() { 183 152 : frozen = true; 184 152 : } 185 : }