-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ed1b4a
commit ee08943
Showing
9 changed files
with
267 additions
and
12 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
46 changes: 46 additions & 0 deletions
46
konfigyr-crypto-api/src/main/java/com/konfigyr/crypto/KeysetCache.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,46 @@ | ||
package com.konfigyr.crypto; | ||
|
||
import org.springframework.lang.NonNull; | ||
|
||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Interface that defines how encrypted cryptographic material is cached. | ||
* | ||
* @author : Vladimir Spasic | ||
* @since : 06.09.23, Wed | ||
**/ | ||
public interface KeysetCache { | ||
|
||
/** | ||
* Retrieves the {@link EncryptedKeyset} with the specified cached key, obtaining that | ||
* value from {@link Supplier} if it is not present. | ||
* <p> | ||
* If possible, implementations of this method should ensure that the loading | ||
* operation is synchronized so that the specified supplying function is only called | ||
* once in case of concurrent access on the same key. | ||
* @param key the key for which the {@link EncryptedKeyset} is to be returned, can't | ||
* be {@literal null} | ||
* @param supplier function that would obtain the value if the cached value is not | ||
* present in the cache, can't be {@literal null} | ||
* @return cached encrypted keyset, never {@literal null} | ||
*/ | ||
EncryptedKeyset get(@NonNull String key, @NonNull Supplier<EncryptedKeyset> supplier); | ||
|
||
/** | ||
* Stores the {@link EncryptedKeyset} value with the specified key in this cache. | ||
* @param key the key for which the {@link EncryptedKeyset} is to be associated, can't | ||
* be {@literal null} | ||
* @param keyset the {@link EncryptedKeyset} value to be associated with the specified | ||
* key | ||
*/ | ||
void put(@NonNull String key, @NonNull EncryptedKeyset keyset); | ||
|
||
/** | ||
* Evicts the {@link EncryptedKeyset} for this key from this cache if it is present. | ||
* @param key the key for which the {@link EncryptedKeyset} is to be evicted, can't be | ||
* {@literal null} | ||
*/ | ||
void evict(@NonNull String key); | ||
|
||
} |
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
51 changes: 51 additions & 0 deletions
51
konfigyr-crypto-api/src/main/java/com/konfigyr/crypto/SpringKeysetCache.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,51 @@ | ||
package com.konfigyr.crypto; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.cache.Cache; | ||
import org.springframework.lang.NonNull; | ||
|
||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Implementation of the {@link KeysetCache} that uses Spring {@link Cache} implementation | ||
* as the actual cache store. | ||
* | ||
* @author : Vladimir Spasic | ||
* @since : 06.09.23, Wed | ||
**/ | ||
@RequiredArgsConstructor | ||
public class SpringKeysetCache implements KeysetCache { | ||
|
||
private final Cache cache; | ||
|
||
@Override | ||
public synchronized EncryptedKeyset get(@NonNull String key, @NonNull Supplier<EncryptedKeyset> supplier) { | ||
EncryptedKeyset encryptedKeyset = cache.get(key, EncryptedKeyset.class); | ||
|
||
if (encryptedKeyset != null) { | ||
return encryptedKeyset; | ||
} | ||
|
||
encryptedKeyset = supplier.get(); | ||
|
||
if (encryptedKeyset == null) { | ||
throw new IllegalStateException("Keyset cache detected a null encrypted keyset value for " + "cache key '" | ||
+ key + "'. This is currently not supported by this cache implementation."); | ||
} | ||
|
||
put(key, encryptedKeyset); | ||
|
||
return encryptedKeyset; | ||
} | ||
|
||
@Override | ||
public synchronized void put(@NonNull String key, @NonNull EncryptedKeyset keyset) { | ||
cache.put(key, keyset); | ||
} | ||
|
||
@Override | ||
public synchronized void evict(@NonNull String key) { | ||
cache.evict(key); | ||
} | ||
|
||
} |
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.