summaryrefslogtreecommitdiffstats
path: root/library/Director/Db/Cache/PrefetchCache.php
blob: aa9f950801046fd06dda1e79d64273ec0e5725cf (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php

namespace Icinga\Module\Director\Db\Cache;

use Icinga\Module\Director\CustomVariable\CustomVariable;
use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Resolver\HostServiceBlacklist;
use Icinga\Module\Director\Resolver\TemplateTree;
use LogicException;

/**
 * Central prefetch cache
 *
 * Might be improved, accept various caches based on an interface and then
 * finally replace prefetch logic in DbObject itself. This would also allow
 * to get rid of IcingaObject-related code in this place
 */
class PrefetchCache
{
    protected $db;

    protected static $instance;

    protected $varsCaches = array();

    protected $groupsCaches = array();

    protected $templateResolvers = array();

    protected $renderedVars = array();

    protected $templateTrees = array();

    protected $hostServiceBlacklist;

    public static function initialize(Db $db)
    {
        self::forget();
        self::$instance = new static($db);
    }

    protected function __construct(Db $db)
    {
        $this->db = $db;
    }

    /**
     * @throws LogicException
     *
     * @return self
     */
    public static function instance()
    {
        if (static::$instance === null) {
            throw new LogicException('Prefetch cache has not been loaded');
        }

        return static::$instance;
    }

    public static function forget()
    {
        DbObject::clearAllPrefetchCaches();
        self::$instance = null;
    }

    public static function shouldBeUsed()
    {
        return self::$instance !== null;
    }

    public function vars(IcingaObject $object)
    {
        return $this->varsCache($object)->getVarsForObject($object);
    }

    public function groups(IcingaObject $object)
    {
        return $this->groupsCache($object)->getGroupsForObject($object);
    }

    /* Hint: not implemented, this happens in DbObject right now
    public function byObjectType($type)
    {
        if (! array_key_exists($type, $this->caches)) {
            $this->caches[$type] = new ObjectCache($type);
        }

        return $this->caches[$type];
    }
    */

    public function renderVar(CustomVariable $var, $renderExpressions = false)
    {
        $checksum = $var->getChecksum();
        if (null === $checksum) {
            return $var->toConfigString($renderExpressions);
        } else {
            $checksum .= (int) $renderExpressions;
            if (! array_key_exists($checksum, $this->renderedVars)) {
                $this->renderedVars[$checksum] = $var->toConfigString($renderExpressions);
            }

            return $this->renderedVars[$checksum];
        }
    }

    public function hostServiceBlacklist()
    {
        if ($this->hostServiceBlacklist === null) {
            $this->hostServiceBlacklist = new HostServiceBlacklist($this->db);
            $this->hostServiceBlacklist->preloadMappings();
        }

        return $this->hostServiceBlacklist;
    }

    /**
     * @param IcingaObject $object
     * @return CustomVariableCache
     */
    protected function varsCache(IcingaObject $object)
    {
        $key = $object->getShortTableName();

        if (! array_key_exists($key, $this->varsCaches)) {
            $this->varsCaches[$key] = new CustomVariableCache($object);
        }

        return $this->varsCaches[$key];
    }

    protected function groupsCache(IcingaObject $object)
    {
        $key = $object->getShortTableName();

        if (! array_key_exists($key, $this->groupsCaches)) {
            $this->groupsCaches[$key] = new GroupMembershipCache($object);
        }

        return $this->groupsCaches[$key];
    }

    protected function templateTree(IcingaObject $object)
    {
        $key = $object->getShortTableName();
        if (! array_key_exists($key, $this->templateTrees)) {
            $this->templateTrees[$key] = new TemplateTree(
                $key,
                $object->getConnection()
            );
        }

        return $this->templateTrees[$key];
    }

    public function __destruct()
    {
        unset($this->groupsCaches);
        unset($this->varsCaches);
        unset($this->templateResolvers);
        unset($this->renderedVars);
    }
}