aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/StudipCacheWrapper.php
blob: 65c5edb22326375d34d5150e9a75208d1e122378 (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
<?php

/**
 * The cache wrapper wraps a memory cache around another cache. This should
 * reduce the accesses to the actual cache.
 *
 * @author Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @license GPL2 or any later version
 * @since Stud.IP 5.4
 */
class StudipCacheWrapper implements StudipCache
{
    const DEFAULT_MEMORY_EXPIRATION = 60;

    protected $actual_cache;
    protected $memory_cache;

    public function __construct(StudipCache $actual_cache)
    {
        $this->actual_cache = $actual_cache;
        $this->memory_cache = new StudipMemoryCache();
    }

    /**
     * @inheritdoc
     */
    public function expire($arg)
    {
        $this->memory_cache->expire($arg);
        $this->actual_cache->expire($arg);
    }

    /**
     * @inheritdoc
     */
    public function flush()
    {
        $this->memory_cache->flush();
        $this->actual_cache->flush();
    }

    /**
     * @inheritdoc
     */
    public function read($arg)
    {
        $cached = $this->memory_cache->read($arg);
        if ($cached !== false) {
            return $cached;
        }

        $cached = $this->actual_cache->read($arg);
        if ($cached !== false) {
            $this->memory_cache->write($arg, $cached, self::DEFAULT_MEMORY_EXPIRATION);
        }
        return $cached;
    }

    /**
     * @inheritdoc
     */
    public function write($name, $content, $expires = self::DEFAULT_EXPIRATION)
    {
        $this->memory_cache->expire($name);
        $this->actual_cache->write($name, $content, $expires);
    }
}