|
1
|
|
|
<?php namespace AdammBalogh\KeyValueStore\Adapter\SharedMemoryAdapter; |
|
2
|
|
|
|
|
3
|
|
|
use AdammBalogh\KeyValueStore\Adapter\Util; |
|
4
|
|
|
use AdammBalogh\KeyValueStore\Exception\KeyNotFoundException; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
8
|
|
|
*/ |
|
9
|
|
|
trait KeyTrait |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Removes a key. |
|
13
|
|
|
* |
|
14
|
|
|
* @param string $key |
|
15
|
|
|
* |
|
16
|
|
|
* @return bool True if the deletion was successful, false if the deletion was unsuccessful. |
|
17
|
|
|
* |
|
18
|
|
|
* @throws \Exception |
|
19
|
|
|
*/ |
|
20
|
|
|
public function delete($key) |
|
21
|
|
|
{ |
|
22
|
|
|
try { |
|
23
|
|
|
$this->get($key); |
|
|
|
|
|
|
24
|
|
|
} catch (KeyNotFoundException $e) { |
|
25
|
|
|
return false; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
if (!$this->shmProxy->remove($this->client, $key)) { |
|
|
|
|
|
|
29
|
|
|
throw new \Exception('Shm remove error'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return true; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Sets a key's time to live in seconds. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $key |
|
39
|
|
|
* @param int $seconds |
|
40
|
|
|
* |
|
41
|
|
|
* @return bool True if the timeout was set, false if the timeout could not be set. |
|
42
|
|
|
* |
|
43
|
|
|
* @throws \Exception |
|
44
|
|
|
*/ |
|
45
|
|
|
public function expire($key, $seconds) |
|
46
|
|
|
{ |
|
47
|
|
|
try { |
|
48
|
|
|
$value = $this->get($key); |
|
|
|
|
|
|
49
|
|
|
} catch (KeyNotFoundException $e) { |
|
50
|
|
|
return false; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $this->set($key, Util::getDataWithExpire($value, $seconds, time())); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Returns the remaining time to live of a key that has a timeout. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $key |
|
60
|
|
|
* |
|
61
|
|
|
* @return int Ttl in seconds. |
|
62
|
|
|
* |
|
63
|
|
|
* @throws KeyNotFoundException |
|
64
|
|
|
* @throws \Exception |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getTtl($key) |
|
67
|
|
|
{ |
|
68
|
|
|
if (!$this->shmProxy->has($this->client, $key)) { |
|
69
|
|
|
throw new KeyNotFoundException(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$getResult = $this->shmProxy->get($this->client, $key); |
|
73
|
|
|
$unserialized = @unserialize($getResult); |
|
74
|
|
|
|
|
75
|
|
|
if (!Util::hasInternalExpireTime($unserialized)) { |
|
76
|
|
|
throw new \Exception('Cannot retrieve ttl'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $this->handleTtl($key, $unserialized['ts'], $unserialized['s']); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Determines if a key exists. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $key |
|
86
|
|
|
* |
|
87
|
|
|
* @return bool True if the key does exist, false if the key does not exist. |
|
88
|
|
|
* |
|
89
|
|
|
* @throws \Exception |
|
90
|
|
|
*/ |
|
91
|
|
|
public function has($key) |
|
92
|
|
|
{ |
|
93
|
|
|
try { |
|
94
|
|
|
$this->get($key); |
|
|
|
|
|
|
95
|
|
|
} catch (KeyNotFoundException $e) { |
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Removes the existing timeout on key, turning the key from volatile (a key with an expire set) |
|
104
|
|
|
* to persistent (a key that will never expire as no timeout is associated). |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $key |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool True if the persist was success, false if the persis was unsuccessful. |
|
109
|
|
|
* |
|
110
|
|
|
* @throws \Exception |
|
111
|
|
|
*/ |
|
112
|
|
|
public function persist($key) |
|
113
|
|
|
{ |
|
114
|
|
|
if (!$this->shmProxy->has($this->client, $key)) { |
|
115
|
|
|
return false; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$getResult = $this->shmProxy->get($this->client, $key); |
|
119
|
|
|
$unserialized = @unserialize($getResult); |
|
120
|
|
|
|
|
121
|
|
|
if (!Util::hasInternalExpireTime($unserialized)) { |
|
122
|
|
|
return false; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
try { |
|
126
|
|
|
$this->handleTtl($key, $unserialized['ts'], $unserialized['s']); |
|
|
|
|
|
|
127
|
|
|
} catch (KeyNotFoundException $e) { |
|
128
|
|
|
return false; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $this->set($key, $unserialized['v']); |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.