使用阿里云 OCS 的一点坑
阿里云使用自己的开源项目Tair,这货不支持Memcached 的 stats
命令,所以导致无法直接在Cache
中使用
想使用OCS的话就跳过version检查就可以了。
新建一个Extension, AliyunOCSConnector.php
namespace App\Extensions;
use Illuminate\Cache\MemcachedConnector;
/**
* Aliyun OSC Connector
*/
class AliyunOCSConnector extends MemcachedConnector
{
/**
* Validate the given Memcached connection.
*
* @param \Memcached $memcached
* @return \Memcached
*/
protected function validateConnection($memcached)
{
// $status = $memcached->getVersion();
// if (! is_array($status)) {
// throw new RuntimeException('No Memcached servers added.');
// }
// disable status check for memcache check
// if (in_array('255.255.255', $status) && count(array_unique($status)) === 1) {
// throw new RuntimeException('Could not establish Memcached connection.');
// }
return $memcached;
}
}
新建一个ServiceProvider,AliyunCacheServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Cache\CacheServiceProvider;
use Illuminate\Cache\CacheManager;
use App\Extensions\AliyunOCSConnector;
/**
* 扩展和替换默认的CacheServiceProvider;
*/
class AliyunCacheServiceProvider extends CacheServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton('cache', function ($app) {
return new CacheManager($app);
});
$this->app->singleton('cache.store', function ($app) {
return $app['cache']->driver();
});
$this->app->singleton('memcached.connector', function () {
return new AliyunOCSConnector;
});
$this->registerCommands();
}
}
在config/app.php
中注释掉
// Illuminate\Cache\CacheServiceProvider::class,
增加自己的AliyunCacheServiceProvider
App\Providers\AliyunCacheServiceProvider::class,
剩下的该怎么用就怎么用了
推荐文章: