Changeset 14:8b4b6177d6a2 for jmemcached-core/src/main/java/com/thimbleware/jmemcached/ServerSessionHandler.java
Legend:
- Unmodified
- Added
- Removed
-
jmemcached-core/src/main/java/com/thimbleware/jmemcached/ServerSessionHandler.java
r10 r14 22 22 import org.slf4j.LoggerFactory; 23 23 24 import static java.lang.Integer.parseInt; 25 import static java.lang.String.valueOf; 24 26 import java.nio.charset.CharacterCodingException; 25 27 import java.nio.charset.Charset; 26 28 import java.nio.charset.CharsetEncoder; 27 29 import java.util.Iterator; 28 import static java.lang.String.valueOf;29 import static java.lang.Integer.*;30 30 31 31 /** … … 50 50 public boolean verbose; 51 51 52 public static CharsetEncoder ENCODER = Charset.forName("US-ASCII").newEncoder();52 public static CharsetEncoder ENCODER = Charset.forName("US-ASCII").newEncoder(); 53 53 54 54 /** … … 59 59 * Construct the server session handler 60 60 * 61 * @param cache the cache to use61 * @param cache the cache to use 62 62 * @param memcachedVersion the version string to return to clients 63 * @param verbosity verbosity level for debugging64 * @param idle how long sessions can be idle for63 * @param verbosity verbosity level for debugging 64 * @param idle how long sessions can be idle for 65 65 */ 66 66 public ServerSessionHandler(Cache cache, String memcachedVersion, boolean verbosity, int idle) { … … 68 68 69 69 this.cache = cache; 70 70 71 71 started = Now(); 72 72 version = memcachedVersion; … … 141 141 142 142 ResponseMessage r = new ResponseMessage(); 143 if (cmd == Commands.GET ) {143 if (cmd == Commands.GET || cmd == Commands.GETS) { 144 144 for (int i = 0; i < cmdKeysSize; i++) { 145 145 MCElement result = get(command.keys.get(i)); 146 146 if (result != null) { 147 r.out.putString("VALUE " + result.keystring + " " + result.flags + " " + result.data_length + "\r\n", ENCODER);147 r.out.putString("VALUE " + result.keystring + " " + result.flags + " " + result.data_length + (cmd == Commands.GETS ? " " + result.cas_unique : "") + "\r\n", ENCODER); 148 148 r.out.put(result.data, 0, result.data_length); 149 149 r.out.putString("\r\n", ENCODER); … … 153 153 r.out.putString("END\r\n", ENCODER); 154 154 } else if (cmd == Commands.SET) { 155 r.out.putString(set(command.element), ENCODER); 155 String ret = set(command.element); 156 if (!command.noreply) 157 r.out.putString(ret, ENCODER); 158 } else if (cmd == Commands.CAS) { 159 String ret = cas(command.cas_key, command.element); 160 if (!command.noreply) 161 r.out.putString(ret, ENCODER); 156 162 } else if (cmd == Commands.ADD) { 157 r.out.putString(add(command.element), ENCODER); 163 String ret = add(command.element); 164 if (!command.noreply) 165 r.out.putString(ret, ENCODER); 158 166 } else if (cmd == Commands.REPLACE) { 159 r.out.putString(replace(command.element), ENCODER); 167 String ret = replace(command.element); 168 if (!command.noreply) 169 r.out.putString(ret, ENCODER); 160 170 } else if (cmd == Commands.INCR) { 161 171 r.out.putString(get_add(command.keys.get(0), parseInt(command.keys.get(1))), ENCODER); … … 207 217 /** 208 218 * Triggered when a session has gone idle. 219 * 209 220 * @param session the MINA session 210 * @param status the idle status221 * @param status the idle status 211 222 */ 212 223 public void sessionIdle(IoSession session, IdleStatus status) { … … 217 228 /** 218 229 * Triggered when an exception is caught by the protocol handler 230 * 219 231 * @param session the MINA session 220 * @param cause the exception232 * @param cause the exception 221 233 */ 222 234 public void exceptionCaught(IoSession session, Throwable cause) { … … 230 242 * Handle the deletion of an item from the cache. 231 243 * 232 * @param key the key for the item244 * @param key the key for the item 233 245 * @param time only delete the element if time (time in seconds) 234 246 * @return the message response … … 246 258 */ 247 259 protected String add(MCElement e) { 248 if (cache.add(e) ) return "STORED\r\n";260 if (cache.add(e) == Cache.StoreResponse.STORED) return "STORED\r\n"; 249 261 else return "NOT_STORED\r\n"; 262 } 263 264 protected String getStoreResponeString(Cache.StoreResponse storeResponse) { 265 switch (storeResponse) { 266 case EXISTS: 267 return "EXISTS\r\n"; 268 case NOT_FOUND: 269 return "NOT_FOUND\r\n"; 270 case NOT_STORED: 271 return "NOT_STORED\r\n"; 272 case STORED: 273 return "STORED\r\n"; 274 } 275 return null; 250 276 } 251 277 … … 257 283 */ 258 284 protected String replace(MCElement e) { 259 if (cache.replace(e)) return "STORED\r\n"; 260 else return "NOT_STORED\r\n"; 285 return getStoreResponeString(cache.replace(e)); 261 286 } 262 287 … … 268 293 */ 269 294 protected String set(MCElement e) { 270 if (cache.set(e)) return "STORED\r\n"; 271 else return "NOT_STORED\r\n"; 272 } 273 274 /** 275 * Increment an (integer) element inthe cache 295 return getStoreResponeString(cache.set(e)); 296 } 297 298 /** 299 * Check and set an element in the cache 300 * 301 * @param cas_key 302 * @param e the element to set @return the message response string 303 */ 304 protected String cas(Long cas_key, MCElement e) { 305 return getStoreResponeString(cache.cas(cas_key, e)); 306 } 307 308 /** 309 * Increment an (integer) element in the cache 310 * 276 311 * @param key the key to increment 277 312 * @param mod the amount to add to the value … … 289 324 /** 290 325 * Check whether an element is in the cache and non-expired 326 * 291 327 * @param key the key for the element to lookup 292 328 * @return whether the element is in the cache and is live … … 298 334 /** 299 335 * Get an element from the cache 336 * 300 337 * @param key the key for the element to lookup 301 338 * @return the element, or 'null' in case of cache miss. … … 326 363 /** 327 364 * Return runtime statistics 365 * 328 366 * @param arg additional arguments to the stats command 329 367 * @return the full command response … … 372 410 /** 373 411 * Flush all cache entries 412 * 374 413 * @return command response 375 414 */ … … 380 419 /** 381 420 * Flush all cache entries with a timestamp after a given expiration time 421 * 382 422 * @param expire the flush time in seconds 383 423 * @return command response … … 388 428 389 429 390 391 392 393 430 }
