<?php

namespace Application\Service\Extension\Wechat;

use Application\Service\Extension\Laminas;
use Psr\Cache\CacheItemInterface;
use Psr\SimpleCache\CacheInterface;

class WechatCache implements CacheInterface
{
    public static \Redis $cacheInstance;

    public function __construct(?int $db = null)
    {
        if ($db) {
            self::$cacheInstance = Laminas::$serviceManager->redisExtend->setDatabase(2)->getRedisInstance();
        }
        self::$cacheInstance = Laminas::$serviceManager->redisExtend->getRedisInstance();
    }

    public function set($key, $value, $ttl = null): bool
    {
        if (is_array($value)) {
            $value = json_encode($value);
        }
        // 90分钟过期时间
        return self::$cacheInstance->set($key, $value, 5400);
    }

    public function get($key, $default = null)
    {
        $value = self::$cacheInstance->get($key);
        return json_decode($value,  true);
    }

    public function delete($key)
    {
        return self::$cacheInstance->get($key);
    }
    public function has($key)
    {
        return self::$cacheInstance->exists($key);
    }

    public function deleteMultiple($keys){}

    public function setMultiple($values, $ttl = null){}

    public function clear(){}

    public function getMultiple($keys, $default = null){}
}