Completed
Push — master ( a1970e...6c5d6a )
by Oleg
04:15
created

XcacheDriver::clean()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 5
nc 3
nop 0
1
<?php /** MicroXcacheDriver */
2
3
namespace Micro\Cache\Drivers;
4
5
use Micro\Base\Exception;
6
7
/**
8
 * Class XcacheDriver
9
 *
10
 * @author Oleg Lunegov <[email protected]>
11
 * @link https://github.com/linpax/microphp-framework
12
 * @copyright Copyright (c) 2013 Oleg Lunegov
13
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
14
 * @package Micro
15
 * @subpackage Cache\Driver
16
 * @version 1.0
17
 * @since 1.0
18
 */
19
class XcacheDriver extends CacheDriver
20
{
21
    /**
22
     * Constructor
23
     *
24
     * @access public
25
     *
26
     * @param array $config config array
27
     *
28
     * @result void
29
     * @throws Exception
30
     */
31
    public function __construct(array $config = [])
32
    {
33
        parent::__construct($config);
34
35
        if (!$this->check()) {
36
            throw new Exception('Extension XCache not installed');
37
        }
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function check()
44
    {
45
        return extension_loaded('xcache') ? true : false;
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function delete($name)
52
    {
53
        return xcache_unset($name);
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public function clean()
60
    {
61
        /** @noinspection PhpUndefinedConstantInspection */
62
        for ($i = 0, $cnt = xcache_count(XC_TYPE_VAR); $i < $cnt; $i++) {
63
            /** @noinspection PhpUndefinedConstantInspection */
64
            if (xcache_clear_cache(XC_TYPE_VAR, $i) === false) {
65
                return false;
66
            }
67
        }
68
69
        return true;
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75
    public function info()
76
    {
77
        /** @noinspection PhpUndefinedConstantInspection */
78
        return xcache_count(XC_TYPE_VAR);
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84
    public function getMeta($id)
85
    {
86
        return false;
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92
    public function increment($name, $offset = 1)
93
    {
94
        $val = $this->get($name) + $offset;
95
96
        return $this->set($name, $val);
97
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102
    public function get($name)
103
    {
104
        return xcache_isset($name) ? xcache_get($name) : false;
105
    }
106
107
    /**
108
     * @inheritdoc
109
     */
110
    public function set($name, $value, $duration = 0)
111
    {
112
        return xcache_set($name, $value, $duration);
113
    }
114
115
    /**
116
     * @inheritdoc
117
     */
118
    public function decrement($name, $offset = 1)
119
    {
120
        $val = $this->get($name) - $offset;
121
122
        return $this->set($name, $val);
123
    }
124
} 
125