aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/cache/Proxy.php
blob: fef034d2bb59c05ea90de2add61b832180638ffb (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php

namespace Studip\Cache;

use Psr\Cache\CacheItemInterface;
use StudipCacheOperation;

/**
 * Proxies a StudipCache and stores the expire operation in the database.
 * These operations are lateron applied to the cache they should have
 * been applied to in the beginning.
 *
 * @author  Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @license GPL2 or any later version
 * @since   Stud.IP 3.3
 */
class Proxy extends Cache
{
    protected Cache $actual_cache;
    protected array $proxy_these;

    /**
     * @param Cache $cache       The actual cache object
     * @param mixed $proxy_these List of operations to proxy (should be an
     *                           array but a space seperated string is also
     *                           valid)
     */
    public function __construct(Cache $cache, $proxy_these = ['expire'])
    {
        if (!is_array($proxy_these)) {
            $proxy_these = words($proxy_these);
        }

        $this->actual_cache = $cache;
        $this->proxy_these  = is_array($proxy_these)
                            ? $proxy_these
                            : words($proxy_these);
    }

    /**
     * Expires just a single key.
     *
     * @param string $arg The item's key
     */
    public function expire($arg)
    {
        if (in_array('expire', $this->proxy_these)) {
            try {
                $operation = new StudipCacheOperation([$arg, 'expire']);
                $operation->parameters = serialize([]);
                $operation->store();
            } catch (\Exception) {
            }
        }

        return $this->actual_cache->expire($arg);
    }

    /**
     * Expire all items from the cache.
     */
    public function flush()
    {
        if (in_array('flush', $this->proxy_these)) {
            try {
                $operation = new StudipCacheOperation(['', 'flush']);
                $operation->parameters = serialize([]);
                $operation->store();
            } catch (\Exception) {
            }
        }

        return $this->actual_cache->flush();
    }

    public static function getDisplayName(): string
    {
        return static::class;
    }

    public function getStats(): array
    {
        return $this->actual_cache->getStats();
    }

    public static function getConfig(): array
    {
        return [];
    }

    /**
     * @inheritDoc
     */
    public function getItem(string $key): CacheItemInterface
    {
        return $this->actual_cache->getItem($key);
    }

    /**
     * @inheritDoc
     */
    public function hasItem(string $key): bool
    {
        return $this->actual_cache->hasItem($key);
    }

    /**
     * @inheritDoc
     */
    public function save(CacheItemInterface $item): bool
    {
        if (in_array('save', $this->proxy_these)) {
            try {
                $operation = new StudipCacheOperation([$item->getKey(), 'save']);
                $operation->parameters = serialize([$item]);
                $operation->store();
            } catch (\Exception) {
            }
        }

        return $this->actual_cache->save($item);
    }
}