MemcachedAdapter::has()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * Adapter for Memcached
4
 *
5
 * @file      MemcachedAdapter.php
6
 *
7
 * PHP version 8.0+
8
 *
9
 * @author    Alexander Yancharuk <alex at itvault dot info>
10
 * @copyright © 2012-2021 Alexander Yancharuk
11
 * @date      8/21/13 18:42
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
14
 */
15
16
namespace Veles\Cache\Adapters;
17
18
use Exception;
19
use Memcached;
20
21
/**
22
 * Class MemcachedAdapter
23
 * @author  Alexander Yancharuk <alex at itvault dot info>
24
 */
25
class MemcachedAdapter extends CacheAdapterAbstract implements CacheAdapterInterface
26
{
27
	/**
28
	 * Create Memcached class instance and connect to memcached pool
29
	 */
30 1
	protected function __construct()
31
	{
32 1
		$this->setDriver(new Memcached);
33
	}
34
35
	/**
36
	 * Get data
37
	 *
38
	 * @param string $key Key
39
	 * @return mixed
40
	 */
41 7
	public function get($key)
42
	{
43 7
		return $this->getDriver()->get($key);
44
	}
45
46
	/**
47
	 * Save data
48
	 *
49
	 * @param string $key Key
50
	 * @param mixed $value Data
51
	 * @param int $ttl Time to live
52
	 * @return bool
53
	 */
54 5
	public function set($key, $value, $ttl)
55
	{
56 5
		return $this->getDriver()->set($key, $value, $ttl);
57
	}
58
59
	/**
60
	 * Save data if key not exists
61
	 *
62
	 * @param string $key   Key
63
	 * @param mixed  $value Data
64
	 * @param int    $ttl   Time to live
65
	 *
66
	 * @return mixed
67
	 */
68 3
	public function add($key, $value, $ttl)
69
	{
70 3
		return $this->getDriver()->add($key, $value, $ttl);
71
	}
72
73
	/**
74
	 * Check if data stored in cache
75
	 *
76
	 * @param string $key Key
77
	 * @return bool
78
	 */
79 2
	public function has($key)
80
	{
81 2
		return (bool) $this->getDriver()->get($key);
82
	}
83
84
	/**
85
	 * Delete data
86
	 *
87
	 * @param string $key Key
88
	 * @return bool
89
	 */
90 8
	public function del($key)
91
	{
92 8
		return $this->getDriver()->delete($key);
93
	}
94
95
	/**
96
	 * Method for deletion keys by template
97
	 *
98
	 * ATTENTION: if key contains spaces, for example 'THIS IS KEY::ID:50d98ld',
99
	 * then in cache it will be saved as 'THIS_IS_KEY::ID:50d98ld'. So, template
100
	 * for that key deletion must be look like - 'THIS_IS_KEY'.
101
	 * Deletion can be made by substring, containing in keys. For example
102
	 * '_KEY::ID'.
103
	 *
104
	 * @param string $tpl Substring containing in needed keys
105
	 * @return bool
106
	 */
107 3
	public function delByTemplate($tpl)
108
	{
109
		try {
110 3
			$cache = new MemcacheRaw();
111 2
			$cache->delByTemplate($tpl)->disconnect();
112 2
			return true;
113 1
		} catch (Exception $e) {
114 1
			return false;
115
		}
116
	}
117
118
	/**
119
	 * Cache cleanup
120
	 *
121
	 * @return bool
122
	 */
123 2
	public function clear()
124
	{
125 2
		return $this->getDriver()->flush();
126
	}
127
128
	/**
129
	 * Increment key value
130
	 *
131
	 * @param string $key Key
132
	 * @param int $offset Offset
133
	 *
134
	 * @return bool|int
135
	 */
136 3
	public function increment($key, $offset)
137
	{
138 3
		return $this->getDriver()->increment($key, $offset);
139
	}
140
141
	/**
142
	 * Decrement key value
143
	 *
144
	 * @param string $key Key
145
	 * @param int $offset Offset
146
	 *
147
	 * @return bool|int
148
	 */
149 3
	public function decrement($key, $offset)
150
	{
151 3
		return $this->getDriver()->decrement($key, $offset);
152
	}
153
}
154