Class RedisAtomicDouble
java.lang.Object
java.lang.Number
org.springframework.data.redis.support.atomic.RedisAtomicDouble
- All Implemented Interfaces:
Serializable
,BoundKeyOperations<String>
Atomic double backed by Redis. Uses Redis atomic increment/decrement and watch/multi/exec operations for CAS
operations.
- Author:
- Jennifer Hickey, Thomas Darimont, Christoph Strobl, Mark Paluch, Graham MacMaster, Ning Wei
- See Also:
-
Constructor Summary
ConstructorDescriptionRedisAtomicDouble
(String redisCounter, RedisConnectionFactory factory) Constructs a newRedisAtomicDouble
instance.RedisAtomicDouble
(String redisCounter, RedisConnectionFactory factory, double initialValue) Constructs a newRedisAtomicDouble
instance with ainitialValue
that overwrites the existing value.RedisAtomicDouble
(String redisCounter, RedisOperations<String, Double> template) Constructs a newRedisAtomicDouble
instance.RedisAtomicDouble
(String redisCounter, RedisOperations<String, Double> template, double initialValue) Constructs a newRedisAtomicDouble
instance with ainitialValue
that overwrites the existing value atredisCounter
. -
Method Summary
Modifier and TypeMethodDescriptiondouble
accumulateAndGet
(double updateValue, DoubleBinaryOperator accumulatorFunction) Atomically update the current value using the givenaccumulator function
.double
addAndGet
(double delta) Atomically add the given value to current value.boolean
compareAndSet
(double expect, double update) Atomically set the value to the given updated value if the current value==
the expected value.double
Atomically decrement by one the current value.double
Sets the key time-to-live/expiration.Sets the key time-to-live/expiration.float
double
get()
Get the current value.double
getAndAccumulate
(double updateValue, DoubleBinaryOperator accumulatorFunction) Atomically update the current value using the givenaccumulator function
.double
getAndAdd
(double delta) Atomically add the given value to current value.double
Atomically decrement by one the current value.double
Atomically increment by one the current value.double
getAndSet
(double newValue) Set to the given value and return the old value.double
getAndUpdate
(DoubleUnaryOperator updateFunction) Atomically update the current value using the givenupdate function
.Returns the expiration of this key.getKey()
Returns the key associated with this entity.getType()
Returns the associated Redis type.double
Atomically increment by one the current value.int
intValue()
long
persist()
Removes the expiration (if any) of the key.void
Renames the key.void
set
(double newValue) Set to the given value.toString()
double
updateAndGet
(DoubleUnaryOperator updateFunction) Atomically update the current value using the givenupdate function
.Methods inherited from class java.lang.Number
byteValue, shortValue
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.springframework.data.redis.core.BoundKeyOperations
expire, expireAt
-
Constructor Details
-
RedisAtomicDouble
Constructs a newRedisAtomicDouble
instance. Uses the value existing in Redis or0
if none is found.- Parameters:
redisCounter
- Redis key of this counter.factory
- connection factory.
-
RedisAtomicDouble
Constructs a newRedisAtomicDouble
instance with ainitialValue
that overwrites the existing value.- Parameters:
redisCounter
- Redis key of this counter.factory
- connection factory.initialValue
- initial value to set.
-
RedisAtomicDouble
Constructs a newRedisAtomicDouble
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:
-
RedisAtomicDouble
public RedisAtomicDouble(String redisCounter, RedisOperations<String, Double> template, double initialValue) Constructs a newRedisAtomicDouble
instance with ainitialValue
that overwrites the existing value atredisCounter
.Note: You need to configure the given
template
with appropriateRedisSerializer
for the key and value.As an alternative one could use the
RedisAtomicDouble(String, RedisConnectionFactory, Double)
constructor which uses appropriate default serializers.- Parameters:
redisCounter
- Redis key of this counter.template
- the templateinitialValue
- initial value to set if the Redis key is absent.
-
-
Method Details
-
get
public double get()Get the current value.- Returns:
- the current value.
-
set
public void set(double newValue) Set to the given value.- Parameters:
newValue
- the new value.
-
getAndSet
public double getAndSet(double newValue) Set to the given value and return the old value.- Parameters:
newValue
- the new value.- Returns:
- the previous value.
-
compareAndSet
public boolean compareAndSet(double expect, double 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 double getAndIncrement()Atomically increment by one the current value.- Returns:
- the previous value.
-
getAndDecrement
public double getAndDecrement()Atomically decrement by one the current value.- Returns:
- the previous value.
-
getAndAdd
public double getAndAdd(double delta) Atomically add the given value to current value.- Parameters:
delta
- the value to add.- Returns:
- the previous value.
-
getAndUpdate
Atomically update the current value using the givenupdate 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. Must not be null.- Returns:
- the previous value.
- Since:
- 2.2
-
getAndAccumulate
Atomically update the current value using the givenaccumulator function
. The new value is calculated by applying the accumulator function to the current value and the givenupdateValue
.- 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. Must not be null.- Returns:
- the previous value.
- Since:
- 2.2
-
incrementAndGet
public double incrementAndGet()Atomically increment by one the current value.- Returns:
- the updated value.
-
decrementAndGet
public double decrementAndGet()Atomically decrement by one the current value.- Returns:
- the updated value.
-
addAndGet
public double addAndGet(double delta) Atomically add the given value to current value.- Parameters:
delta
- the value to add.- Returns:
- the updated value.
-
updateAndGet
Atomically update the current value using the givenupdate 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. Must not be null.- Returns:
- the updated value.
- Since:
- 2.2
-
accumulateAndGet
Atomically update the current value using the givenaccumulator function
. The new value is calculated by applying the accumulator function to the current value and the givenupdateValue
.- 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. Must not be null.- Returns:
- the updated value.
- Since:
- 2.2
-
toString
-
getKey
Description copied from interface:BoundKeyOperations
Returns the key associated with this entity.- Specified by:
getKey
in interfaceBoundKeyOperations<String>
- Returns:
- key associated with the implementing entity
-
getType
Description copied from interface:BoundKeyOperations
Returns the associated Redis type.- Specified by:
getType
in interfaceBoundKeyOperations<String>
- Returns:
- key type. null when used in pipeline / transaction.
-
getExpire
Description copied from interface:BoundKeyOperations
Returns the expiration of this key.- Specified by:
getExpire
in interfaceBoundKeyOperations<String>
- Returns:
- expiration value (in seconds). null when used in pipeline / transaction.
-
expire
Description copied from interface:BoundKeyOperations
Sets the key time-to-live/expiration.- Specified by:
expire
in interfaceBoundKeyOperations<String>
- Parameters:
timeout
- expiration valueunit
- expiration unit- Returns:
- true if expiration was set, false otherwise. null when used in pipeline / transaction.
-
expireAt
Description copied from interface:BoundKeyOperations
Sets the key time-to-live/expiration.- Specified by:
expireAt
in interfaceBoundKeyOperations<String>
- Parameters:
date
- expiration date- Returns:
- true if expiration was set, false otherwise. null when used in pipeline / transaction.
-
persist
Description copied from interface:BoundKeyOperations
Removes the expiration (if any) of the key.- Specified by:
persist
in interfaceBoundKeyOperations<String>
- Returns:
- true if expiration was removed, false otherwise. null when used in pipeline / transaction.
-
rename
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 interfaceBoundKeyOperations<String>
- Parameters:
newKey
- new key. Must not be null.
-
intValue
public int intValue() -
longValue
public long longValue() -
floatValue
public float floatValue()- Specified by:
floatValue
in classNumber
-
doubleValue
public double doubleValue()- Specified by:
doubleValue
in classNumber
-