Package ninja.cache
Class NinjaCache
- java.lang.Object
-
- ninja.cache.NinjaCache
-
public class NinjaCache extends Object
A convenience class to access the underlying cache implementation. Makes getting and setting of objects a lot simpler. This class originates from Play 1.2.5's excellent cache implementation.- Author:
- ra
-
-
Constructor Summary
Constructors Constructor Description NinjaCache(Cache cache)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(String key, Object value)
Add an element only if it doesn't exist and store it indefinitely.void
add(String key, Object value, String expiration)
Add an element only if it doesn't exist.void
clear()
Clear all data from cache.long
decr(String key)
Decrement the element value (must be a Number) by 1.long
decr(String key, int by)
Decrement the element value (must be a Number).void
delete(String key)
Delete an element from the cache.Object
get(String key)
Retrieve an object.Map<String,Object>
get(String... key)
Bulk retrieve.<T> T
get(String key, Class<T> clazz)
Convenient clazz to get a value a class type;long
incr(String key)
Increment the element value (must be a Number) by 1.long
incr(String key, int by)
Increment the element value (must be a Number).void
replace(String key, Object value)
Replace an element only if it already exists and store it indefinitely.void
replace(String key, Object value, String expiration)
Replace an element only if it already exists.boolean
safeAdd(String key, Object value, String expiration)
Add an element only if it doesn't exist, and return only when the element is effectively cached.boolean
safeDelete(String key)
Delete an element from the cache and return only when the element is effectively removed.boolean
safeReplace(String key, Object value, String expiration)
Replace an element only if it already exists and return only when the element is effectively cached.boolean
safeSet(String key, Object value, String expiration)
Set an element and return only when the element is effectively cached.void
set(String key, Object value)
Set an element and store it indefinitely.void
set(String key, Object value, String expiration)
Set an element.
-
-
-
Constructor Detail
-
NinjaCache
@Inject public NinjaCache(Cache cache)
-
-
Method Detail
-
add
public void add(String key, Object value, String expiration)
Add an element only if it doesn't exist.- Parameters:
key
- Element keyvalue
- Element valueexpiration
- Ex: 10s, 3mn, 8h
-
safeAdd
public boolean safeAdd(String key, Object value, String expiration)
Add an element only if it doesn't exist, and return only when the element is effectively cached.- Parameters:
key
- Element keyvalue
- Element valueexpiration
- Ex: 10s, 3mn, 8h- Returns:
- If the element an eventually been cached
-
add
public void add(String key, Object value)
Add an element only if it doesn't exist and store it indefinitely.- Parameters:
key
- Element keyvalue
- Element value
-
set
public void set(String key, Object value, String expiration)
Set an element.- Parameters:
key
- Element keyvalue
- Element valueexpiration
- Ex: 10s, 3mn, 8h
-
safeSet
public boolean safeSet(String key, Object value, String expiration)
Set an element and return only when the element is effectively cached.- Parameters:
key
- Element keyvalue
- Element valueexpiration
- Ex: 10s, 3mn, 8h- Returns:
- If the element an eventually been cached
-
set
public void set(String key, Object value)
Set an element and store it indefinitely.- Parameters:
key
- Element keyvalue
- Element value
-
replace
public void replace(String key, Object value, String expiration)
Replace an element only if it already exists.- Parameters:
key
- Element keyvalue
- Element valueexpiration
- Ex: 10s, 3mn, 8h
-
safeReplace
public boolean safeReplace(String key, Object value, String expiration)
Replace an element only if it already exists and return only when the element is effectively cached.- Parameters:
key
- Element keyvalue
- Element valueexpiration
- Ex: 10s, 3mn, 8h- Returns:
- If the element an eventually been cached
-
replace
public void replace(String key, Object value)
Replace an element only if it already exists and store it indefinitely.- Parameters:
key
- Element keyvalue
- Element value
-
incr
public long incr(String key, int by)
Increment the element value (must be a Number).- Parameters:
key
- Element keyby
- The incr value- Returns:
- The new value
-
incr
public long incr(String key)
Increment the element value (must be a Number) by 1.- Parameters:
key
- Element key- Returns:
- The new value
-
decr
public long decr(String key, int by)
Decrement the element value (must be a Number).- Parameters:
key
- Element keyby
- The decr value- Returns:
- The new value
-
decr
public long decr(String key)
Decrement the element value (must be a Number) by 1.- Parameters:
key
- Element key- Returns:
- The new value
-
get
public Object get(String key)
Retrieve an object.- Parameters:
key
- The element key- Returns:
- The element value or null
-
get
public Map<String,Object> get(String... key)
Bulk retrieve.- Parameters:
key
- List of keys- Returns:
- Map of keys & values
-
delete
public void delete(String key)
Delete an element from the cache.- Parameters:
key
- The element key
-
safeDelete
public boolean safeDelete(String key)
Delete an element from the cache and return only when the element is effectively removed.- Parameters:
key
- The element key- Returns:
- If the element an eventually been deleted
-
clear
public void clear()
Clear all data from cache.
-
-