Skip to content

Commit 004c538

Browse files
Merge pull request GoogleCloudPlatform#107 from GoogleCloudPlatform/squashMemFlex2
Add an app engine flex memcache sample.
2 parents 9dafc0b + 7a49f46 commit 004c538

File tree

11 files changed

+2420
-0
lines changed

11 files changed

+2420
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Memcache and Google App Engine Flexible Environment
2+
3+
This sample application demonstrates how to use memcache with Google App Engine.
4+
5+
## Prerequisites
6+
7+
- Install [`composer`](https://getcomposer.org)
8+
- Install dependencies by running:
9+
10+
```sh
11+
$ composer install
12+
```
13+
14+
## Deploy to App Engine
15+
16+
**Prerequisites**
17+
18+
- Install the [Google Cloud SDK](https://developers.google.com/cloud/sdk/).
19+
20+
**Deploy with gcloud**
21+
22+
```
23+
$ gcloud config set project YOUR_PROJECT_ID
24+
$ gcloud preview app deploy
25+
```
26+
27+
**Store and retrieve values from the cache.**
28+
29+
```
30+
$ echo hello > hello.txt
31+
$ echo bye > bye.txt
32+
$ curl http://{YOUR_PROJECT_ID}.appspot.com/memcache/a
33+
# Store the value hello in /a.
34+
$ curl http://{YOUR_PROJECT_ID}.appspot.com/memcache/a -T hello.txt
35+
$ curl http://{YOUR_PROJECT_ID}.appspot.com/memcache/a
36+
hello
37+
```
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* Copyright 2015 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
# [START memcached]
19+
use Silex\Application;
20+
use Silex\Provider\TwigServiceProvider;
21+
use Symfony\Component\HttpFoundation\Request;
22+
use Symfony\Component\HttpFoundation\Response;
23+
24+
// create the Silex application
25+
$app = new Application();
26+
$app->register(new TwigServiceProvider());
27+
$app['twig.path'] = [ __DIR__ ];
28+
$app['memcached'] = function () {
29+
$addr = getenv('MEMCACHE_PORT_11211_TCP_ADDR');
30+
$port = (int) getenv('MEMCACHE_PORT_11211_TCP_PORT');
31+
$memcached = new Memcached;
32+
if (!$memcached->addServer($addr, $port)) {
33+
throw new Exception("Failed to add server $addr:$port");
34+
}
35+
return $memcached;
36+
};
37+
# [END memcached]
38+
39+
$app->get('/vars', function () {
40+
$vars = array('MEMCACHE_PORT_11211_TCP_ADDR',
41+
'MEMCACHE_PORT_11211_TCP_PORT');
42+
$lines = array();
43+
foreach ($vars as $var) {
44+
$val = getenv($var);
45+
array_push($lines, "$var = $val");
46+
}
47+
return new Response(
48+
implode("\n", $lines),
49+
200,
50+
['Content-Type' => 'text/plain']);
51+
});
52+
53+
$app->get('/', function (Application $app, Request $request) {
54+
/** @var Twig_Environment $twig */
55+
$twig = $app['twig'];
56+
/** @var Memcached $memcached */
57+
$memcached = $app['memcached'];
58+
return $twig->render('memcache.html.twig', [
59+
'who' => $memcached->get('who'),
60+
'count' => $memcached->get('count'),
61+
'host' => $request->getHttpHost(),
62+
]);
63+
});
64+
65+
$app->post('/reset', function (Application $app, Request $request) {
66+
/** @var Twig_Environment $twig */
67+
$twig = $app['twig'];
68+
/** @var Memcached $memcached */
69+
$memcached = $app['memcached'];
70+
$memcached->delete('who');
71+
$memcached->set('count', 0);
72+
return $twig->render('memcache.html.twig', [
73+
'host' => $request->getHttpHost(),
74+
'count' => 0,
75+
'who' => '',
76+
]);
77+
});
78+
79+
$app->post('/', function (Application $app, Request $request) {
80+
/** @var Twig_Environment $twig */
81+
$twig = $app['twig'];
82+
/** @var Memcached $memcached */
83+
$memcached = $app['memcached'];
84+
$memcached->set('who', $request->get('who'));
85+
$count = $memcached->increment('count');
86+
if (false === $count) {
87+
// Potential race condition. Use binary protocol to avoid.
88+
$memcached->set('count', 0);
89+
$count = 0;
90+
}
91+
return $twig->render('memcache.html.twig', [
92+
'who' => $request->get('who'),
93+
'count' => $count,
94+
'host' => $request->getHttpHost(),
95+
]);
96+
});
97+
98+
# [START memcached]
99+
$app->get('/memcached/{key}', function (Application $app, $key) {
100+
/** @var Memcached $memcached */
101+
$memcached = $app['memcached'];
102+
return $memcached->get($key);
103+
});
104+
105+
$app->put('/memcached/{key}', function (Application $app, $key, Request $request) {
106+
/** @var Memcached $memcached */
107+
$memcached = $app['memcached'];
108+
$value = $request->getContent();
109+
return $memcached->set($key, $value);
110+
});
111+
# [END memcached]
112+
113+
return $app;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
runtime: php
2+
vm: true
3+
4+
runtime_config:
5+
document_root: web
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"require": {
3+
"silex/silex": "^1.3",
4+
"twig/twig": "^1.24"
5+
},
6+
"require-dev": {
7+
"phpunit/phpunit": "~4",
8+
"guzzlehttp/guzzle": "^6.2",
9+
"monolog/monolog": "^1.19",
10+
"google/cloud-tools": "^0.1.0"
11+
}
12+
}

0 commit comments

Comments
 (0)