因为smartwiki的演示站点部署在阿里云上,阿里云有一个128m免费的memcache服务,刚开始按照memcached的配置方式配置完后,发现laravel报错,查看日志报错位置是addserver出错,连不上阿里云的memcache。
很无奈,于是安装阿里云的手册写了一个脚本放到服务器上,结果可以连接,也可以写入。
阿里云提供的脚本如下:
<?php
$connect = new memcached; //声明一个新的memcached链接
$connect->setoption(memcached::opt_compression, false); //关闭压缩功能
$connect->setoption(memcached::opt_binary_protocol, true); //使用binary二进制协议
$connect->addserver('00000000.ocs.aliyuncs.com', 11211); //添加ocs实例地址及端口号
//$connect->setsaslauthdata('aaaaaaaaaa, 'password'); //设置ocs帐号密码进行鉴权,如已开启免密码功能,则无需此步骤
$connect->set("hello", "world");
echo 'hello: ',$connect->get("hello");
print_r( $connect->getversion());
$connect->quit();
翻看laravel的memcached驱动,在 /vendor/laravel/framework/src/illuminate/cache/memcachedconnector.php 中创建memcached对象的代码如下:
public function connect(array $servers)
{
$memcached = $this->getmemcached();
// for each server in the array, we'll just extract the configuration and add
// the server to the memcached connection. once we have added all of these
// servers we'll verify the connection is successful and return it back.
foreach ($servers as $server) {
$memcached->addserver(
$server['host'], $server['port'], $server['weight']
);
}
$memcachedstatus = $memcached->getversion();
if (! is_array($memcachedstatus)) {
throw new runtimeexception('no memcached servers added.');
}
if (in_array('255.255.255', $memcachedstatus) && count(array_unique($memcachedstatus)) === 1) {
throw new runtimeexception('could not establish memcached connection.');
}
return $memcached;
}
可以看到laravel的memcached没有设置setoption方法的选项,仅仅包含最简连接建立,紧接着就调用getversion来测试是否连通。而阿里云的演示代码是设置了关闭压缩和使用binary二进制协议的选项的。
没办法只能自己来扩展memcached的功能实现自定义选项。laravel中扩展缓存可以使用cache::extend来扩展。扩展代码如下:
cache::extend('memcachedextend', function ($app) {
$memcached = $this->creatememcached($app);
// 从配置文件中读取缓存前缀
$prefix = $app['config']['cache.prefix'];
// 创建 memcachedstore 对象
$store = new memcachedstore($memcached, $prefix);
// 创建 repository 对象,并返回
return new repository($store);
});
/**
* 创建memcached对象
* @param $app
* @return mixed
*/
protected function creatememcached($app)
{
// 从配置文件中读取 memcached 服务器配置
$servers = $app['config']['cache.stores.memcachedextend.servers'];
// 利用 illuminate\cache\memcachedconnector 类来创建新的 memcached 对象
$memcached = new \memcached;
foreach ($servers as $server) {
$memcached->addserver(
$server['host'], $server['port'], $server['weight']
);
}
// 如果服务器上的 php memcached 扩展支持 sasl 认证
if (ini_get('memcached.use_sasl') && isset($app['config']['cache.storess.memcachedextend.memcached_user']) && isset($app['config']['cache.storess.memcachedextend.memcached_pass'])) {
// 从配置文件中读取 sasl 认证用户名
$user = $app['config']['cache.storess.memcachedextend.memcached_user'];
// 从配置文件中读取 sasl 认证密码
$pass = $app['config']['cache.storess.memcachedextend.memcached_pass'];
// 指定用于 sasl 认证的账号密码
$memcached->setsaslauthdata($user, $pass);
}
//扩展
if (isset($app['config']['cache.stores.memcachedextend.options'])) {
foreach ($app['config']['cache.stores.memcachedextend.options'] as $key => $option) {
$memcached->setoption($key, $option);
}
}
$memcachedstatus = $memcached->getversion();
if (! is_array($memcachedstatus)) {
throw new runtimeexception('no memcached servers added.');
}
if (in_array('255.255.255', $memcachedstatus) && count(array_unique($memcachedstatus)) === 1) {
throw new runtimeexception('could not establish memcached connection.');
}
return $memcached;
}
缓存扩展后的代码是需要创建一个serviceprovider来注册服务提供者。服务提供者是laravel应用启动的中心,你自己的应用以及所有laravel的核心服务都是通过服务提供者启动。
但是,我们所谓的“启动”指的是什么?通常,这意味着注册事物,包括注册服务容器绑定、事件监听器、中间件甚至路由。服务提供者是应用配置的中心。
如果你打开laravel自带的config/app.php文件,将会看到一个providers数组,这里就是应用所要加载的所有服务提供者类,当然,其中很多是延迟加载的,也就是说不是每次请求都会被加载,只有真的用到它们的时候才会加载。
所有的服务提供者都继承自illuminate\support\serviceprovider类。大部分服务提供者都包含两个方法: register 和 boot 。在register方法中,你唯一要做的事情就是绑事物到服务容器,不要尝试在其中注册事件监听器,路由或者任何其它功能。
通过artisan命令make:provider可以简单生成一个新的提供者:
php artisan make:provider memcachedextendserviceprovider
所有服务提供者都是通过配置文件config/app.php中进行注册,该文件包含了一个列出所有服务提供者名字的providers数组,默认情况下,其中列出了所有核心服务提供者,这些服务提供者启动laravel核心组件,比如邮件、队列、缓存等等。
要注册你自己的服务提供者,只需要将其追加到该数组中即可:
'providers' => [
smartwiki\providers\memcachedextendserviceprovider::class //
在providers节点添加实现的provider
]
同时在config/cache.php中配置memcached配置:
'memcachedextend' => [
'driver' => 'memcachedextend',
'servers' => [
[
'host' => env('memcached_extend_host', '127.0.0.1'),
'port' => env('memcached_extend_port', 11211),
'weight' => 100,
],
],
'options' => [
\memcached::opt_binary_protocol => true,
\memcached::opt_compression => false
]
]
如果需要把session也储存到我们扩展的缓存中还需要调用session::extend来扩展我们的session储存:
session::extend('memcachedextend',function ($app){
$memcached = $this->creatememcached($app);
return new memcachedsessionhandler($memcached);
});
之后再.env中就可以配置我们扩展后的缓存了。完整代码如下:
<?php
namespace smartwiki\providers;
use illuminate\cache\repository;
use illuminate\cache\memcachedstore;
use illuminate\support\serviceprovider;
use cache;
use session;
use symfony\component\httpfoundation\session\storage\handler\memcachedsessionhandler;
use runtimeexception;
class memcachedextendserviceprovider extends serviceprovider
{
/**
* bootstrap the application services.
*
* @return void
*/
public function boot()
{
cache::extend('memcachedextend', function ($app) {
$memcached = $this->creatememcached($app);
// 从配置文件中读取缓存前缀
$prefix = $app['config']['cache.prefix'];
// 创建 memcachedstore 对象
$store = new memcachedstore($memcached, $prefix);
// 创建 repository 对象,并返回
return new repository($store);
});
session::extend('memcachedextend',function ($app){
$memcached = $this->creatememcached($app);
return new memcachedsessionhandler($memcached);
});
}
/**
* register the application services.
*
* @return void
*/
public function register()
{
//
}
/**
* 创建memcached对象
* @param $app
* @return mixed
*/
protected function creatememcached($app)
{
// 从配置文件中读取 memcached 服务器配置
$servers = $app['config']['cache.stores.memcachedextend.servers'];
// 利用 illuminate\cache\memcachedconnector 类来创建新的 memcached 对象
$memcached = new \memcached;
foreach ($servers as $server) {
$memcached->addserver(
$server['host'], $server['port'], $server['weight']
);
}
// 如果服务器上的 php memcached 扩展支持 sasl 认证
if (ini_get('memcached.use_sasl') && isset($app['config']['cache.storess.memcachedextend.memcached_user']) && isset($app['config']['cache.storess.memcachedextend.memcached_pass'])) {
// 从配置文件中读取 sasl 认证用户名
$user = $app['config']['cache.storess.memcachedextend.memcached_user'];
// 从配置文件中读取 sasl 认证密码
$pass = $app['config']['cache.storess.memcachedextend.memcached_pass'];
// 指定用于 sasl 认证的账号密码
$memcached->setsaslauthdata($user, $pass);
}
//扩展
if (isset($app['config']['cache.stores.memcachedextend.options'])) {
foreach ($app['config']['cache.stores.memcachedextend.options'] as $key => $option) {
$memcached->setoption($key, $option);
}
}
$memcachedstatus = $memcached->getversion();
if (! is_array($memcachedstatus)) {
throw new runtimeexception('no memcached servers added.');
}
if (in_array('255.255.255', $memcachedstatus) && count(array_unique($memcachedstatus)) === 1) {
throw new runtimeexception('could not establish memcached connection.');
}
return $memcached;
}
}
smartwikicode