Class CachingStateRepository

  • All Implemented Interfaces:
    StateRepository

    public class CachingStateRepository
    extends Object
    implements StateRepository
    Simple implementation of StateRepository which adds caching capabilities to an existing repository. You should consider using this class if lookups in your StateRepository are expensive (like database queries).
    Author:
    Christian Kaltepoth
    • Constructor Detail

      • CachingStateRepository

        public CachingStateRepository​(StateRepository delegate)
        Creates a caching facade for the supplied StateRepository. The cached state of a feature will only expire if setFeatureState(FeatureState) is invoked. You should therefore never use this constructor if the feature state is modified directly (for example by modifying the database table or the properties file).
        Parameters:
        delegate - The repository to delegate invocations to
      • CachingStateRepository

        public CachingStateRepository​(StateRepository delegate,
                                      long ttl)
        Creates a caching facade for the supplied StateRepository. The cached state of a feature will expire after the supplied TTL or if setFeatureState(FeatureState) is invoked.
        Parameters:
        delegate - The repository to delegate invocations to
        ttl - The time in milliseconds after which a cache entry will expire
        Throws:
        IllegalArgumentException - if the specified ttl is negative
      • CachingStateRepository

        public CachingStateRepository​(StateRepository delegate,
                                      long ttl,
                                      TimeUnit ttlTimeUnit)
        Creates a caching facade for the supplied StateRepository. The cached state of a feature will expire after the supplied TTL rounded down to milliseconds or if setFeatureState(FeatureState) is invoked.
        Parameters:
        delegate - The repository to delegate invocations to
        ttl - The time in a given ttlTimeUnit after which a cache entry will expire
        ttlTimeUnit - The unit that ttl is expressed in
    • Method Detail

      • getFeatureState

        public FeatureState getFeatureState​(Feature feature)
        Description copied from interface: StateRepository
        Get the persisted state of a feature from the repository. If the repository doesn't contain any information regarding this feature it must return null.
        Specified by:
        getFeatureState in interface StateRepository
        Parameters:
        feature - The feature to read the state for
        Returns:
        The persisted feature state or null
      • clear

        public void clear()
        Clears the contents of the cache