因為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 中創建Memced對象的程式碼如下:
laravel的Memcached沒有設定setOption方法的選項,僅包含最簡連接建立,緊接著就呼叫getVersion來測試是否連通。而阿里雲的示範程式碼是設定了關閉壓縮和使用binary二進位協定的選項的。 沒辦法只能自己來擴充Memcached的功能實作自訂選項。 laravel中擴充快取可以使用Cache::extend來擴充。擴充代碼如下: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; }
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; }
'providers' => [ SmartWiki\Providers\MemcachedExtendServiceProvider::class // 在providers节点添加实现的provider ]
'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::extend('MemcachedExtend',function ($app){ $memcached = $this->createMemcached($app); return new MemcachedSessionHandler($memcached); });