Skip to content

Commit 9d4f056

Browse files
authored
updates memcache sample for env:flex memcache changes (GoogleCloudPlatform#258)
* updates memcache sample for env:flex memcache changes
1 parent 043ad08 commit 9d4f056

File tree

8 files changed

+353
-235
lines changed

8 files changed

+353
-235
lines changed

appengine/flexible/memcache/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ $ composer install
1616
**Prerequisites**
1717

1818
- Install the [Google Cloud SDK](https://developers.google.com/cloud/sdk/).
19+
- Set up memcache using [Redis Labs Memcache Cloud][redis labs memcache].
20+
- edit `app.yaml` and update the environment variables for your Memcache
21+
instance.
1922

2023
**Deploy with gcloud**
2124

@@ -33,4 +36,6 @@ $ curl http://{YOUR_PROJECT_ID}.appspot.com/memcached/a
3336
$ curl http://{YOUR_PROJECT_ID}.appspot.com/memcached/a -T hello.txt
3437
$ curl http://{YOUR_PROJECT_ID}.appspot.com/memcached/a
3538
hello
36-
```
39+
```
40+
41+
[redis labs memcache]: https://cloud.google.com/appengine/docs/flexible/python/using-redislabs-memcache

appengine/flexible/memcache/app.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,20 @@
2626
$app->register(new TwigServiceProvider());
2727
$app['twig.path'] = [ __DIR__ ];
2828
$app['memcached'] = function () {
29-
$addr = getenv('MEMCACHE_PORT_11211_TCP_ADDR');
30-
$port = (int) getenv('MEMCACHE_PORT_11211_TCP_PORT');
29+
if (getenv('USE_GAE_MEMCACHE')) {
30+
$addr = getenv('GAE_MEMCACHE_HOST') ?: 'localhost';
31+
$port = getenv('GAE_MEMCACHE_PORT') ?: '11211';
32+
} else {
33+
$server = getenv('MEMCACHE_SERVER') ?: 'localhost:11211';
34+
list($addr, $port) = explode(':', $server);
35+
}
36+
$username = getenv('MEMCACHE_USERNAME');
37+
$password = getenv('MEMCACHE_PASSWORD');
3138
$memcached = new Memcached;
39+
if ($username && $password) {
40+
$memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
41+
$memcached->setSaslAuthData($username, $password);
42+
}
3243
if (!$memcached->addServer($addr, $port)) {
3344
throw new Exception("Failed to add server $addr:$port");
3445
}

appengine/flexible/memcache/app.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,16 @@ runtime: php
22
vm: true
33

44
runtime_config:
5-
document_root: web
5+
document_root: web
6+
7+
# [START env_variables]
8+
env_variables:
9+
# If you are using the App Engine Memcache service (currently in alpha),
10+
# uncomment this section and comment out the other Memcache variables.
11+
# USE_GAE_MEMCACHE: 1
12+
MEMCACHE_SERVER: your-memcache-server
13+
# If you are using a Memcached server with SASL authentiation enabled,
14+
# fill in these values with your username and password.
15+
MEMCACHE_USERNAME: ""
16+
MEMCACHE_PASSWORD: ""
17+
# [END env_variables]

appengine/flexible/memcache/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"require-dev": {
77
"gecko-packages/gecko-memcache-mock": "^2.0",
88
"google/cloud-tools": "<=1.0",
9+
"paragonie/random_compat": "^2.0",
910
"guzzlehttp/guzzle": "^6.2",
1011
"monolog/monolog": "^1.19",
1112
"phpunit/phpunit": "~4"

0 commit comments

Comments
 (0)