-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FMWK-48 Spring Data Aerospike Cacheable sync option #755
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,20 +112,48 @@ public Object getNativeCache() { | |
* @return The value (bins) to which this cache maps the specified key. | ||
*/ | ||
@Override | ||
@SuppressWarnings({"unchecked", "NullableProblems"}) | ||
@SuppressWarnings({"NullableProblems"}) | ||
public <T> T get(Object key, Callable<T> valueLoader) { | ||
T value = (T) client.get(null, getKey(key)).getValue(VALUE); | ||
if (Objects.isNull(value)) { | ||
try { | ||
value = valueLoader.call(); | ||
if (Objects.nonNull(value)) { | ||
put(key, value); | ||
Key dbKey = getKey(key); | ||
Record record = client.get(null, dbKey); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should go under the following |
||
if (record == null && valueLoader != null) { | ||
synchronized (this) { | ||
record = client.get(null, dbKey); | ||
if (record == null) { | ||
T value = callValueLoader(valueLoader, key); | ||
if (Objects.nonNull(value)) { | ||
put(key, value); | ||
} | ||
return value; | ||
} | ||
} catch (Exception e) { | ||
throw new Cache.ValueRetrievalException(key, valueLoader, e); | ||
} | ||
} | ||
return value; | ||
if (record.getValue(VALUE) != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
AerospikeReadData data = AerospikeReadData.forRead(dbKey, record); | ||
Class<T> type = getValueType(valueLoader); // determine the class of T | ||
return aerospikeConverter.read(type, data); | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for else. |
||
return null; | ||
} | ||
} | ||
|
||
private <T> T callValueLoader(Callable<T> valueLoader, Object key) { | ||
try { | ||
return valueLoader.call(); | ||
} catch (Exception e) { | ||
throw new Cache.ValueRetrievalException(key, valueLoader, e); | ||
} | ||
} | ||
|
||
// Helper method to determine the class of T | ||
@SuppressWarnings("unchecked") | ||
private static <T> Class<T> getValueType(Callable<T> valueLoader) { | ||
try { | ||
// Use reflection to get the return type of the Callable | ||
return (Class<T>) valueLoader.getClass().getMethod("call").getReturnType(); | ||
} catch (NoSuchMethodException e) { | ||
throw new IllegalArgumentException("Cannot determine value type", e); | ||
} | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.