Wincache::doGet()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
1
<?php
2
/*
3
 * This file is part of the Shieldon Simple Cache package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Shieldon\SimpleCache\Driver;
14
15
use Shieldon\SimpleCache\CacheProvider;
16
use Shieldon\SimpleCache\Exception\CacheException;
17
use function unserialize;
18
use function serialize;
19
20
/**
21
 * Provided by WinCache
22
 */
23
use function wincache_ucache_clear;
24
use function wincache_ucache_delete;
25
use function wincache_ucache_exists;
26
use function wincache_ucache_get;
27
use function wincache_ucache_info;
28
use function wincache_ucache_set;
29
30
/**
31
 * A cache driver class provided by WinCache (Windows Cache for PHP)
32
 *
33
 * Note: This class is excluded from the unit tests since it is only used on
34
 * Windows server. And all our tests are run on Linux system.
35
 *
36
 * @see https://www.php.net/manual/en/book.wincache.php
37
 */
38
class Wincache extends CacheProvider
39
{
40
    protected $type = 'wincache';
41
42
    /**
43
     * Constructor.
44
     *
45
     * @param array $setting The settings.
46
     *
47
     * @throws CacheException
48
     */
49
    public function __construct(array $setting = [])
50
    {
51
        if (!function_exists('wincache_ucache_get')) {
52
            throw new CacheException(
53
                'WinCache extension is not enable.'
54
            );
55
        }
56
57
        unset($setting);
58
    }
59
60
    /**
61
     * Fetch a cache by an extended Cache Driver.
62
     *
63
     * @param string $key The key of a cache.
64
     *
65
     * @return array
66
     */
67
    protected function doGet(string $key): array
68
    {
69
        $success = false;
70
        $content = wincache_ucache_get($key, $success);
71
72
        if (empty($content) || !$success) {
73
            return [];
74
        }
75
        $data = unserialize($content);
76
77
        return $data;
78
    }
79
80
    /**
81
     * Set a cache by an extended Cache Driver.
82
     *
83
     * @param string $key       The key of a cache.
84
     * @param mixed  $value     The value of a cache. (serialized)
85
     * @param int    $ttl       The time to live for a cache.
86
     * @param int    $timestamp The time to store a cache.
87
     *
88
     * @return bool
89
     */
90
    protected function doSet(string $key, $value, int $ttl, int $timestamp): bool
91
    {
92
        $contents = [
93
            'timestamp' => $timestamp,
94
            'ttl'       => $ttl,
95
            'value'     => $value,
96
        ];
97
98
        $result = wincache_ucache_set(
99
            $key,
100
            serialize($contents),
101
            $ttl
102
        );
103
104
        return $result;
105
    }
106
107
    /**
108
     * Delete a cache by an extended Cache Driver.
109
     *
110
     * @param string $key The key of a cache.
111
     *
112
     * @return bool
113
     */
114
    protected function doDelete(string $key): bool
115
    {
116
        return wincache_ucache_delete($key);
117
    }
118
119
    /**
120
     * Delete all caches by an extended Cache Driver.
121
     *
122
     * @return bool
123
     */
124
    protected function doClear(): bool
125
    {
126
        return wincache_ucache_clear();
127
    }
128
129
    /**
130
     * Check if the cache exists or not.
131
     *
132
     * @param string $key The key of a cache.
133
     *
134
     * @return bool
135
     */
136
    protected function doHas(string $key): bool
137
    {
138
        return wincache_ucache_exists($key);
139
    }
140
141
    /**
142
     * Fetch all cache items.
143
     *
144
     * @return array
145
     */
146
    protected function getAll(): array
147
    {
148
        $list = [];
149
        $info = wincache_ucache_info();
150
151
        foreach ($info['ucache_entries'] as $item) {
152
            $key = $item['key_name'];
153
            $value = $this->doGet($key);
154
155
            $list[$key] = $value;
156
        }
157
        return $list;
158
    }
159
}
160