* @owner University of Latvia * @since 14.07.2021 * * @package Lu\LuNabamusic\Helpers */ class CacheHelper { /** * Table name * * @var string */ private $tableName = 'tx_lunabamusic_cache'; /** * DB configuration * * @var mixed */ private $config; /** * PDO DB connector * * @var \PDO */ private \PDO $db; /** * CacheHelper constructor. */ public function __construct() { $this->config = require '/home/portal/typo3/typo3conf/LocalConfiguration.php'; $this->db = new \PDO( 'mysql:host='.$this->config['DB']['Connections']['Default']['host'].';dbname='.$this->config['DB']['Connections']['Default']['dbname'], $this->config['DB']['Connections']['Default']['user'], $this->config['DB']['Connections']['Default']['password'] ); } /** * Set cache * * @param $identifier * @param $content */ public function set($identifier, $content) { // Read identifier $query = $this->db->prepare("SELECT * FROM ".$this->tableName." WHERE identifier = :identifier"); $query->bindParam('identifier', $identifier, $this->db::PARAM_STR); $query->execute(); if ($query->rowCount() == 0) { // Make new entry $query = $this->db->prepare("INSERT INTO ".$this->tableName." (crdate, identifier, content) VALUES (?, ?, ?)"); $query->execute([time(), $identifier, serialize($content)]); } else { // Update exiusting entry $query = $this->db->prepare("UPDATE ".$this->tableName." SET crdate = :crdate, content = :content WHERE identifier = :identifier"); $content = serialize($content); $time = time(); $query->bindParam('crdate', $time); $query->bindParam('content', $content); $query->bindParam('identifier', $identifier); $query->execute(); } } /** * Get cache * * @param $identifier * * @return false|mixed */ public function get($identifier) { // Read identifier $query = $this->db->prepare("SELECT * FROM ".$this->tableName." WHERE identifier = :identifier"); $query->bindParam('identifier', $identifier, $this->db::PARAM_STR); $query->execute(); // Return false ig nothing found if ($query->rowCount() == 0) { return false; } // Return cached data return unserialize($query->fetch()['content']); } }