Driver   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 23
dl 0
loc 86
rs 10
c 1
b 1
f 0
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A driverConnect() 0 3 1
A getHelp() 0 3 1
A driverCheck() 0 3 2
A getStats() 0 8 1
A driverRead() 0 9 3
A driverClear() 0 3 1
A driverDelete() 0 3 1
A driverWrite() 0 6 2
1
<?php
2
3
/**
4
 *
5
 * This file is part of Phpfastcache.
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For full copyright and license information, please see the docs/CREDITS.txt file.
10
 *
11
 * @author Lucas Brucksch <[email protected]>
12
 *
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phpfastcache\Drivers\Zendshm;
18
19
use Phpfastcache\Cluster\AggregatablePoolInterface;
20
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
21
use Phpfastcache\Core\Pool\TaggableCacheItemPoolTrait;
22
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
23
use Phpfastcache\Entities\DriverStatistic;
24
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
25
26
/**
27
 * Requires Zend Data Cache Functions from ZendServer
28
 * @method Config getConfig()
29
 */
30
class Driver implements AggregatablePoolInterface
31
{
32
    use TaggableCacheItemPoolTrait;
33
34
    /**
35
     * @return bool
36
     */
37
    public function driverCheck(): bool
38
    {
39
        return extension_loaded('Zend Data Cache') && function_exists('zend_shm_cache_store');
40
    }
41
42
    /**
43
     * @return string
44
     */
45
    public function getHelp(): string
46
    {
47
        return <<<HELP
48
<p>
49
This driver rely on Zend Server 8.5+, see: https://www.zend.com/products/zend-server
50
</p>
51
HELP;
52
    }
53
54
    /**
55
     * @return DriverStatistic
56
     */
57
    public function getStats(): DriverStatistic
58
    {
59
        $stats = (array)zend_shm_cache_info();
60
        return (new DriverStatistic())
61
            ->setData(implode(', ', array_keys($this->itemInstances)))
62
            ->setInfo(sprintf("The Zend memory have %d item(s) in cache.\n For more information see RawData.", $stats['items_total']))
63
            ->setRawData($stats)
64
            ->setSize($stats['memory_total']);
65
    }
66
67
    /**
68
     * @return bool
69
     */
70
    protected function driverConnect(): bool
71
    {
72
        return true;
73
    }
74
75
    /**
76
     * @param ExtendedCacheItemInterface $item
77
     * @return ?array<string, mixed>
78
     */
79
    protected function driverRead(ExtendedCacheItemInterface $item): ?array
80
    {
81
        $data = zend_shm_cache_fetch($item->getKey());
82
83
        if (empty($data) || !\is_array($data)) {
84
            return null;
85
        }
86
87
        return $data;
88
    }
89
90
    /**
91
     * @param ExtendedCacheItemInterface $item
92
     * @return mixed
93
     * @throws PhpfastcacheInvalidArgumentException
94
     */
95
    protected function driverWrite(ExtendedCacheItemInterface $item): bool
96
    {
97
98
        $ttl = $item->getExpirationDate()->getTimestamp() - time();
99
100
        return zend_shm_cache_store($item->getKey(), $this->driverPreWrap($item), ($ttl > 0 ? $ttl : 0));
101
    }
102
103
    /**
104
     * @param string $key
105
     * @param string $encodedKey
106
     * @return bool
107
     */
108
    protected function driverDelete(string $key, string $encodedKey): bool
109
    {
110
        return (bool)zend_shm_cache_delete($key);
111
    }
112
113
    protected function driverClear(): bool
114
    {
115
        return @zend_shm_cache_clear();
116
    }
117
}
118