1
1
/**
2
- * Copyright 2015 Google Inc. All Rights Reserved.
2
+ * Copyright 2016 Google Inc. All Rights Reserved.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
21
21
import com .google .appengine .api .memcache .MemcacheServiceFactory ;
22
22
23
23
import java .io .IOException ;
24
- import java .math .BigInteger ;
25
24
import java .util .logging .Level ;
26
25
27
26
import javax .servlet .ServletException ;
@@ -36,27 +35,23 @@ public class MemcacheServlet extends HttpServlet {
36
35
@ Override
37
36
public void doGet (HttpServletRequest req , HttpServletResponse resp ) throws IOException ,
38
37
ServletException {
38
+ String path = req .getRequestURI ();
39
+ if (path .startsWith ("/favicon.ico" )) {
40
+ return ; // ignore the request for favicon.ico
41
+ }
42
+
39
43
MemcacheService syncCache = MemcacheServiceFactory .getMemcacheService ();
40
44
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
- }
56
45
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 " );
60
55
}
61
56
}
62
57
// [END example]
0 commit comments