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

Cache.php « lib - github.com/nextcloud/user_sql.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 622deb7945cfad40732d1a0f947e22b7916b0a92 (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
<?php
/**
 * Nextcloud - user_sql
 *
 * @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
 * @author    Marcin Łojewski <dev@mlojewski.me>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

namespace OCA\UserSQL;

use OC\Memcache\NullCache;
use OCA\UserSQL\Constant\App;
use OCA\UserSQL\Constant\Opt;
use OCP\ICache;
use OCP\IConfig;
use OCP\ILogger;

/**
 * Used to store key-value pairs in the cache memory.
 * If there's no distributed cache available NULL cache is used.
 *
 * @author Marcin Łojewski <dev@mlojewski.me>
 */
class Cache
{
    /**
     * @var ICache The cache instance.
     */
    private $cache;

    /**
     * The default constructor. Initiates the cache memory.
     *
     * @param string  $AppName The application name.
     * @param IConfig $config  The config instance.
     * @param ILogger $logger  The logger instance.
     */
    public function __construct($AppName, IConfig $config, ILogger $logger)
    {
        $factory = \OC::$server->getMemCacheFactory();
        $useCache = $config->getAppValue(
            $AppName, Opt::USE_CACHE, App::FALSE_VALUE
        );

        if ($useCache === App::FALSE_VALUE) {
            $this->cache = new NullCache();
        } elseif ($factory->isAvailable()) {
            $this->cache = $factory->createDistributed();
            $logger->debug("Distributed cache initiated.", ["app" => $AppName]);
        } else {
            $logger->warning(
                "There's no distributed cache available, fallback to null cache.",
                ["app" => $AppName]
            );
            $this->cache = new NullCache();
        }
    }

    /**
     * Fetch a value from the cache memory.
     *
     * @param string $key The cache value key.
     *
     * @return mixed|NULL Cached value or NULL if there's no value stored.
     */
    public function get($key)
    {
        return $this->cache->get($key);
    }

    /**
     * Store a value in the cache memory.
     *
     * @param string $key   The cache value key.
     * @param mixed  $value The value to store.
     * @param int    $ttl   (optional) TTL in seconds. Defaults to 1 hour.
     *
     * @return bool TRUE on success, FALSE otherwise.
     */
    public function set($key, $value, $ttl = 3600)
    {
        return $this->cache->set($key, $value, $ttl);
    }

    /**
     * Clear the cache of all entries.
     *
     * @return bool TRUE on success, FALSE otherwise.
     */
    public function clear()
    {
        return $this->cache->clear();
    }
}