Using Aerospike as a cache / Spring integration of Aerospike with cache manager


I want to know how can we use Aerospike for caching in our java based application written with spring framework. I am looking for cache manager in Spring similar to RedisCacheManager. Do we have something so we can use @Cacheable etc.



To date, there is no Spring Data for Aerospike and no cache integration. But a cache is dead simple to implement. Try this code and let me know if it works:


    import org.springframework.cache.Cache;

    import com.aerospike.client.AerospikeClient;
    import com.aerospike.client.Bin;
    import com.aerospike.client.Key;
    import com.aerospike.client.Operation;
    import com.aerospike.client.Record;
    import com.aerospike.client.policy.RecordExistsAction;
    import com.aerospike.client.policy.WritePolicy;

public class AerospikeCache implements Cache{
	private static final String VALUE = "value";
	private final AerospikeClient client;
	private String namespace;
	private String set;
	private WritePolicy createOnly;
	public AerospikeCache(String namespace, String set, AerospikeClient client,
			long expiration){
		this.client = client;
		this.namespace = namespace;
		this.set = set;
		this.createOnly = new WritePolicy(client.writePolicyDefault);
		this.createOnly.recordExistsAction = RecordExistsAction.CREATE_ONLY;
	private Key getKey(Object key){
		return new Key(namespace, set, key.toString());
	private ValueWrapper toWrapper(Object value) {
		return (value != null ? new SimpleValueWrapper(value) : null);
	public void clear() {
		// TODO Auto-generated method stub

	public void evict(Object key) {
		this.client.delete(null, getKey(key));

	public ValueWrapper get(Object key) {
		Record record =  client.get(null, getKey(key));
		ValueWrapper vr = toWrapper(record.getValue(VALUE));
		return vr;

	public <T> T get(Object key, Class<T> type) {
		return (T) client.get(null, getKey(key));

	public String getName() {
		return this.namespace+":"+this.set;

	public Object getNativeCache() {
		return client;

	public void put(Object key, Object value) {
		client.put(null, getKey(key), new Bin(VALUE, value));

	public ValueWrapper putIfAbsent(Object key, Object value) {
		Record record = client.operate(this.createOnly, getKey(key), Operation.put(new Bin(VALUE, value)), Operation.get(VALUE));
		return toWrapper(record.getValue(VALUE));


I hope this helps