HEX
Server: Apache/2.4.57 (Ubuntu) mod_fcgid/2.3.9 OpenSSL/3.0.2
System: Linux vmi267337.contaboserver.net 5.15.0-25-generic #25-Ubuntu SMP Wed Mar 30 15:54:22 UTC 2022 x86_64
User: ohirex (1008)
PHP: 8.2.8
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,exec,system,passthru,shell_exec
Upload Files
File: /home/ohirex/web/ohirex.com/public_html/join/db/Classes/CacheHandler.php
<?php


namespace SleekDB\Classes;


use SleekDB\Cache;
use SleekDB\Exceptions\IOException;
use SleekDB\QueryBuilder;

/**
 * Class CacheHandler
 * Bridge between Query and Cache
 */
class CacheHandler
{
  /**
   * @var Cache
   */
  protected $cache;

  protected $cacheTokenArray;
  protected $regenerateCache;
  protected $useCache;

  /**
   * CacheHandler constructor.
   * @param string $storePath
   * @param QueryBuilder $queryBuilder
   */
  public function __construct(string $storePath, QueryBuilder $queryBuilder)
  {
    $this->cacheTokenArray = $queryBuilder->_getCacheTokenArray();

    $queryBuilderProperties = $queryBuilder->_getConditionProperties();
    $this->useCache = $queryBuilderProperties["useCache"];
    $this->regenerateCache = $queryBuilderProperties["regenerateCache"];

    $this->cache = new Cache($storePath, $this->_getCacheTokenArray(), $queryBuilderProperties["cacheLifetime"]);
  }

  /**
   * @return Cache
   */
  public function getCache(): Cache
  {
    return $this->cache;
  }

  /**
   * Get results from cache
   * @return array|null
   * @throws IOException
   */
  public function getCacheContent($getOneDocument)
  {
    if($this->getUseCache() !== true){
      return null;
    }

    $this->updateCacheTokenArray(['oneDocument' => $getOneDocument]);

    if($this->regenerateCache === true) {
      $this->getCache()->delete();
    }

    $cacheResults = $this->getCache()->get();
    if(is_array($cacheResults)) {
      return $cacheResults;
    }

    return null;
  }

  /**
   * Add content to cache
   * @param array $results
   * @throws IOException
   */
  public function setCacheContent(array $results)
  {
    if($this->getUseCache() === true){
      $this->getCache()->set($results);
    }
  }

  /**
   * Delete all cache files that have no lifetime.
   * @return bool
   */
  public function deleteAllWithNoLifetime(): bool
  {
    return $this->getCache()->deleteAllWithNoLifetime();
  }

  /**
   * Returns a reference to the array used for cache token generation
   * @return array
   */
  public function &_getCacheTokenArray(): array
  {
    return $this->cacheTokenArray;
  }

  /**
   * @param array $tokenUpdate
   */
  private function updateCacheTokenArray(array $tokenUpdate)
  {
    if(empty($tokenUpdate)) {
      return;
    }
    $cacheTokenArray = $this->_getCacheTokenArray();
    foreach ($tokenUpdate as $key => $value){
      $cacheTokenArray[$key] = $value;
    }
    $this->cacheTokenArray = $cacheTokenArray;
  }

  /**
   * Status if cache is used or not
   * @return bool
   */
  private function getUseCache(): bool
  {
    return $this->useCache;
  }

}