Php 在云端托管的Laravel应用程序上配置缓存服务器
我使用的是Laravel框架,我在Heroku上托管我的登台和生产环境 我想使用诸如Memcached之类的缓存。在我的本地机器上,一切运行正常,我可以存储值并在我运行的缓存中检索它们 但是,一旦部署到Heroku上,在缓存中设置和访问密钥不会做任何事情,也不会导致错误 经过一些调查,我相信问题在于与我使用的Memcached服务的连接(在我的例子中是Memcached cloud) 如何为我的暂存和生产环境设置Laravel,以连接到外部Memcached提供程序 我尝试使用在我的应用程序上安装Memcached cloud后提供的用户名和密码,但Laravel没有使用它们 在cache.php中Php 在云端托管的Laravel应用程序上配置缓存服务器,php,caching,heroku,laravel,Php,Caching,Heroku,Laravel,我使用的是Laravel框架,我在Heroku上托管我的登台和生产环境 我想使用诸如Memcached之类的缓存。在我的本地机器上,一切运行正常,我可以存储值并在我运行的缓存中检索它们 但是,一旦部署到Heroku上,在缓存中设置和访问密钥不会做任何事情,也不会导致错误 经过一些调查,我相信问题在于与我使用的Memcached服务的连接(在我的例子中是Memcached cloud) 如何为我的暂存和生产环境设置Laravel,以连接到外部Memcached提供程序 我尝试使用在我的应用程序上安
$memcachedURL = parse_url(getenv("MEMCACHEDCLOUD_SERVERS"));
我应该写一个Memcached连接器来使用这样的用户名和密码吗?如果是的话,你会有一些关于如何开始做这件事的参考资料吗?我只是在调查同样的事情。这似乎有两种方法
\illumb\Cache\MemcachedConnector
以包括setaslauthdata
调用,并将其绑定到IoC容器中MemcachedConnector
class()有一个getMemcached()
方法,该方法返回Memcached
的实例。根据http://php.net/manual/en/memcached.setsaslauthdata.php
您可以在Memcached实例上设置SASL身份验证凭据
因此,您可以将
\illumb\Cache\MemcachedConnector子类化,并将其绑定到您自己的服务提供商中的$app['memcached.connector']
。然后,子类可以覆盖getMemcached()
以包含凭据。更多关于IoC基础扩展的信息:。多亏了@tscheepers和@atyagi,以下是我用来让它与Memcached cloud和Heroku一起工作的代码
我还在Packagist和Github上提供了一个包
创建Memcached云连接器
class MemcachedCloudConnector {
/**
* Extend the Memcached connection to use MemcachedCloud via Heroku
*
* @param array $servers
* @throws \RuntimeException
* @return \Memcached
*/
public function connect(array $servers)
{
$memcached = $this->getMemcached();
// Set Elasticache options here
if (defined('\Memcached::OPT_BINARY_PROTOCOL')) {
$memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
}
$memcached->addServers(array_map(function($server) { return explode(':', $server, 2); }, explode(',', $_ENV['MEMCACHEDCLOUD_SERVERS'])));
$memcached->setSaslAuthData($_ENV['MEMCACHEDCLOUD_USERNAME'], $_ENV['MEMCACHEDCLOUD_PASSWORD']);
if ($memcached->getVersion() === false) {
throw new \RuntimeException("Could not establish Memcached connection.");
}
return $memcached;
}
/**
* Get a new Memcached instance.
*
* @return \Memcached
*/
protected function getMemcached()
{
return new Memcached;
}
}
通过服务提供商将其绑定到IoC
// Based on https://github.com/atyagi/elasticache-laravel
class MemcachedCloudServiceProvider extends ServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$servers = $this->app['config']->get('cache.memcached');
$memcachedCloud = new MemcachedCloudConnector();
$memcached = $memcachedCloud->connect($servers);
$this->app->getProviderRepository()->load($this->app, array('Illuminate\Cache\CacheServiceProvider'));
$this->app->make('cache')->extend('memcached', function() use ($memcached) {
/** @noinspection PhpUndefinedNamespaceInspection */
/** @noinspection PhpUndefinedClassInspection */
return new \Illuminate\Cache\Repository(
new \Illuminate\Cache\MemcachedStore($memcached, $this->app['config']->get('cache.prefix')));
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array();
}
}
不要忘记在app.php文件中引用新的服务提供商,并确保您的Memcached ID已注册为Heroku应用程序中的环境变量。不是Laravel专家,但请注意,Memcached Cloud应使用SASL(二进制)连接来自Heroku的Memcached协议。我遇到了一个为Elasticache构建的类似扩展。它使用您提到的第二种解决方案。我将其改编为Memcached Cloud,并没有覆盖getMemcached()方法,而是覆盖connect()方法。我正在粘贴代码作为答案
// Based on https://github.com/atyagi/elasticache-laravel
class MemcachedCloudServiceProvider extends ServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$servers = $this->app['config']->get('cache.memcached');
$memcachedCloud = new MemcachedCloudConnector();
$memcached = $memcachedCloud->connect($servers);
$this->app->getProviderRepository()->load($this->app, array('Illuminate\Cache\CacheServiceProvider'));
$this->app->make('cache')->extend('memcached', function() use ($memcached) {
/** @noinspection PhpUndefinedNamespaceInspection */
/** @noinspection PhpUndefinedClassInspection */
return new \Illuminate\Cache\Repository(
new \Illuminate\Cache\MemcachedStore($memcached, $this->app['config']->get('cache.prefix')));
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array();
}
}