1<?php
2
3declare(strict_types=1);
4
5namespace voku\cache;
6
7/**
8 * AdapterXcache: Xcache-adapter
9 */
10class AdapterXcache implements iAdapter
11{
12    public $installed = false;
13
14    /**
15     * __construct
16     */
17    public function __construct()
18    {
19        if (\extension_loaded('xcache') === true) {
20            $this->installed = true;
21        }
22    }
23
24    /**
25     * {@inheritdoc}
26     */
27    public function exists(string $key): bool
28    {
29        return \xcache_isset($key);
30    }
31
32    /**
33     * {@inheritdoc}
34     */
35    public function get(string $key)
36    {
37        return \xcache_get($key);
38    }
39
40    /**
41     * {@inheritdoc}
42     */
43    public function installed(): bool
44    {
45        return $this->installed;
46    }
47
48    /**
49     * {@inheritdoc}
50     */
51    public function remove(string $key): bool
52    {
53        return \xcache_unset($key);
54    }
55
56    /**
57     * {@inheritdoc}
58     */
59    public function removeAll(): bool
60    {
61        if (\defined('XC_TYPE_VAR')) {
62            $xCacheCount = xcache_count(XC_TYPE_VAR);
63            for ($i = 0; $i < $xCacheCount; $i++) {
64                \xcache_clear_cache(XC_TYPE_VAR, $i);
65            }
66
67            return true;
68        }
69
70        return false;
71    }
72
73    /**
74     * {@inheritdoc}
75     */
76    public function set(string $key, $value): bool
77    {
78        return \xcache_set($key, $value);
79    }
80
81    /**
82     * {@inheritdoc}
83     */
84    public function setExpired(string $key, $value, int $ttl = 0): bool
85    {
86        return \xcache_set($key, $value, $ttl);
87    }
88}
89