|
| 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; |
0 commit comments