<?php

namespace Helpers\Cache;

trait APCu
{
    
    public function __construct()
    {
    }

     /**
     * surchargeGetUcache
     *
     * @param  string $key
     * @return array<mixed>
     */
    public static function surchargeGetUcache(string $key) : array
    {
        if (\Helpers\Cache\Cache::variableTest() == true) {
            return [];
        } else {
            $data = \apcu_fetch($key);

            if ($data === false) {
                return [];
            } else {
                return $data;
            }
        }
    }
        
   
    /**
     * surchargeSetUcache
     *
     * @param  string $key
     * @param  mixed $value
     * @param  mixed $ttl
     * @return bool
     */
    public static function surchargeSetUcache(string $key, $value, $ttl = 0) : bool
    {
        if (\Helpers\Cache\Cache::variableTest() == true) {
            return true;
        } else {
            if (self::surchargeUcacheExists($key)) {
                return self::surchargeSetUcacheUpdate($key, $value, $ttl);
            } else {
                return self::surchargeSetUcacheNew($key, $value, $ttl);
            }
        }
    }

    public static function surchargeSetUcacheNew(string $key, mixed $value, int $ttl = 0) : bool
    {
        return \apcu_store($key, $value, $ttl);
    }

    public static function surchargeSetUcacheUpdate(string $key, mixed $value, int $ttl = 0) : bool
    {
         return \apcu_store($key, $value, $ttl);
    }
    
    /**
     * surchargeUcacheExists
     *
     * @param  string $key
     * @return bool
     */
    public static function surchargeUcacheExists(string $key) : bool
    {
        if (\Helpers\Cache\Cache::variableTest() == true) {
            return false;
        } else {
            return \apcu_exists($key);
        }
    }
    
    /**
     * surchargeDeleteUcache
     *
     * @param  string $key
     * @return bool
     */
    public static function surchargeDeleteUcache(string $key) : bool
    {
        if (\Helpers\Cache\Cache::variableTest() == true) {
            return false;
        } else {
            return \apcu_delete($key);
        }
    }
    /**
     * surchargeDeleteUcache
     *
     * @return bool
     */
    public static function surchargeClearUcache() : bool
    {
        return \apcu_clear_cache();
    }
}
