Completed
Push — master ( 3d22df...95226d )
by Georges
22s queued 12s
created

Driver::driverCheck()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
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
declare(strict_types=1);
15
16
namespace Phpfastcache\Drivers\Wincache;
17
18
use DateTime;
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
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
26
27
/**
28
 * @property Config $config Return the config object
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('wincache') && function_exists('wincache_ucache_set');
40
    }
41
42
    /**
43
     * @return DriverStatistic
44
     */
45
    public function getStats(): DriverStatistic
46
    {
47
        $memInfo = wincache_ucache_meminfo();
48
        $info = wincache_ucache_info();
49
        $date = (new DateTime())->setTimestamp(time() - $info['total_cache_uptime']);
50
51
        return (new DriverStatistic())
52
            ->setInfo(sprintf("The Wincache daemon is up since %s.\n For more information see RawData.", $date->format(DATE_RFC2822)))
53
            ->setSize($memInfo['memory_free'] - $memInfo['memory_total'])
54
            ->setData(implode(', ', array_keys($this->itemInstances)))
55
            ->setRawData($memInfo);
56
    }
57
58
    /**
59
     * @return bool
60
     */
61
    protected function driverConnect(): bool
62
    {
63
        return true;
64
    }
65
66
    /**
67
     * @param ExtendedCacheItemInterface $item
68
     * @return null|array
69
     */
70
    protected function driverRead(ExtendedCacheItemInterface $item): ?array
71
    {
72
        $val = wincache_ucache_get($item->getKey(), $suc);
73
74
        if ($suc === false) {
75
            return null;
76
        }
77
78
        return $val;
79
    }
80
81
    /**
82
     * @param ExtendedCacheItemInterface $item
83
     * @return mixed
84
     * @throws PhpfastcacheInvalidArgumentException
85
     * @throws PhpfastcacheLogicException
86
     */
87
    protected function driverWrite(ExtendedCacheItemInterface $item): bool
88
    {
89
        $this->assertCacheItemType($item, Item::class);
90
91
        return wincache_ucache_set($item->getKey(), $this->driverPreWrap($item), $item->getTtl());
92
    }
93
94
    /**
95
     * @param ExtendedCacheItemInterface $item
96
     * @return bool
97
     * @throws PhpfastcacheInvalidArgumentException
98
     */
99
    protected function driverDelete(ExtendedCacheItemInterface $item): bool
100
    {
101
        $this->assertCacheItemType($item, Item::class);
102
103
        return wincache_ucache_delete($item->getKey());
104
    }
105
106
    /**
107
     * @return bool
108
     */
109
    protected function driverClear(): bool
110
    {
111
        return wincache_ucache_clear();
112
    }
113
114
    public function getConfig(): Config
115
    {
116
        return $this->config;
117
    }
118
}
119