-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENHANCE: Seperate FrontCacheLogic in asyncGetBulk from MemcachedClien…
…t to FrontCacheMemcachedClient.
- Loading branch information
Showing
5 changed files
with
156 additions
and
47 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
60 changes: 60 additions & 0 deletions
60
src/main/java/net/spy/memcached/plugin/FrontCacheBulkGetFuture.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,60 @@ | ||
package net.spy.memcached.plugin; | ||
|
||
import net.spy.memcached.internal.BulkGetFuture; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
public class FrontCacheBulkGetFuture<T> extends BulkGetFuture<T> { | ||
private final LocalCacheManager localCacheManager; | ||
|
||
private final Map<String, T> localCachedData = new HashMap<String, T>(); | ||
|
||
public FrontCacheBulkGetFuture(BulkGetFuture<T> parentFuture, | ||
LocalCacheManager localCacheManager) { | ||
super(parentFuture.getRvMap(), parentFuture.getOps(), parentFuture.getLatch()); | ||
this.localCacheManager = localCacheManager; | ||
} | ||
|
||
public Map<String, T> get() throws InterruptedException, ExecutionException { | ||
Map<String, T> noneCachedValue = null; | ||
try { | ||
noneCachedValue = super.get(Long.MAX_VALUE, TimeUnit.MILLISECONDS); | ||
} catch (TimeoutException e) { | ||
throw new RuntimeException(e); | ||
} | ||
putLocalCache(noneCachedValue); | ||
noneCachedValue.putAll(localCachedData); | ||
return noneCachedValue; | ||
} | ||
|
||
public Map<String, T> get(long duration, TimeUnit units) | ||
throws InterruptedException, ExecutionException, TimeoutException { | ||
Map<String, T> noneCachedValue = null; | ||
try { | ||
noneCachedValue = super.get(duration, units); | ||
} catch (TimeoutException e) { | ||
throw new RuntimeException(e); | ||
} | ||
putLocalCache(noneCachedValue); | ||
noneCachedValue.putAll(localCachedData); | ||
return noneCachedValue; | ||
} | ||
|
||
public void addLocalCacheData(String key, T value) { | ||
localCachedData.put(key, value); | ||
} | ||
|
||
private void putLocalCache(Map<String, T> noneCachedValue) { | ||
for (Map.Entry<String, T> entry : noneCachedValue.entrySet()) { | ||
String key = entry.getKey(); | ||
T value = entry.getValue(); | ||
if (value != null) { | ||
localCacheManager.put(key, value); | ||
} | ||
} | ||
} | ||
} |
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