Completed
Branch feature/pre-split (60f5c0)
by Anton
03:19
created

MemcacheStore::__construct()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 10
nop 4
dl 0
loc 33
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Cache\Stores;
10
11
use Spiral\Cache\Exceptions\StoreException;
12
use Spiral\Cache\Prototypes\CacheStore;
13
use Spiral\Cache\Stores\Memcache\DriverInterface;
14
use Spiral\Cache\Stores\Memcache\MemcachedDriver;
15
use Spiral\Cache\Stores\Memcache\MemcacheDriver;
16
17
/**
18
 * Talks to Memcache and Memcached drivers using interface.
19
 */
20
class MemcacheStore extends CacheStore
21
{
22
    /**
23
     * Maximum expiration time you can set.
24
     *
25
     * @link http://www.php.net/manual/ru/memcache.set.php
26
     */
27
    const MAX_EXPIRATION = 2592000;
28
29
    /**
30
     * @var string
31
     */
32
    private $prefix;
33
34
    /**
35
     * Currently active driver.
36
     *
37
     * @var DriverInterface
38
     */
39
    private $driver;
40
41
    /**
42
     * @param string          $prefix
43
     * @param array           $servers
44
     * @param DriverInterface $driver Pre-created driver instance.
45
     * @param bool            $connect
46
     */
47
    public function __construct(
48
        string $prefix = 'spiral:',
49
        array $servers = [],
50
        DriverInterface $driver = null,
51
        bool $connect = true
52
    ) {
53
        $this->prefix = $prefix;
54
55
        if (is_object($driver)) {
56
            $this->setDriver($driver, $connect);
57
58
            return;
59
        }
60
61
        if (empty($servers)) {
62
            throw new StoreException(
63
                'Unable to create Memcache[d] cache store. A least one server has to be specified.'
64
            );
65
        }
66
67
        //New Driver creation
68
        if (class_exists('Memcache')) {
69
            $this->setDriver(new MemcacheDriver($servers), true);
70
        }
71
72
        if (class_exists('Memcached')) {
73
            $this->setDriver(new MemcachedDriver($servers), true);
74
        }
75
76
        if (empty($this->driver)) {
77
            throw new StoreException('No available Memcache drivers found.');
78
        }
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function isAvailable(): bool
85
    {
86
        return $this->driver->isAvailable();
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function has(string $name): bool
93
    {
94
        return $this->driver->get($this->prefix . $name);
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function get(string $name)
101
    {
102
        return $this->driver->get($this->prefix . $name);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     *
108
     * Will apply MAX_EXPIRATION.
109
     */
110
    public function set(string $name, $data, $ttl = null)
111
    {
112
        $ttl = min(self::MAX_EXPIRATION + time(), $ttl + time());
113
        if ($ttl < 0) {
114
            $ttl = 0;
115
        }
116
117
        return $this->driver->set($this->prefix . $name, $data, $ttl);
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function delete(string $name)
124
    {
125
        return $this->driver->delete($this->prefix . $name);
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function inc(string $name, int $delta = 1): int
132
    {
133
        return $this->driver->inc($this->prefix . $name);
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function dec(string $name, int $delta = 1): int
140
    {
141
        return $this->driver->dec($this->prefix . $name, $delta);
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function clear()
148
    {
149
        $this->driver->clear();
150
    }
151
152
    /**
153
     * Set pre-created Memcache driver.
154
     *
155
     * @param DriverInterface $driver  Pre-created driver instance.
156
     * @param bool            $connect Force connection.
157
     */
158
    protected function setDriver(DriverInterface $driver, bool $connect = false)
159
    {
160
        $this->driver = $driver;
161
162
        if ($connect) {
163
            $this->driver->connect();
164
        }
165
    }
166
}
167