CacheHelper.php 2.69 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php

namespace Lu\LuNabamusic\Helpers;

/**
 * Class CacheHelper
 *
 * @author  Lauris Karklis <lauris,karklis@lu.lv>
 * @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']);
    }
}