couchbaselabs / couchbaselabs/couchbase-spring-cache

CouchbaseCache.get(<key>, <valueLoader>) more resilient

Open
#15 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
15
Forks
15
PR merge metrics
No merged PRs in 30d

Description

Hi theres,

I've been testing this library over the past days and I have a concern/doubt about how the method 'CouchbaseCache.get(, )' behaves when an exception in Bucket.get is thrown.

As I could notice none of the possible runtime exceptions that are throwed are handled in, what makes my applications fails when Couchbase has a temporarily failure. Could this method catch all these exceptions and just call the valueLoader?

Something Like:
```@Override
public T get(final Object key, final Callable valueLoader) {
try {
final String documentId = getDocumentId(key.toString());
SerializableDocument doc = client.get(documentId, SerializableDocument.class);
if (doc == null && valueLoader != null) {
synchronized (client) {
doc = client.get(documentId, SerializableDocument.class);
if (doc == null) {
T value = callValueLoader(key, valueLoader);
put(key, value);
return value;
}
}
}

if (doc != null) {
return (T) doc.content();
}
} catch(Exception ex) {
//log error
return callValueLoader(key, valueLoader);
}
return null;
}

private T callValueLoader(final Object key, final Callable valueLoader) {
try {
return valueLoader.call();
} catch (ValueRetrievalException ex) {
throw ex;
} catch (Exception e) {
throw new ValueRetrievalException(key, valueLoader, e);
}
}
```

Let me know whether this makes sense to you, so I can create a pull request with tests and a better code than this one on the example.

Thanks,

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.