Class DbValueUpdater<V>

java.lang.Object
tech.ydb.yoj.repository.db.cache.DbValueUpdater<V>

public abstract class DbValueUpdater<V> extends Object
Abstract base class for caching rarely updated values, e.g. feature flags.

Each active updater runs a periodic task which retrieves the value and caches it.

Clients should call readCached() to retrieve value cached by an active updater; implementors should override doReadValue() and choose which super constructor to call.

Lifecycle of ValueUpdater instances should be preferably managed by some external framework, e.g., Spring lifecycle. We recommend that you call start() at application start/component test start and shutdown() at graceful shutdown/component test shutdown.

See Also:
  • Field Details

    • log

      protected static final org.slf4j.Logger log
    • DEFAULT_THREAD_FACTORY_CREATOR

      protected static final DbValueUpdater.ThreadFactoryCreator DEFAULT_THREAD_FACTORY_CREATOR
    • DEFAULT_CACHE_TIMEOUT

      protected static final Duration DEFAULT_CACHE_TIMEOUT
    • DEFAULT_SHUTDOWN_TIMEOUT

      protected static final Duration DEFAULT_SHUTDOWN_TIMEOUT
    • DEFAULT_MAX_LAG

      protected static final Duration DEFAULT_MAX_LAG
    • DEFAULT_MAX_READ_DURATION

      protected static final Duration DEFAULT_MAX_READ_DURATION
    • pollInterval

      protected final Duration pollInterval
    • shutdownTimeout

      protected final Duration shutdownTimeout
    • maxAge

      protected final Duration maxAge
    • maxReadDuration

      protected final Duration maxReadDuration
    • threadFactory

      protected final ThreadFactory threadFactory
    • name

      protected final String name
  • Constructor Details

  • Method Details

    • doReadValue

      @NonNull protected abstract V doReadValue()
      Returns:
      value to cache; must not be null
    • start

      public void start()
    • shutdown

      public void shutdown()
    • isUpdaterActive

      public boolean isUpdaterActive()
      Checks if updater is active
      Returns:
      true if updater is active (cachedValue not null), else false
    • readCached

      @NonNull public V readCached()
      Returns:
      cached value
      Throws:
      IllegalStateException - if value updater is not active
    • forceUpdate

      public void forceUpdate()
      Forces an immediate value update. Will throw if the value cannot be updated.
      This method should ONLY be used in tests.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • tryReadValue

      @Nullable protected V tryReadValue()