11/**
2- * Copyright 2015 Google Inc. All Rights Reserved.
2+ * Copyright 2016 Google Inc. All Rights Reserved.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2121import com .google .appengine .api .memcache .MemcacheServiceFactory ;
2222
2323import java .io .IOException ;
24- import java .math .BigInteger ;
2524import java .util .logging .Level ;
2625
2726import javax .servlet .ServletException ;
@@ -36,27 +35,23 @@ public class MemcacheServlet extends HttpServlet {
3635 @ Override
3736 public void doGet (HttpServletRequest req , HttpServletResponse resp ) throws IOException ,
3837 ServletException {
38+ String path = req .getRequestURI ();
39+ if (path .startsWith ("/favicon.ico" )) {
40+ return ; // ignore the request for favicon.ico
41+ }
42+
3943 MemcacheService syncCache = MemcacheServiceFactory .getMemcacheService ();
4044 syncCache .setErrorHandler (ErrorHandlers .getConsistentLogAndContinue (Level .INFO ));
41- String key = "count" ;
42- byte [] value ;
43- long count = 1 ;
44- value = (byte []) syncCache .get (key );
45- if (value == null ) {
46- value = BigInteger .valueOf (count ).toByteArray ();
47- syncCache .put (key , value );
48- } else {
49- // Increment value
50- count = new BigInteger (value ).longValue ();
51- count ++;
52- value = BigInteger .valueOf (count ).toByteArray ();
53- // Put back in cache
54- syncCache .put (key , value );
55- }
5645
57- // Output content
58- resp .setContentType ("text/plain" );
59- resp .getWriter ().print ("Value is " + count + "\n " );
46+ byte [] whoKey = "who" .getBytes ();
47+ byte [] countKey = "count" .getBytes ();
48+
49+ byte [] who = (byte []) syncCache .get (whoKey );
50+ String whoString = who == null ? "nobody" : new String (who );
51+ resp .getWriter ().print ("Previously incremented by " + whoString + "\n " );
52+ syncCache .put (whoKey , "Java" .getBytes ());
53+ Long count = syncCache .increment (countKey , 1L , 0L );
54+ resp .getWriter ().print ("Count incremented by Java = " + count + "\n " );
6055 }
6156}
6257// [END example]
0 commit comments