forked from spring-attic/spring-data-aerospike
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FMWK-459 Fix requiring id property from entities by AerospikeCache (#752
) * id is not required for entities in cached methods * use hashCode userKey instead of String in AerospikeCache, add tests * make serialization and hashing methods overridable * create AerospikeCacheKeyProcessor bean to enable overriding it with a different implementation if needed
- Loading branch information
Showing
13 changed files
with
560 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/org/springframework/data/aerospike/cache/AerospikeCacheKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.springframework.data.aerospike.cache; | ||
|
||
import com.aerospike.client.Value; | ||
import lombok.Getter; | ||
|
||
/** | ||
* Wrapper class used in caching. Receives hash of the cache key as a String, a long number or a byte array. | ||
*/ | ||
public class AerospikeCacheKey { | ||
|
||
@Getter | ||
private final Value value; | ||
|
||
private AerospikeCacheKey(String string) { | ||
this.value = new Value.StringValue(string); | ||
} | ||
|
||
private AerospikeCacheKey(long number) { | ||
this.value = new Value.LongValue(number); | ||
} | ||
|
||
private AerospikeCacheKey(byte[] data) { | ||
this.value = new Value.BytesValue(data); | ||
} | ||
|
||
/** | ||
* Instantiate AerospikeCacheKey instance with a String. | ||
* | ||
* @param string String parameter | ||
* @return new instance of AerospikeCacheKey initialized with the given parameter | ||
*/ | ||
public static AerospikeCacheKey of(String string) { | ||
return new AerospikeCacheKey(string); | ||
} | ||
|
||
/** | ||
* Instantiate AerospikeCacheKey instance with a long number. | ||
* | ||
* @param number long number | ||
* @return new instance of AerospikeCacheKey initialized with the given parameter | ||
*/ | ||
public static AerospikeCacheKey of(long number) { | ||
return new AerospikeCacheKey(number); | ||
} | ||
|
||
/** | ||
* Instantiate AerospikeCacheKey instance with a byte array. | ||
* | ||
* @param data byte array | ||
* @return new instance of AerospikeCacheKey initialized with the given parameter | ||
*/ | ||
public static AerospikeCacheKey of(byte[] data) { | ||
return new AerospikeCacheKey(data); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/org/springframework/data/aerospike/cache/AerospikeCacheKeyProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.springframework.data.aerospike.cache; | ||
|
||
/** | ||
* Interface that provides methods used in caching | ||
*/ | ||
public interface AerospikeCacheKeyProcessor { | ||
|
||
/** | ||
* Serialize the given key and calculate hash based on the serialization result. | ||
* | ||
* @param key Object to be serialized and hashed | ||
* @return AerospikeCacheKey instantiated with either a String or a long number | ||
*/ | ||
AerospikeCacheKey serializeAndHash(Object key); | ||
|
||
/** | ||
* Serialize the given key. | ||
* <p> | ||
* The default implementation uses Kryo. | ||
* | ||
* @param key Object to be serialized | ||
* @return byte[] | ||
*/ | ||
byte[] serialize(Object key); | ||
|
||
/** | ||
* Calculate hash based on the given byte array. | ||
* <p> | ||
* The default implementation uses 128 bit Murmur3 hashing. | ||
* | ||
* @param data Byte array to be hashed | ||
* @return AerospikeCacheKey instantiated with either a String or a long number | ||
*/ | ||
AerospikeCacheKey calculateHash(byte[] data); | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/org/springframework/data/aerospike/cache/AerospikeCacheKeyProcessorImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.springframework.data.aerospike.cache; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
import com.esotericsoftware.kryo.io.ByteBufferOutput; | ||
import lombok.Getter; | ||
import org.apache.commons.codec.digest.MurmurHash3; | ||
import org.objenesis.strategy.StdInstantiatorStrategy; | ||
|
||
import java.util.Arrays; | ||
|
||
public class AerospikeCacheKeyProcessorImpl implements AerospikeCacheKeyProcessor { | ||
|
||
@Getter | ||
private final Kryo kryoInstance = new Kryo(); | ||
|
||
public AerospikeCacheKeyProcessorImpl() { | ||
configureKryo(); | ||
} | ||
|
||
/** | ||
* Configuration for Kryo instance. | ||
* <p> | ||
* Classes of the objects to be cached can be pre-registered if required. Registering in advance is not necessary, | ||
* however it can be done to increase serialization performance. If a class has been pre-registered, the first time | ||
* it is encountered Kryo can just output a numeric reference to it instead of writing fully qualified class name. | ||
*/ | ||
public void configureKryo() { | ||
// setting to false means not requiring registration for all the classes of cached objects in advance | ||
getKryoInstance().setRegistrationRequired(false); | ||
getKryoInstance().setInstantiatorStrategy(new StdInstantiatorStrategy()); | ||
} | ||
|
||
public AerospikeCacheKey serializeAndHash(Object key) { | ||
return calculateHash(serialize(key)); | ||
} | ||
|
||
public byte[] serialize(Object key) { | ||
ByteBufferOutput output = new ByteBufferOutput(1024); // Initial buffer size | ||
kryoInstance.writeClassAndObject(output, key); | ||
output.flush(); | ||
return output.toBytes(); | ||
} | ||
|
||
public AerospikeCacheKey calculateHash(byte[] data) { | ||
return AerospikeCacheKey.of(Arrays.toString(MurmurHash3.hash128(data))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.