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

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

There is a newer version: 3.11.0-rc3
Show newest version
// Copyright (C) 2018 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 static com.google.common.base.Preconditions.checkState;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.Weigher;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.server.cache.serialize.CacheSerializer;
import com.google.gerrit.server.cache.serialize.JavaCacheSerializer;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.TypeLiteral;
import java.io.Serializable;
import java.time.Duration;

class PersistentCacheProvider extends CacheProvider
    implements Provider>, PersistentCacheBinding, PersistentCacheDef {
  private int version;
  private long diskLimit;
  private CacheSerializer keySerializer;
  private CacheSerializer valueSerializer;

  private PersistentCacheFactory persistentCacheFactory;

  PersistentCacheProvider(
      CacheModule module, String name, TypeLiteral keyType, TypeLiteral valType) {
    super(module, name, keyType, valType);
    version = -1;
    diskLimit = 128 << 20;
  }

  @Inject(optional = true)
  void setPersistentCacheFactory(@Nullable PersistentCacheFactory factory) {
    this.persistentCacheFactory = factory;
  }

  @Override
  public PersistentCacheBinding maximumWeight(long weight) {
    return (PersistentCacheBinding) super.maximumWeight(weight);
  }

  @Override
  public PersistentCacheBinding expireAfterWrite(Duration duration) {
    return (PersistentCacheBinding) super.expireAfterWrite(duration);
  }

  @Override
  public PersistentCacheBinding loader(Class> clazz) {
    return (PersistentCacheBinding) super.loader(clazz);
  }

  @Override
  public PersistentCacheBinding expireFromMemoryAfterAccess(Duration duration) {
    return (PersistentCacheBinding) super.expireFromMemoryAfterAccess(duration);
  }

  @Override
  public PersistentCacheBinding weigher(Class> clazz) {
    return (PersistentCacheBinding) super.weigher(clazz);
  }

  @Override
  public PersistentCacheBinding version(int version) {
    this.version = version;
    return this;
  }

  @Override
  public PersistentCacheBinding keySerializer(CacheSerializer keySerializer) {
    this.keySerializer = keySerializer;
    return this;
  }

  @Override
  public PersistentCacheBinding valueSerializer(CacheSerializer valueSerializer) {
    this.valueSerializer = valueSerializer;
    return this;
  }

  @Override
  public PersistentCacheBinding diskLimit(long limit) {
    checkNotFrozen();
    diskLimit = limit;
    return this;
  }

  @Override
  public long diskLimit() {
    return diskLimit;
  }

  @Override
  public int version() {
    return version;
  }

  @Override
  public CacheSerializer keySerializer() {
    return keySerializer;
  }

  @Override
  public CacheSerializer valueSerializer() {
    return valueSerializer;
  }

  @Override
  public Cache get() {
    if (persistentCacheFactory == null) {
      return super.get();
    }
    checkState(version >= 0, "version is required");
    checkSerializer(keyType(), keySerializer, "key");
    checkSerializer(valueType(), valueSerializer, "value");
    freeze();
    CacheLoader ldr = loader();
    return ldr != null
        ? persistentCacheFactory.build(this, ldr)
        : persistentCacheFactory.build(this);
  }

  private static  void checkSerializer(
      TypeLiteral type, CacheSerializer serializer, String name) {
    checkState(serializer != null, "%sSerializer is required", name);
    if (serializer instanceof JavaCacheSerializer) {
      checkState(
          Serializable.class.isAssignableFrom(type.getRawType()),
          "%s type %s must implement Serializable",
          name,
          type);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy