问题
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";
回答1:
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.
来源:https://stackoverflow.com/questions/30134733/using-memcache-inside-google-compute-engine-with-php