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 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
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 and LICENCE files.
10
 *
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 * @author Contributors  https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phpfastcache\Drivers\Apcu;
18
19
use DateTime;
20
use Phpfastcache\Cluster\AggregatablePoolInterface;
21
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
22
use Phpfastcache\Core\Pool\TaggableCacheItemPoolTrait;
23
use Phpfastcache\Config\ConfigurationOption;
24
use Phpfastcache\Config\ConfigurationOptionInterface;
25
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
26
use Phpfastcache\Entities\DriverStatistic;
27
use Phpfastcache\Util\SapiDetector;
28
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
29
30
/**
31
 * Class Driver
32
 * @method Config getConfig()
33
 */
34
class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface
35
{
36
    use TaggableCacheItemPoolTrait;
37
38
    /**
39
     * @return bool
40
     */
41
    public function driverCheck(): bool
42
    {
43
        return extension_loaded('apcu') && ((ini_get('apc.enabled') && SapiDetector::isWebScript()) || (ini_get('apc.enable_cli') && SapiDetector::isCliScript()));
44
    }
45
46
    /**
47
     * @return DriverStatistic
48
     */
49
    public function getStats(): DriverStatistic
50
    {
51
        $stats = (array)apcu_cache_info();
52
        $date = (new DateTime())->setTimestamp($stats['start_time']);
53
54
        return (new DriverStatistic())
55
            ->setData(implode(', ', array_keys($this->itemInstances)))
56
            ->setInfo(
57
                sprintf(
58
                    "The APCU cache is up since %s, and have %d item(s) in cache.\n For more information see RawData.",
59
                    $date->format(DATE_RFC2822),
60
                    $stats['num_entries']
61
                )
62
            )
63
            ->setRawData($stats)
64
            ->setSize((int)$stats['mem_size']);
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 bool
78
     * @throws PhpfastcacheInvalidArgumentException
79
     */
80
    protected function driverWrite(ExtendedCacheItemInterface $item): bool
81
    {
82
        $this->assertCacheItemType($item, Item::class);
83
84
        return (bool)apcu_store($item->getKey(), $this->driverPreWrap($item), $item->getTtl());
85
    }
86
87
    /**
88
     * @param ExtendedCacheItemInterface $item
89
     * @return null|array
90
     */
91
    protected function driverRead(ExtendedCacheItemInterface $item): ?array
92
    {
93
        $data = apcu_fetch($item->getKey(), $success);
94
95
        if ($success === false) {
96
            return null;
97
        }
98
99
        return $data;
100
    }
101
102
    /**
103
     * @param ExtendedCacheItemInterface $item
104
     * @return bool
105
     * @throws PhpfastcacheInvalidArgumentException
106
     */
107
    protected function driverDelete(ExtendedCacheItemInterface $item): bool
108
    {
109
        $this->assertCacheItemType($item, Item::class);
110
111
        return (bool)apcu_delete($item->getKey());
112
    }
113
114
    /**
115
     * @return bool
116
     */
117
    protected function driverClear(): bool
118
    {
119
        return @apcu_clear_cache();
120
    }
121
}
122