Skip to content

Commit

Permalink
Merge pull request #258 from benjchristensen/only-cache-when-needed
Browse files Browse the repository at this point in the history
Skip CachedObservableOriginal when no getCacheKey()
  • Loading branch information
benjchristensen committed May 5, 2014
2 parents 3ba9f78 + 1bd64e0 commit 07b8fb4
Showing 1 changed file with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -785,9 +785,11 @@ private ObservableCommand<R> toObservable(Scheduler observeOn, boolean performAs
throw new IllegalStateException("This instance can only be executed once. Please instantiate a new instance.");
}

final String _cacheKey = getCacheKey();

/* try from cache first */
if (isRequestCachingEnabled()) {
Observable<R> fromCache = requestCache.get(getCacheKey());
if (isRequestCachingEnabled() && _cacheKey != null) {
Observable<R> fromCache = requestCache.get(_cacheKey);
if (fromCache != null) {
/* mark that we received this response from cache */
metrics.markResponseFromCache();
Expand Down Expand Up @@ -875,10 +877,10 @@ public void call() {
});

// put in cache
if (isRequestCachingEnabled()) {
if (isRequestCachingEnabled() && _cacheKey != null) {
// wrap it for caching
o = new CachedObservableOriginal<R>(o.cache(), this);
Observable<R> fromCache = requestCache.putIfAbsent(getCacheKey(), o);
Observable<R> fromCache = requestCache.putIfAbsent(_cacheKey, o);
if (fromCache != null) {
// another thread beat us so we'll use the cached value instead
o = new CachedObservableResponse<R>((CachedObservableOriginal<R>) fromCache, this);
Expand Down

0 comments on commit 07b8fb4

Please sign in to comment.