Skip to content

Commit 543107e

Browse files
Shun Fanlesv
Shun Fan
authored andcommitted
Update memcache sample with code from docs pages (GoogleCloudPlatform#176)
1 parent 6dae2cb commit 543107e

File tree

6 files changed

+257
-6
lines changed

6 files changed

+257
-6
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.appengine.memcache;
18+
19+
import com.google.appengine.api.memcache.AsyncMemcacheService;
20+
import com.google.appengine.api.memcache.ErrorHandlers;
21+
import com.google.appengine.api.memcache.MemcacheServiceFactory;
22+
23+
import java.io.IOException;
24+
import java.math.BigInteger;
25+
import java.util.concurrent.ExecutionException;
26+
import java.util.concurrent.Future;
27+
import java.util.logging.Level;
28+
29+
import javax.servlet.ServletException;
30+
import javax.servlet.http.HttpServlet;
31+
import javax.servlet.http.HttpServletRequest;
32+
import javax.servlet.http.HttpServletResponse;
33+
34+
@SuppressWarnings("serial")
35+
public class MemcacheAsyncCacheServlet extends HttpServlet {
36+
37+
@Override
38+
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,
39+
ServletException {
40+
String path = req.getRequestURI();
41+
if (path.startsWith("/favicon.ico")) {
42+
return; // ignore the request for favicon.ico
43+
}
44+
45+
// [START example]
46+
AsyncMemcacheService asyncCache = MemcacheServiceFactory.getAsyncMemcacheService();
47+
asyncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));
48+
String key = "count-async";
49+
byte[] value;
50+
long count = 1;
51+
Future<Object> futureValue = asyncCache.get(key); // Read from cache.
52+
// ... Do other work in parallel to cache retrieval.
53+
try {
54+
value = (byte[]) futureValue.get();
55+
if (value == null) {
56+
value = BigInteger.valueOf(count).toByteArray();
57+
asyncCache.put(key, value);
58+
} else {
59+
// Increment value
60+
count = new BigInteger(value).longValue();
61+
count++;
62+
value = BigInteger.valueOf(count).toByteArray();
63+
// Put back in cache
64+
asyncCache.put(key, value);
65+
}
66+
} catch (InterruptedException | ExecutionException e) {
67+
throw new ServletException("Error when waiting for future value", e);
68+
}
69+
// [END example]
70+
71+
// Output content
72+
resp.setContentType("text/plain");
73+
resp.getWriter().print("Value is " + count + "\n");
74+
}
75+
}

appengine/memcache/src/main/java/com/example/appengine/memcache/MemcacheServlet.java renamed to appengine/memcache/src/main/java/com/example/appengine/memcache/MemcacheBestPracticeServlet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
// [START example]
3232
@SuppressWarnings("serial")
33-
public class MemcacheServlet extends HttpServlet {
33+
public class MemcacheBestPracticeServlet extends HttpServlet {
3434

3535
@Override
3636
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.appengine.memcache;
18+
19+
import com.google.appengine.api.memcache.MemcacheService;
20+
import com.google.appengine.api.memcache.MemcacheService.IdentifiableValue;
21+
import com.google.appengine.api.memcache.MemcacheServiceFactory;
22+
23+
import java.io.IOException;
24+
import java.math.BigInteger;
25+
26+
import javax.servlet.ServletException;
27+
import javax.servlet.http.HttpServlet;
28+
import javax.servlet.http.HttpServletRequest;
29+
import javax.servlet.http.HttpServletResponse;
30+
31+
// [START example]
32+
@SuppressWarnings("serial")
33+
public class MemcacheConcurrentServlet extends HttpServlet {
34+
35+
@Override
36+
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,
37+
ServletException {
38+
String path = req.getRequestURI();
39+
if (path.startsWith("/favicon.ico")) {
40+
return; // ignore the request for favicon.ico
41+
}
42+
43+
String key = "count-concurrent";
44+
// Using the synchronous cache.
45+
MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
46+
47+
// Write this value to cache using getIdentifiable and putIfUntouched.
48+
for (long delayMs = 1; delayMs < 1000; delayMs *= 2) {
49+
IdentifiableValue oldValue = syncCache.getIdentifiable(key);
50+
byte[] newValue = oldValue == null
51+
? BigInteger.valueOf(0).toByteArray()
52+
: increment((byte[]) oldValue.getValue()); // newValue depends on old value
53+
resp.setContentType("text/plain");
54+
resp.getWriter().print("Value is " + new BigInteger(newValue).intValue() + "\n");
55+
if (oldValue == null) {
56+
// Key doesn't exist. We can safely put it in cache.
57+
syncCache.put(key, newValue);
58+
break;
59+
} else if (syncCache.putIfUntouched(key, oldValue, newValue)) {
60+
// newValue has been successfully put into cache.
61+
break;
62+
} else {
63+
// Some other client changed the value since oldValue was retrieved.
64+
// Wait a while before trying again, waiting longer on successive loops.
65+
try {
66+
Thread.sleep(delayMs);
67+
} catch (InterruptedException e) {
68+
throw new ServletException("Error when sleeping", e);
69+
}
70+
}
71+
}
72+
}
73+
74+
/**
75+
* Increments an integer stored as a byte array by one.
76+
* @param oldValue a byte array with the old value
77+
* @return a byte array as the old value increased by one
78+
*/
79+
private byte[] increment(byte[] oldValue) {
80+
long val = new BigInteger(oldValue).intValue();
81+
val++;
82+
return BigInteger.valueOf(val).toByteArray();
83+
}
84+
}
85+
// [END example]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.appengine.memcache;
18+
19+
import com.google.appengine.api.memcache.ErrorHandlers;
20+
import com.google.appengine.api.memcache.MemcacheService;
21+
import com.google.appengine.api.memcache.MemcacheServiceFactory;
22+
23+
import java.io.IOException;
24+
import java.math.BigInteger;
25+
import java.util.logging.Level;
26+
27+
import javax.servlet.ServletException;
28+
import javax.servlet.http.HttpServlet;
29+
import javax.servlet.http.HttpServletRequest;
30+
import javax.servlet.http.HttpServletResponse;
31+
32+
// [START example]
33+
@SuppressWarnings("serial")
34+
public class MemcacheSyncCacheServlet extends HttpServlet {
35+
36+
@Override
37+
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,
38+
ServletException {
39+
String path = req.getRequestURI();
40+
if (path.startsWith("/favicon.ico")) {
41+
return; // ignore the request for favicon.ico
42+
}
43+
44+
MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
45+
syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));
46+
String key = "count-sync";
47+
byte[] value;
48+
long count = 1;
49+
value = (byte[]) syncCache.get(key);
50+
if (value == null) {
51+
value = BigInteger.valueOf(count).toByteArray();
52+
syncCache.put(key, value);
53+
} else {
54+
// Increment value
55+
count = new BigInteger(value).longValue();
56+
count++;
57+
value = BigInteger.valueOf(count).toByteArray();
58+
// Put back in cache
59+
syncCache.put(key, value);
60+
}
61+
62+
// Output content
63+
resp.setContentType("text/plain");
64+
resp.getWriter().print("Value is " + count + "\n");
65+
}
66+
}
67+
// [END example]

appengine/memcache/src/main/webapp/WEB-INF/appengine-web.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<!-- [START_EXCLUDE] -->
33
<!--
4-
Copyright 2015 Google Inc. All Rights Reserved.
4+
Copyright 2016 Google Inc. All Rights Reserved.
55
Licensed under the Apache License, Version 2.0 (the "License");
66
you may not use this file except in compliance with the License.
77
You may obtain a copy of the License at

appengine/memcache/src/main/webapp/WEB-INF/web.xml

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<!-- [START_EXCLUDE] -->
33
<!--
4-
Copyright 2015 Google Inc. All Rights Reserved.
4+
Copyright 2016 Google Inc. All Rights Reserved.
55
Licensed under the Apache License, Version 2.0 (the "License");
66
you may not use this file except in compliance with the License.
77
You may obtain a copy of the License at
@@ -18,12 +18,36 @@
1818
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
1919
version="2.5">
2020
<servlet>
21-
<servlet-name>memcache</servlet-name>
22-
<servlet-class>com.example.appengine.memcache.MemcacheServlet</servlet-class>
21+
<servlet-name>memcache-best-practice</servlet-name>
22+
<servlet-class>com.example.appengine.memcache.MemcacheBestPracticeServlet</servlet-class>
2323
</servlet>
2424
<servlet-mapping>
25-
<servlet-name>memcache</servlet-name>
25+
<servlet-name>memcache-best-practice</servlet-name>
2626
<url-pattern>/</url-pattern>
2727
</servlet-mapping>
28+
<servlet>
29+
<servlet-name>memcache-async</servlet-name>
30+
<servlet-class>com.example.appengine.memcache.MemcacheAsyncCacheServlet</servlet-class>
31+
</servlet>
32+
<servlet-mapping>
33+
<servlet-name>memcache-async</servlet-name>
34+
<url-pattern>/async</url-pattern>
35+
</servlet-mapping>
36+
<servlet>
37+
<servlet-name>memcache-sync</servlet-name>
38+
<servlet-class>com.example.appengine.memcache.MemcacheSyncCacheServlet</servlet-class>
39+
</servlet>
40+
<servlet-mapping>
41+
<servlet-name>memcache-sync</servlet-name>
42+
<url-pattern>/sync</url-pattern>
43+
</servlet-mapping>
44+
<servlet>
45+
<servlet-name>memcache-concurrent</servlet-name>
46+
<servlet-class>com.example.appengine.memcache.MemcacheConcurrentServlet</servlet-class>
47+
</servlet>
48+
<servlet-mapping>
49+
<servlet-name>memcache-concurrent</servlet-name>
50+
<url-pattern>/concurrent</url-pattern>
51+
</servlet-mapping>
2852
</web-app>
2953

0 commit comments

Comments
 (0)