Legend:
- Unmodified
- Added
- Removed
-
jmemcached-core/src/main/java/com/thimbleware/jmemcached/Cache.java
r10 r14 32 32 public int getMisses; 33 33 34 public long casCounter; 35 34 36 protected CacheStorage cacheStorage; 37 38 39 public enum StoreResponse { 40 STORED, NOT_STORED, EXISTS, NOT_FOUND 41 } 35 42 36 43 … … 40 47 */ 41 48 private final ReadWriteLock cacheReadWriteLock; 42 43 49 44 50 /** … … 90 96 * @return the message response string 91 97 */ 92 protected boolean add(MCElement e) { 93 try { 94 startCacheRead(); 95 return !is_there(e.keystring) && set(e); 98 protected StoreResponse add(MCElement e) { 99 try { 100 startCacheRead(); 101 if (is_there(e.keystring)) return set(e); 102 else return StoreResponse.NOT_STORED; 96 103 } finally { 97 104 finishCacheRead(); … … 105 112 * @return the message response string 106 113 */ 107 public boolean replace(MCElement e) { 108 try { 109 startCacheRead(); 110 return is_there(e.keystring) && set(e); 111 } finally { 112 finishCacheRead(); 113 } 114 } 114 public StoreResponse replace(MCElement e) { 115 try { 116 startCacheRead(); 117 if (is_there(e.keystring)) return set(e); 118 else return StoreResponse.NOT_STORED; 119 } finally { 120 finishCacheRead(); 121 } 122 } 123 115 124 116 125 /** … … 120 129 * @return the message response string 121 130 */ 122 protected booleanset(MCElement e) {131 protected StoreResponse set(MCElement e) { 123 132 try { 124 133 startCacheWrite(); 125 134 setCmds += 1;//update stats 135 136 // increment the CAS counter; put in the new CAS 137 e.cas_unique = casCounter++; 138 126 139 this.cacheStorage.put(e.keystring, e); 127 return true; 140 return StoreResponse.STORED; 141 } finally { 142 finishCacheWrite(); 143 } 144 } 145 146 public StoreResponse cas(Long cas_key, MCElement e) { 147 try { 148 startCacheWrite(); 149 // have to get the element 150 MCElement element = get(e.keystring); 151 if (element == null) 152 return StoreResponse.NOT_FOUND; 153 154 if (element.cas_unique == cas_key) { 155 // cas_unique matches, now set the element 156 return set(e); 157 } else { 158 // cas didn't match; someone else beat us to it 159 return StoreResponse.EXISTS; 160 } 161 128 162 } finally { 129 163 finishCacheWrite(); … … 157 191 e.data = valueOf(old_val).getBytes(); // toString 158 192 e.data_length = e.data.length; 193 194 // assign new cas id 195 e.cas_unique = casCounter++; 196 159 197 this.cacheStorage.put(e.keystring, e); // save new value 160 198 return old_val; … … 345 383 return getMisses; 346 384 } 385 347 386 }
