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

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

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

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