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.
add using Kryo to serialize cached objects
- Loading branch information
Showing
4 changed files
with
117 additions
and
20 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
34 changes: 34 additions & 0 deletions
34
src/main/java/org/springframework/data/aerospike/cache/CacheUtils.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,34 @@ | ||
package org.springframework.data.aerospike.cache; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
import com.esotericsoftware.kryo.io.ByteBufferOutput; | ||
|
||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
|
||
public class CacheUtils { | ||
|
||
private CacheUtils() { | ||
} | ||
|
||
protected static byte[] serialize(Object key, Kryo kryoInstance) { | ||
ByteBufferOutput output = new ByteBufferOutput(1024); // Initial buffer size | ||
kryoInstance.writeClassAndObject(output, key); | ||
output.flush(); | ||
return output.toBytes(); | ||
} | ||
|
||
protected static String sha256(byte[] data) throws NoSuchAlgorithmException { | ||
MessageDigest digest = MessageDigest.getInstance("SHA-256"); | ||
byte[] hash = digest.digest(data); | ||
return bytesToHex(hash); | ||
} | ||
|
||
private static String bytesToHex(byte[] bytes) { | ||
StringBuilder result = new StringBuilder(); | ||
for (byte b : bytes) { | ||
result.append(String.format("%02x", b)); | ||
} | ||
return result.toString(); | ||
} | ||
} |
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