Passed
Pull Request — master (#857)
by Georges
01:55
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\Wincache;
18
19
use DateTime;
20
use Phpfastcache\Cluster\AggregatablePoolInterface;
21
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
22
use Phpfastcache\Core\Pool\TaggableCacheItemPoolTrait;
23
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
24
use Phpfastcache\Entities\DriverStatistic;
25
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
26
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
27
28
/**
29
 * @method Config getConfig()
30
 */
31
class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface
32
{
33
    use TaggableCacheItemPoolTrait;
34
35
    /**
36
     * @return bool
37
     */
38
    public function driverCheck(): bool
39
    {
40
        return extension_loaded('wincache') && function_exists('wincache_ucache_set');
41
    }
42
43
    /**
44
     * @return DriverStatistic
45
     */
46
    public function getStats(): DriverStatistic
47
    {
48
        $memInfo = wincache_ucache_meminfo();
49
        $info = wincache_ucache_info();
50
        $date = (new DateTime())->setTimestamp(time() - $info['total_cache_uptime']);
51
52
        return (new DriverStatistic())
53
            ->setInfo(sprintf("The Wincache daemon is up since %s.\n For more information see RawData.", $date->format(DATE_RFC2822)))
54
            ->setSize($memInfo['memory_free'] - $memInfo['memory_total'])
55
            ->setData(implode(', ', array_keys($this->itemInstances)))
56
            ->setRawData($memInfo);
57
    }
58
59
    /**
60
     * @return bool
61
     */
62
    protected function driverConnect(): bool
63
    {
64
        return true;
65
    }
66
67
    /**
68
     * @param ExtendedCacheItemInterface $item
69
     * @return null|array
70
     */
71
    protected function driverRead(ExtendedCacheItemInterface $item): ?array
72
    {
73
        $val = wincache_ucache_get($item->getKey(), $suc);
74
75
        if ($suc === false) {
76
            return null;
77
        }
78
79
        return $val;
80
    }
81
82
    /**
83
     * @param ExtendedCacheItemInterface $item
84
     * @return mixed
85
     * @throws PhpfastcacheInvalidArgumentException
86
     * @throws PhpfastcacheLogicException
87
     */
88
    protected function driverWrite(ExtendedCacheItemInterface $item): bool
89
    {
90
        $this->assertCacheItemType($item, Item::class);
91
92
        return wincache_ucache_set($item->getKey(), $this->driverPreWrap($item), $item->getTtl());
93
    }
94
95
    /**
96
     * @param ExtendedCacheItemInterface $item
97
     * @return bool
98
     * @throws PhpfastcacheInvalidArgumentException
99
     */
100
    protected function driverDelete(ExtendedCacheItemInterface $item): bool
101
    {
102
        $this->assertCacheItemType($item, Item::class);
103
104
        return wincache_ucache_delete($item->getKey());
105
    }
106
107
    /**
108
     * @return bool
109
     */
110
    protected function driverClear(): bool
111
    {
112
        return wincache_ucache_clear();
113
    }
114
}
115