SmartWiki开发日记之Laravel缓存扩展

时间:2021-09-18 09:37:56

SmartWiki简介请阅读: http://www.cnblogs.com/lifeil/p/6113323.html

因为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;
}

网上有流传的laravel缓存扩展的文章,其中对配置读取在5.2以上版本不适用。

缓存扩展后的代码是需要创建一个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

SmartWiki官网: https://www.iminho.me

SmartWiki源码: https://github.com/lifei6671/SmartWiki