Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/**
4
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10
 * THE SOFTWARE.
11
 *
12
 * This software consists of voluntary contributions made by many individuals
13
 * and is licensed under the MIT license.
14
 *
15
 * Copyright (c) 2014-2022 Yuuki Takezawa
16
 *
17
 */
18
 
19
declare(strict_types=1);
20
 
21
namespace Ytake\LaravelSmarty\Cache;
22
 
23
use Predis\Client;
24
 
25
use function count;
26
use function sha1;
27
 
28
/**
29
 * Class Redis
30
 *
31
 * @author yuuki.takezawa<yuuki.takezawa@comnect.jp.net>
32
 * @license http://opensource.org/licenses/MIT MIT
33
 */
34
class Redis extends KeyValueStorage
35
{
36
    /** @var Client */
37
    protected Client $redis;
38
 
39
    /**
40
     * @param array $servers
41
     */
42
    public function __construct(array $servers)
43
    {
44
        if (count($servers) === 1) {
45
            $this->redis = new Client($servers[0]);
46
        } else {
47
            $this->redis = new Client($servers);
48
        }
49
    }
50
 
51
    /**
52
     * Read values for a set of keys from cache
53
     *
54
     * @param array $keys list of keys to fetch
55
     *
56
     * @return array   list of values with the given keys used as indexes
57
     */
58
    protected function read(array $keys): array
59
    {
60
        $map = $lookup = [];
61
        list($map, $lookup) = $this->eachKeys($keys, $map, $lookup);
62
        $result = [];
63
        foreach ($map as $key) {
64
            $result[$lookup[$key]] = $this->redis->get($key);
65
        }
66
        return $result;
67
    }
68
 
69
    /**
70
     * Save values for a set of keys to cache
71
     *
72
     * @param array $keys list of values to save
73
     * @param int $expire expiration time
74
     *
75
     * @return bool true on success, false on failure
76
     */
77
    protected function write(array $keys, $expire = 1): bool
78
    {
79
        foreach ($keys as $k => $v) {
80
            $k = sha1($k);
81
            $this->redis->setex($k, $expire, $v);
82
        }
83
        return true;
84
    }
85
 
86
    /**
87
     * Remove values from cache
88
     *
89
     * @param array $keys list of keys to delete
90
     *
91
     * @return bool true on success, false on failure
92
     */
93
    protected function delete(array $keys): bool
94
    {
95
        foreach ($keys as $k) {
96
            $k = sha1($k);
97
            $this->redis->del($k);
98
        }
99
        return true;
100
    }
101
 
102
    /**
103
     * Remove *all* values from cache
104
     *
105
     * @return bool true on success, false on failure
106
     */
107
    protected function purge(): bool
108
    {
109
        $this->redis->flushdb();
110
        return true;
111
    }
112
}