Class RedisAtomicLong

java.lang.Object
java.lang.Number
org.springframework.data.redis.support.atomic.RedisAtomicLong
All Implemented Interfaces:
Serializable, BoundKeyOperations<String>

public class RedisAtomicLong extends Number implements Serializable, BoundKeyOperations<String>
Atomic long backed by Redis. Uses Redis atomic increment/decrement and watch/multi/exec operations for CAS operations.
Author:
Costin Leau, Thomas Darimont, Christoph Strobl, Mark Paluch, Graham MacMaster, Ning Wei
See Also:
  • Constructor Details

    • RedisAtomicLong

      public RedisAtomicLong(String redisCounter, RedisConnectionFactory factory)
      Constructs a new RedisAtomicLong instance. Uses the value existing in Redis or 0 if none is found.
      Parameters:
      redisCounter - Redis key of this counter.
      factory - connection factory.
    • RedisAtomicLong

      public RedisAtomicLong(String redisCounter, RedisConnectionFactory factory, long initialValue)
      Constructs a new RedisAtomicLong instance with a initialValue that overwrites the existing value at redisCounter.
      Parameters:
      redisCounter - Redis key of this counter.
      factory - connection factory.
      initialValue - initial value to set.
    • RedisAtomicLong

      public RedisAtomicLong(String redisCounter, RedisOperations<String,Long> template)
      Constructs a new RedisAtomicLong instance. Uses the value existing in Redis or 0 if none is found.
      Parameters:
      redisCounter - Redis key of this counter.
      template - the template.
      See Also:
    • RedisAtomicLong

      public RedisAtomicLong(String redisCounter, RedisOperations<String,Long> template, long initialValue)
      Constructs a new RedisAtomicLong instance with a initialValue that overwrites the existing value.

      Note: You need to configure the given template with appropriate RedisSerializer for the key and value. The key serializer must be able to deserialize to a String and the value serializer must be able to deserialize to a Long.

      As an alternative one could use the RedisAtomicLong(String, RedisConnectionFactory, Long) constructor which uses appropriate default serializers, in this case StringRedisSerializer for the key and GenericToStringSerializer for the value.

      Parameters:
      redisCounter - Redis key of this counter.
      template - the template
      initialValue - initial value to set if the Redis key is absent.
  • Method Details

    • get

      public long get()
      Get the current value.
      Returns:
      the current value.
    • set

      public void set(long newValue)
      Set to the given value.
      Parameters:
      newValue - the new value.
    • getAndSet

      public long getAndSet(long newValue)
      Set to the given value and return the old value.
      Parameters:
      newValue - the new value.
      Returns:
      the previous value.
    • compareAndSet

      public boolean compareAndSet(long expect, long update)
      Atomically set the value to the given updated value if the current value == the expected value.
      Parameters:
      expect - the expected value.
      update - the new value.
      Returns:
      true if successful. false indicates that the actual value was not equal to the expected value.
    • getAndIncrement

      public long getAndIncrement()
      Atomically increment by one the current value.
      Returns:
      the previous value.
    • getAndDecrement

      public long getAndDecrement()
      Atomically decrement by one the current value.
      Returns:
      the previous value.
    • getAndAdd

      public long getAndAdd(long delta)
      Atomically add the given value to current value.
      Parameters:
      delta - the value to add.
      Returns:
      the previous value.
    • getAndUpdate

      public long getAndUpdate(LongUnaryOperator updateFunction)
      Atomically update the current value using the given update function.
      Parameters:
      updateFunction - the function which calculates the value to set. Should be a pure function (no side effects), because it will be applied several times if update attempts fail due to concurrent calls.
      Returns:
      the previous value.
      Since:
      2.2
    • getAndAccumulate

      public long getAndAccumulate(long updateValue, LongBinaryOperator accumulatorFunction)
      Atomically update the current value using the given accumulator function. The new value is calculated by applying the accumulator function to the current value and the given updateValue.
      Parameters:
      updateValue - the value which will be passed into the accumulator function.
      accumulatorFunction - the function which calculates the value to set. Should be a pure function (no side effects), because it will be applied several times if update attempts fail due to concurrent calls.
      Returns:
      the previous value.
      Since:
      2.2
    • incrementAndGet

      public long incrementAndGet()
      Atomically increment by one the current value.
      Returns:
      the updated value.
    • decrementAndGet

      public long decrementAndGet()
      Atomically decrement by one the current value.
      Returns:
      the updated value.
    • addAndGet

      public long addAndGet(long delta)
      Atomically add the given value to current value.
      Parameters:
      delta - the value to add.
      Returns:
      the updated value.
    • updateAndGet

      public long updateAndGet(LongUnaryOperator updateFunction)
      Atomically update the current value using the given update function.
      Parameters:
      updateFunction - the function which calculates the value to set. Should be a pure function (no side effects), because it will be applied several times if update attempts fail due to concurrent calls.
      Returns:
      the updated value.
      Since:
      2.2
    • accumulateAndGet

      public long accumulateAndGet(long updateValue, LongBinaryOperator accumulatorFunction)
      Atomically update the current value using the given accumulator function. The new value is calculated by applying the accumulator function to the current value and the given updateValue.
      Parameters:
      updateValue - the value which will be passed into the accumulator function.
      accumulatorFunction - the function which calculates the value to set. Should be a pure function (no side effects), because it will be applied several times if update attempts fail due to concurrent calls.
      Returns:
      the updated value.
      Since:
      2.2
    • toString

      public String toString()
      Overrides:
      toString in class Object
      Returns:
      the String representation of the current value.
    • getKey

      public String getKey()
      Description copied from interface: BoundKeyOperations
      Returns the key associated with this entity.
      Specified by:
      getKey in interface BoundKeyOperations<String>
      Returns:
      key associated with the implementing entity
    • getType

      public DataType getType()
      Description copied from interface: BoundKeyOperations
      Returns the associated Redis type.
      Specified by:
      getType in interface BoundKeyOperations<String>
      Returns:
      key type. null when used in pipeline / transaction.
    • getExpire

      public Long getExpire()
      Description copied from interface: BoundKeyOperations
      Returns the expiration of this key.
      Specified by:
      getExpire in interface BoundKeyOperations<String>
      Returns:
      expiration value (in seconds). null when used in pipeline / transaction.
    • expire

      public Boolean expire(long timeout, TimeUnit unit)
      Description copied from interface: BoundKeyOperations
      Sets the key time-to-live/expiration.
      Specified by:
      expire in interface BoundKeyOperations<String>
      Parameters:
      timeout - expiration value
      unit - expiration unit
      Returns:
      true if expiration was set, false otherwise. null when used in pipeline / transaction.
    • expireAt

      public Boolean expireAt(Date date)
      Description copied from interface: BoundKeyOperations
      Sets the key time-to-live/expiration.
      Specified by:
      expireAt in interface BoundKeyOperations<String>
      Parameters:
      date - expiration date
      Returns:
      true if expiration was set, false otherwise. null when used in pipeline / transaction.
    • persist

      public Boolean persist()
      Description copied from interface: BoundKeyOperations
      Removes the expiration (if any) of the key.
      Specified by:
      persist in interface BoundKeyOperations<String>
      Returns:
      true if expiration was removed, false otherwise. null when used in pipeline / transaction.
    • rename

      public void rename(String newKey)
      Description copied from interface: BoundKeyOperations
      Renames the key.
      Note: The new name for empty collections will be propagated on add of first element.
      Specified by:
      rename in interface BoundKeyOperations<String>
      Parameters:
      newKey - new key. Must not be null.
    • intValue

      public int intValue()
      Specified by:
      intValue in class Number
    • longValue

      public long longValue()
      Specified by:
      longValue in class Number
    • floatValue

      public float floatValue()
      Specified by:
      floatValue in class Number
    • doubleValue

      public double doubleValue()
      Specified by:
      doubleValue in class Number