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