Passed
Push — master ( 4bba79...d3b029 )
by Terry
17:12
created

Wincache::doClear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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