使用阿里云 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,

剩下的该怎么用就怎么用了

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!