forked from spring-attic/spring-data-aerospike
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace hash implementation with a faster one, make serialization and…
… hashing methods overridable
- Loading branch information
Showing
6 changed files
with
131 additions
and
90 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
38 changes: 38 additions & 0 deletions
38
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,38 @@ | ||
package org.springframework.data.aerospike.cache; | ||
|
||
import com.aerospike.client.Value; | ||
import lombok.Getter; | ||
|
||
public class AerospikeCacheKey { | ||
|
||
@Getter | ||
private Value value; | ||
|
||
private AerospikeCacheKey(String string) { | ||
this.value = new Value.StringValue(string); | ||
} | ||
|
||
private AerospikeCacheKey(long number) { | ||
this.value = new Value.LongValue(number); | ||
} | ||
|
||
/** | ||
* Instantiate AerospikeCacheKey instance with a String. | ||
* | ||
* @param string String parameter | ||
* @return AerospikeCacheKey | ||
*/ | ||
public static AerospikeCacheKey of(String string) { | ||
return new AerospikeCacheKey(string); | ||
} | ||
|
||
/** | ||
* Instantiate AerospikeCacheKey instance with a long number. | ||
* | ||
* @param number long number | ||
* @return AerospikeCacheKey | ||
*/ | ||
public static AerospikeCacheKey of(long number) { | ||
return new AerospikeCacheKey(number); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
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,74 @@ | ||
package org.springframework.data.aerospike.cache; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
import com.esotericsoftware.kryo.io.ByteBufferOutput; | ||
import net.jpountz.xxhash.XXHash64; | ||
import net.jpountz.xxhash.XXHashFactory; | ||
import org.objenesis.strategy.StdInstantiatorStrategy; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
public class AerospikeCacheKeyProcessor { | ||
|
||
private static final Kryo kryoInstance = new Kryo(); | ||
|
||
public AerospikeCacheKeyProcessor() { | ||
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 | ||
kryoInstance.setRegistrationRequired(false); | ||
kryoInstance.setInstantiatorStrategy(new StdInstantiatorStrategy()); | ||
} | ||
|
||
/** | ||
* 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 | ||
*/ | ||
public AerospikeCacheKey serializeAndHash(Object key) { | ||
return calculateHash(serialize(key)); | ||
} | ||
|
||
/** | ||
* Serialize the given key. | ||
* <p> | ||
* The default implementation uses Kryo. | ||
* <p> | ||
* The method can be overridden if different serialization implementation is required. | ||
* | ||
* @param key Object to be serialized | ||
* @return byte[] | ||
*/ | ||
public byte[] serialize(Object key) { | ||
ByteBufferOutput output = new ByteBufferOutput(1024); // Initial buffer size | ||
kryoInstance.writeClassAndObject(output, key); | ||
output.flush(); | ||
return output.toBytes(); | ||
} | ||
|
||
/** | ||
* Calculate hash based on the given byte array. | ||
* <p> | ||
* The default implementation is 64 bit xxHash. | ||
* <p> | ||
* The method can be overridden if different hashing algorithm or implementation is required. | ||
* | ||
* @param data Byte array to be hashed | ||
* @return AerospikeCacheKey instantiated with either a String or a long number | ||
*/ | ||
public static AerospikeCacheKey calculateHash(byte[] data) { | ||
XXHash64 xxHash64 = XXHashFactory.fastestInstance().hash64(); | ||
ByteBuffer buffer = ByteBuffer.wrap(data); | ||
return AerospikeCacheKey.of(xxHash64.hash(buffer, 0, data.length, 0)); | ||
} | ||
} |
34 changes: 0 additions & 34 deletions
34
src/main/java/org/springframework/data/aerospike/cache/CacheUtils.java
This file was deleted.
Oops, something went wrong.
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