org.springframework.data.redis.support.atomic
Class RedisAtomicInteger

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

public class RedisAtomicInteger
extends Number
implements Serializable, BoundKeyOperations<String>

Atomic integer backed by Redis. Uses Redis atomic increment/decrement and watch/multi/exec operations for CAS operations.

See Also:
AtomicInteger, Serialized Form

Constructor Summary
RedisAtomicInteger(String redisCounter, RedisConnectionFactory factory)
          Constructs a new RedisAtomicInteger instance.
RedisAtomicInteger(String redisCounter, RedisConnectionFactory factory, int initialValue)
          Constructs a new RedisAtomicInteger instance.
 
Method Summary
 int addAndGet(int delta)
          Atomically add the given value to current value.
 boolean compareAndSet(int expect, int update)
          Atomically set the value to the given updated value if the current value == the expected value.
 int decrementAndGet()
          Atomically decrement by one the current value.
 double doubleValue()
           
 Boolean expire(long timeout, TimeUnit unit)
          Sets the key time-to-live/expiration.
 Boolean expireAt(Date date)
          Sets the key time-to-live/expiration.
 float floatValue()
           
 int get()
          Get the current value.
 int getAndAdd(int delta)
          Atomically add the given value to current value.
 int getAndDecrement()
          Atomically decrement by one the current value.
 int getAndIncrement()
          Atomically increment by one the current value.
 int getAndSet(int newValue)
          Set to the give value and return the old value.
 Long getExpire()
          Returns the expiration of this key.
 String getKey()
          Returns the key associated with this entity.
 DataType getType()
          Returns the associated Redis type.
 int incrementAndGet()
          Atomically increment by one the current value.
 int intValue()
           
 long longValue()
           
 Boolean persist()
          Removes the expiration (if any) of the key.
 void rename(String newKey)
          Renames the key.
 void set(int newValue)
          Set to the given value.
 String toString()
          Returns the String representation of the current value.
 
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
 

Constructor Detail

RedisAtomicInteger

public RedisAtomicInteger(String redisCounter,
                          RedisConnectionFactory factory)
Constructs a new RedisAtomicInteger instance.

Parameters:
redisCounter - redis counter
factory - connection factory

RedisAtomicInteger

public RedisAtomicInteger(String redisCounter,
                          RedisConnectionFactory factory,
                          int initialValue)
Constructs a new RedisAtomicInteger instance.

Parameters:
redisCounter -
factory -
initialValue -
Method Detail

get

public int get()
Get the current value.

Returns:
the current value

set

public void set(int newValue)
Set to the given value.

Parameters:
newValue - the new value

getAndSet

public int getAndSet(int newValue)
Set to the give value and return the old value.

Parameters:
newValue - the new value
Returns:
the previous value

compareAndSet

public boolean compareAndSet(int expect,
                             int 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 return indicates that the actual value was not equal to the expected value.

getAndIncrement

public int getAndIncrement()
Atomically increment by one the current value.

Returns:
the previous value

getAndDecrement

public int getAndDecrement()
Atomically decrement by one the current value.

Returns:
the previous value

getAndAdd

public int getAndAdd(int delta)
Atomically add the given value to current value.

Parameters:
delta - the value to add
Returns:
the previous value

incrementAndGet

public int incrementAndGet()
Atomically increment by one the current value.

Returns:
the updated value

decrementAndGet

public int decrementAndGet()
Atomically decrement by one the current value.

Returns:
the updated value

addAndGet

public int addAndGet(int delta)
Atomically add the given value to current value.

Parameters:
delta - the value to add
Returns:
the updated value

toString

public String toString()
Returns the String representation of the current value.

Overrides:
toString in class Object
Returns:
the String representation of the current value.

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

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

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

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

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)

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

rename

public void rename(String newKey)
Description copied from interface: BoundKeyOperations
Renames the key.

Specified by:
rename in interface BoundKeyOperations<String>
Parameters:
newKey - new key

getType

public DataType getType()
Description copied from interface: BoundKeyOperations
Returns the associated Redis type.

Specified by:
getType in interface BoundKeyOperations<String>
Returns:
key type