Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Cache.php « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3036d050061d9d9a4f6a12f0caac388decc50029 (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
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */
namespace Piwik;

use Piwik\Cache\Backend;
use Piwik\Container\StaticContainer;

class Cache
{

    /**
     * This can be considered as the default cache to use in case you don't know which one to pick. It does not support
     * the caching of any objects though. Only boolean, numbers, strings and arrays are supported. Whenever you request
     * an entry from the cache it will fetch the entry. Cache entries might be persisted but not necessarily. It
     * depends on the configured backend.
     *
     * @return Cache\Lazy
     */
    public static function getLazyCache()
    {
        return StaticContainer::get('Piwik\Cache\Lazy');
    }

    /**
     * This class is used to cache any data during one request. It won't be persisted between requests and it can
     * cache all kind of data, even objects or resources. This cache is very fast.
     *
     * @return Cache\Transient
     */
    public static function getTransientCache()
    {
        return StaticContainer::get('Piwik\Cache\Transient');
    }

    /**
     * This cache stores all its cache entries under one "cache" entry in a configurable backend.
     *
     * This comes handy for things that you need very often, nearly in every request. For example plugin metadata, the
     * list of tracker plugins, the list of available languages, ...
     * Instead of having to read eg. a hundred cache entries from files (or any other backend) it only loads one cache
     * entry which contains the hundred keys. Should be used only for things that you need very often and only for
     * cache entries that are not too large to keep loading and parsing the single cache entry fast.
     * All cache entries it contains have the same life time. For fast performance it won't validate any cache ids.
     * It is not possible to cache any objects using this cache.
     *
     * @return Cache\Eager
     */
    public static function getEagerCache()
    {
        return StaticContainer::get('Piwik\Cache\Eager');
    }

    public static function flushAll()
    {
        self::getLazyCache()->flushAll();
        self::getTransientCache()->flushAll();
        self::getEagerCache()->flushAll();
    }

    /**
     * @param $type
     * @return Cache\Backend
     */
    public static function buildBackend($type)
    {
        $factory = new Cache\Backend\Factory();
        $options = self::getOptions($type);

        $backend = $factory->buildBackend($type, $options);

        return $backend;
    }

    private static function getOptions($type)
    {
        $options = self::getBackendOptions($type);

        switch ($type) {
            case 'file':

                $options = array('directory' => StaticContainer::get('path.cache'));
                break;

            case 'chained':

                foreach ($options['backends'] as $backend) {
                    $options[$backend] = self::getOptions($backend);
                }

                break;

            case 'redis':

                if (!empty($options['timeout'])) {
                    $options['timeout'] = (float)Common::forceDotAsSeparatorForDecimalPoint($options['timeout']);
                }

                break;
        }

        return $options;
    }

    private static function getBackendOptions($backend)
    {
        $key = ucfirst($backend) . 'Cache';
        $options = Config::getInstance()->$key;

        return $options;
    }
}