Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using memcache inside Google Compute Engine with PHP

I am trying to test using App Engine's Memcache with our servers running under Compute Engine. Currently we just have a couple VM instances which run Memcache where we call:

$memcache->addServer('memcache', 11211);

to reference each server. Looking at Google's sample code, it doesn't mention anything about what server we should call. I tried to test the below code from their document but it errors on creating the object. I understand that I might have to include a class, but it didn't mention anything in the document or what server to call. Can anyone help?

<?php

header('Content-Type: text/plain');

echo "Setting Value\n";
$memcache = new Memcached;

echo "Get who value<br>";
$who = $memcache->get('who');

echo 'Previously incremented by ' . $who . "\n";
$memcache->set('who', 'PHP');

$count = $memcache->increment('count', 1, 0);
echo 'Count incremented by PHP = ' .  $count . "\n";
like image 368
Brad Wickwire Avatar asked May 09 '15 00:05

Brad Wickwire


People also ask

How enable memcache in PHP INI?

To enable the PHP memcache extensions, build PHP using the --enable-memcache option to configure when building from source. On Debian-based distributions, use the php-memcache package. To set global runtime configuration options, specify the configuration option values within your php. ini file.

What is memcache in Google App Engine?

Shared memcache is the free default for App Engine applications. It provides cache capacity on a best-effort basis and is subject to the overall demand of all the App Engine applications using the shared memcache service. Dedicated memcache provides a fixed cache capacity assigned exclusively to your application.


1 Answers

Google App Engine provides a hosted Memcache service while Google Compute Engine does not.

On App Engine, the connection to the server is made automatically for the app running on App Engine, which means you don't need to specify a host/port in the app.

On Compute Engine, however, if you want to use memcache, you will need to run your own memcached server, either in the same or different VM as your application, and specify its host/port in your PHP client.

PHP provides two classes to connect to memcached:

  • Memcache
  • Memcached

Each provides a method to specify server(s) to connect to, e.g.,

  • Memcache::addServer
  • Memcached::addServer

but it's still up to you to run these memcached servers.

like image 128
Misha Brukman Avatar answered Nov 15 '22 10:11

Misha Brukman