Passed
Pull Request — master (#857)
by Georges
04:15 queued 02:15
created

Driver::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 1
f 0
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 ExtendedCacheItemPoolInterface, 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/en/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 null|array
78
     */
79
    protected function driverRead(ExtendedCacheItemInterface $item): ?array
80
    {
81
        $data = zend_shm_cache_fetch($item->getKey());
82
        if ($data === false) {
83
            return null;
84
        }
85
86
        return $data;
87
    }
88
89
    /**
90
     * @param ExtendedCacheItemInterface $item
91
     * @return mixed
92
     * @throws PhpfastcacheInvalidArgumentException
93
     */
94
    protected function driverWrite(ExtendedCacheItemInterface $item): bool
95
    {
96
        $this->assertCacheItemType($item, Item::class);
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 ExtendedCacheItemInterface $item
105
     * @return bool
106
     * @throws PhpfastcacheInvalidArgumentException
107
     */
108
    protected function driverDelete(ExtendedCacheItemInterface $item): bool
109
    {
110
        $this->assertCacheItemType($item, Item::class);
111
112
        return (bool)zend_shm_cache_delete($item->getKey());
113
    }
114
115
    protected function driverClear(): bool
116
    {
117
        return @zend_shm_cache_clear();
118
    }
119
}
120