<?php
/**
 *
 * @author：llbjj
 * @DateTime：2022/5/7 7:05
 * @Description：
 *
 */

namespace Application\Service\Redis;

use Application\Service\Extension\Laminas;
use Application\Service\Extension\Swoole\ConnectPool;
use Laminas\Cache\Storage\Adapter\Redis;
use Swoole\Coroutine;

class RedisExtend extends Redis
{

    private bool $isCustomDatabase = false;

    public function __get($name)
    {
        // TODO: Implement __get() method.
    }

    /**
     * Notes: redis list 左存入队列
     * User: llbjj
     * DateTime: 2022/5/7 9:07
     *
     * @param $pushData
     * @param String $listKey
     */
    public function lPush($pushData, String $listKey = 'redisList') {
        if(is_null($pushData) || $pushData == '') return;

        $redisResource = $this->getRedisResource();
        if(is_array($pushData) and !empty($pushData)){
            foreach ($pushData as $datum){
                $redisResource->lPush($listKey, $datum);
            }
        }

        if(is_string($pushData)){
            $redisResource->lPush($listKey, $pushData);
        }
    }

    /**
     * Notes: redis list 右存入队列
     * User: llbjj
     * DateTime: 2022/7/20 15:37
     *
     * @param $pushData
     * @param String $listKey
     */
    public function rPush($pushData, String $listKey = 'redisList'){
        if(is_null($pushData) || $pushData == '') return;

        $redisResource = $this->getRedisResource();
        if(is_array($pushData) and !empty($pushData)){
            foreach ($pushData as $datum){
                $redisResource->rPush($listKey, $datum);
            }
        }

        if(is_string($pushData)){
            $redisResource->lPush($listKey, $pushData);
        }
    }

    /**
     * Notes: redis list 左取出队列
     * User: llbjj
     * DateTime: 2022/5/7 9:07
     *
     * @param $listKey
     */
    public function lPop($listKey = 'redisList') {
        return $this->getRedisResource()->lPop($listKey);
    }

    /**
     * Notes: redis list 右取出队列
     * User: llbjj
     * DateTime: 2022/7/20 15:36
     *
     * @param string $listKey
     * @return bool|mixed
     */
    public function rPop($listKey = 'redisList'){
        return $this->getRedisInstance()->rPop($listKey);
    }

    public function getRedisInstance()
    {
        if ($this->isCustomDatabase === false) {
            $defaultDb = Laminas::$app->getConfig()['caches']['Redis']['adapter']['options']['database'];
            $this->setDatabase($defaultDb);
        }

        // 好像不支持切库, 必须重新实例化
        if (Coroutine::getCid() > -1) {
            return ConnectPool::redis();
        }

        // 每次获取实例都清空一次database设置。保证如果不设置database 就是用配置内默认的database
        $this->isCustomDatabase = false;
        return $this->getRedisResource();
    }

    public function setDatabase(int $db)
    {
        $this->isCustomDatabase = true;
        $this->getOptions()->setDatabase($db);
        return $this;
    }

    public function setNamespace(string $namespace) {
        $this->getOptions()->setNamespace($namespace);
        return $this;
    }

    public function setTtl(int $ttl) {
        $this->getOptions()->setTtl($ttl);
        return $this;
    }
}