55import java .util .concurrent .TimeUnit ;
66
77import com .netflix .evcache .EVCacheKey ;
8+ import com .netflix .evcache .operation .EVCacheItem ;
89import net .spy .memcached .MemcachedClientIF ;
910import net .spy .memcached .transcoders .Transcoder ;
1011import org .slf4j .Logger ;
@@ -49,7 +50,7 @@ public EVCacheClientUtil(String appName, long operationTimeout) {
4950 /**
5051 * TODO : once metaget is available we need to get the remaining ttl from an existing entry and use it
5152 */
52- public EVCacheLatch add (EVCacheKey evcKey , final CachedData cd , Transcoder evcacheValueTranscoder , int timeToLive , Policy policy , final EVCacheClient [] clients , int latchCount , boolean fixMissing , boolean bypassAddOpt , boolean fixupAsFail ) throws Exception {
53+ public EVCacheLatch add (EVCacheKey evcKey , final CachedData cd , Transcoder evcacheValueTranscoder , int timeToLive , Policy policy , final EVCacheClient [] clients , int latchCount , boolean fixMissing , boolean bypassAddOpt , boolean fixupAsFail , boolean newFixup ) throws Exception {
5354 if (cd == null ) return null ;
5455
5556 final EVCacheLatchImpl latch = new EVCacheLatchImpl (policy , latchCount , _appName );
@@ -83,11 +84,11 @@ public EVCacheLatch add(EVCacheKey evcKey, final CachedData cd, Transcoder evcac
8384 return latch ;
8485 }
8586 else {
86- return fixup (client , clients , evcKey , timeToLive , policy , latch , fixupAsFail );
87+ return fixup (client , clients , evcKey , timeToLive , policy , latch , fixupAsFail , newFixup );
8788 }
8889 } else {
8990 if (log .isDebugEnabled ()) log .debug ("Add failed after first client. key: " + key + ", client : " + client );
90- return fixup (client , clients , evcKey , timeToLive , policy , latch , fixupAsFail );
91+ return fixup (client , clients , evcKey , timeToLive , policy , latch , fixupAsFail , newFixup );
9192 }
9293 }
9394 if (firstStatus == null ) firstStatus = Boolean .valueOf (status );
@@ -96,16 +97,28 @@ public EVCacheLatch add(EVCacheKey evcKey, final CachedData cd, Transcoder evcac
9697 return latch ;
9798 }
9899
99- private EVCacheLatch fixup (EVCacheClient sourceClient , EVCacheClient [] destClients , EVCacheKey evcKey , int timeToLive , Policy policy , EVCacheLatchImpl prevLatch , boolean fixupAsFail ) {
100+ private EVCacheLatch fixup (EVCacheClient sourceClient , EVCacheClient [] destClients , EVCacheKey evcKey , int timeToLive , Policy policy , EVCacheLatchImpl prevLatch , boolean fixupAsFail , boolean newFixup ) {
100101 if (log .isDebugEnabled ()) log .debug ("Trying to fix up!! destClient count = " + destClients .length );
101102 final EVCacheLatchImpl latch = new EVCacheLatchImpl (policy , destClients .length , _appName );
102103 try {
103- final CachedData readData = sourceClient .get (evcKey .getDerivedKey (sourceClient .isDuetClient (), sourceClient .getHashingAlgorithm (), sourceClient .shouldEncodeHashKey (), sourceClient .getMaxDigestBytes (), sourceClient .getMaxHashLength (), sourceClient .getBaseEncoder ()), ct , false , false );
104+ CachedData readData = null ;
105+ int ttlToSet = timeToLive ;
106+ if (newFixup ) {
107+ final EVCacheItem <CachedData > obj = sourceClient .metaGet (evcKey .getDerivedKey (sourceClient .isDuetClient (), sourceClient .getHashingAlgorithm (), sourceClient .shouldEncodeHashKey (), sourceClient .getMaxDigestBytes (), sourceClient .getMaxHashLength (), sourceClient .getBaseEncoder ()), ct , false , false );
108+ if (obj != null ) {
109+ readData = obj .getData ();
110+ ttlToSet = (int ) obj .getItemMetaData ().getSecondsLeftToExpire ();
111+ if (log .isDebugEnabled ()) log .debug ("Applying new fixup. ttlToSet = " + ttlToSet );
112+ }
113+ }
114+ else {
115+ readData = sourceClient .get (evcKey .getDerivedKey (sourceClient .isDuetClient (), sourceClient .getHashingAlgorithm (), sourceClient .shouldEncodeHashKey (), sourceClient .getMaxDigestBytes (), sourceClient .getMaxHashLength (), sourceClient .getBaseEncoder ()), ct , false , false );
116+ }
104117
105118 if (readData != null ) {
106- sourceClient .touch (evcKey .getDerivedKey (sourceClient .isDuetClient (), sourceClient .getHashingAlgorithm (), sourceClient .shouldEncodeHashKey (), sourceClient .getMaxDigestBytes (), sourceClient .getMaxHashLength (), sourceClient .getBaseEncoder ()), timeToLive );
119+ sourceClient .touch (evcKey .getDerivedKey (sourceClient .isDuetClient (), sourceClient .getHashingAlgorithm (), sourceClient .shouldEncodeHashKey (), sourceClient .getMaxDigestBytes (), sourceClient .getMaxHashLength (), sourceClient .getBaseEncoder ()), ttlToSet );
107120 for (EVCacheClient destClient : destClients ) {
108- destClient .set (evcKey .getDerivedKey (destClient .isDuetClient (), destClient .getHashingAlgorithm (), destClient .shouldEncodeHashKey (), destClient .getMaxDigestBytes (), destClient .getMaxHashLength (), destClient .getBaseEncoder ()), readData , timeToLive , latch );
121+ destClient .set (evcKey .getDerivedKey (destClient .isDuetClient (), destClient .getHashingAlgorithm (), destClient .shouldEncodeHashKey (), destClient .getMaxDigestBytes (), destClient .getMaxHashLength (), destClient .getBaseEncoder ()), readData , ttlToSet , latch );
109122 }
110123 }
111124 latch .await (_operationTimeout , TimeUnit .MILLISECONDS );
0 commit comments