Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions appengine/flexible/memcache/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Memcache and Google App Engine Flexible Environment

This sample application demonstrates how to use memcache with Google App Engine.

## Prerequisites

- Install [`composer`](https://getcomposer.org)
- Install dependencies by running:

```sh
$ composer install
```

## Deploy to App Engine

**Prerequisites**

- Install the [Google Cloud SDK](https://developers.google.com/cloud/sdk/).

**Deploy with gcloud**

```
$ gcloud config set project YOUR_PROJECT_ID
$ gcloud preview app deploy
```

**Store and retrieve values from the cache.**

```
$ echo hello > hello.txt
$ echo bye > bye.txt
$ curl http://{YOUR_PROJECT_ID}.appspot.com/memcache/a
# Store the value hello in /a.
$ curl http://{YOUR_PROJECT_ID}.appspot.com/memcache/a -T hello.txt
$ curl http://{YOUR_PROJECT_ID}.appspot.com/memcache/a
hello
```
113 changes: 113 additions & 0 deletions appengine/flexible/memcache/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* Copyright 2015 Google Inc.
*
* 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.
*/

# [START memcached]
use Silex\Application;
use Silex\Provider\TwigServiceProvider;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

// create the Silex application
$app = new Application();
$app->register(new TwigServiceProvider());
$app['twig.path'] = [ __DIR__ ];
$app['memcached'] = function () {
$addr = getenv('MEMCACHE_PORT_11211_TCP_ADDR');
$port = (int) getenv('MEMCACHE_PORT_11211_TCP_PORT');
$memcached = new Memcached;
if (!$memcached->addServer($addr, $port)) {
throw new Exception("Failed to add server $addr:$port");
}
return $memcached;
};
# [END memcached]

$app->get('/vars', function () {
$vars = array('MEMCACHE_PORT_11211_TCP_ADDR',
'MEMCACHE_PORT_11211_TCP_PORT');
$lines = array();
foreach ($vars as $var) {
$val = getenv($var);
array_push($lines, "$var = $val");
}
return new Response(
implode("\n", $lines),
200,
['Content-Type' => 'text/plain']);
});

$app->get('/', function (Application $app, Request $request) {
/** @var Twig_Environment $twig */
$twig = $app['twig'];
/** @var Memcached $memcached */
$memcached = $app['memcached'];
return $twig->render('memcache.html.twig', [
'who' => $memcached->get('who'),
'count' => $memcached->get('count'),
'host' => $request->getHttpHost(),
]);
});

$app->post('/reset', function (Application $app, Request $request) {
/** @var Twig_Environment $twig */
$twig = $app['twig'];
/** @var Memcached $memcached */
$memcached = $app['memcached'];
$memcached->delete('who');
$memcached->set('count', 0);
return $twig->render('memcache.html.twig', [
'host' => $request->getHttpHost(),
'count' => 0,
'who' => '',
]);
});

$app->post('/', function (Application $app, Request $request) {
/** @var Twig_Environment $twig */
$twig = $app['twig'];
/** @var Memcached $memcached */
$memcached = $app['memcached'];
$memcached->set('who', $request->get('who'));
$count = $memcached->increment('count');
if (false === $count) {
// Potential race condition. Use binary protocol to avoid.
$memcached->set('count', 0);
$count = 0;
}
return $twig->render('memcache.html.twig', [
'who' => $request->get('who'),
'count' => $count,
'host' => $request->getHttpHost(),
]);
});

# [START memcached]
$app->get('/memcached/{key}', function (Application $app, $key) {
/** @var Memcached $memcached */
$memcached = $app['memcached'];
return $memcached->get($key);
});

$app->put('/memcached/{key}', function (Application $app, $key, Request $request) {
/** @var Memcached $memcached */
$memcached = $app['memcached'];
$value = $request->getContent();
return $memcached->set($key, $value);
});
# [END memcached]

return $app;
5 changes: 5 additions & 0 deletions appengine/flexible/memcache/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
runtime: php
vm: true

runtime_config:
document_root: web
12 changes: 12 additions & 0 deletions appengine/flexible/memcache/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"require": {
"silex/silex": "^1.3",
"twig/twig": "^1.24"
},
"require-dev": {
"phpunit/phpunit": "~4",
"guzzlehttp/guzzle": "^6.2",
"monolog/monolog": "^1.19",
"google/cloud-tools": "^0.1.0"
}
}
Loading