/**
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.appengine.memcache;

import com.google.appengine.api.memcache.ErrorHandlers;
import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;

import java.io.IOException;
import java.math.BigInteger;
import java.util.logging.Level;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// [START example]
@SuppressWarnings("serial")
public class MemcacheServlet extends HttpServlet {

  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,
      ServletException {
    MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
    syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));
    String key = "count";
    byte[] value;
    long count = 1;
    value = (byte[]) syncCache.get(key);
    if (value == null) {
      value = BigInteger.valueOf(count).toByteArray();
      syncCache.put(key, value);
    } else {
      // Increment value
      count = new BigInteger(value).longValue();
      count++;
      value = BigInteger.valueOf(count).toByteArray();
      // Put back in cache
      syncCache.put(key, value);
    }

    // Output content
    resp.setContentType("text/plain");
    resp.getWriter().print("Value is " + count + "\n");
  }
}
// [END example]

@@ -17,9 +17,9 @@
  201
-5
+6
  Goo
@@ -860,37 +860,8 @@
 on;%0A
-import java.math.BigInteger;%0A
 impo
@@ -1287,16 +1287,157 @@
 ption %7B%0A
+    String path = req.getRequestURI();%0A    if (path.startsWith(%22/favicon.ico%22)) %7B%0A      return; // ignore the request for favicon.ico%0A    %7D%0A%0A
     Memc
@@ -1595,81 +1595,104 @@
 ));%0A
+%0A
     
-String key = %22count%22;%0A    byte%5B%5D value;%0A    long count = 1;%0A    value
+byte%5B%5D whoKey = %22who%22.getBytes();%0A    byte%5B%5D countKey = %22count%22.getBytes();%0A%0A    byte%5B%5D who
  = (
@@ -1713,17 +1713,20 @@
 che.get(
-k
+whoK
 ey);%0A   
@@ -1730,399 +1730,245 @@
     
-if (value == null) %7B%0A      value = BigInteger.valueOf(count).toByteArray();%0A      syncCache.put(key, value);%0A    %7D else %7B%0A      // Increment value%0A      count = new BigInteger(value).longValue();%0A      count++;%0A      value = BigInteger.valueOf(count).toByteArray();%0A      // Put back in cache%0A      syncCache.put(key, value);%0A    %7D%0A%0A    // Output content%0A    resp.setContentType(%22text/plain%22
+String whoString = who == null ? %22nobody%22 : new String(who);%0A    resp.getWriter().print(%22Previously incremented by %22 + whoString + %22%5Cn%22);%0A    syncCache.put(whoKey, %22Java%22.getBytes());%0A    Long count = syncCache.increment(countKey, 1L, 0L
 );%0A 
@@ -1998,16 +1998,35 @@
 nt(%22
-Value is
+Count incremented by Java =
  %22 +

Update memcache with code from appengine documentation