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 Memcached as MemcachedExtension;
24
 
25
use function sha1;
26
 
27
/**
28
 * Class Memcached
29
 *
30
 * @author yuuki.takezawa<yuuki.takezawa@comnect.jp.net>
31
 * @license http://opensource.org/licenses/MIT MIT
32
 */
33
class Memcached extends KeyValueStorage
34
{
35
    /**
36
     * @param MemcachedExtension $memcached
37
     */
38
    public function __construct(
39
        protected MemcachedExtension $memcached,
40
        array $servers
41
    ) {
42
        $this->memcached = $this->connection($memcached, $servers);
43
    }
44
 
45
    /**
46
     * @param MemcachedExtension $memcached
47
     * @param array $servers
48
     *
49
     * @return MemcachedExtension
50
     */
51
    protected function connection(MemcachedExtension $memcached, array $servers): MemcachedExtension
52
    {
53
        foreach ($servers as $server) {
54
            $memcached->addServer($server['host'], $server['port'], $server['weight']);
55
        }
56
        return $memcached;
57
    }
58
 
59
 
60
    /**
61
     * Read values for a set of keys from cache
62
     *
63
     * @param array $keys list of keys to fetch
64
     *
65
     * @return array   list of values with the given keys used as indexes
66
     */
67
    protected function read(array $keys): array
68
    {
69
        $map = $lookup = [];
70
        [$map, $lookup] = $this->eachKeys($keys, $map, $lookup);
71
        $result = [];
72
        $memcachedResult = $this->memcached->getMulti($map);
73
        foreach ($memcachedResult as $k => $v) {
74
            $result[$lookup[$k]] = $v;
75
        }
76
        return $result;
77
    }
78
 
79
    /**
80
     * Save values for a set of keys to cache
81
     *
82
     * @param array $keys list of values to save
83
     * @param int $expire expiration time
84
     *
85
     * @return bool true on success, false on failure
86
     */
87
    protected function write(array $keys, $expire = null): bool
88
    {
89
        foreach ($keys as $k => $v) {
90
            $k = sha1($k);
91
            $this->memcached->set($k, $v, $expire);
92
        }
93
        return true;
94
    }
95
 
96
    /**
97
     * Remove values from cache
98
     *
99
     * @param array $keys list of keys to delete
100
     *
101
     * @return bool true on success, false on failure
102
     */
103
    protected function delete(array $keys): bool
104
    {
105
        foreach ($keys as $k) {
106
            $k = sha1($k);
107
            $this->memcached->delete($k);
108
        }
109
        return true;
110
    }
111
 
112
    /**
113
     * Remove *all* values from cache
114
     *
115
     * @return bool true on success, false on failure
116
     */
117
    protected function purge(): bool
118
    {
119
        return $this->memcached->flush();
120
    }
121
}