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 ValueTrait |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Gets the value of a key. |
13
|
|
|
* |
14
|
|
|
* @param string $key |
15
|
|
|
* |
16
|
|
|
* @return mixed The value of the key. |
17
|
|
|
* |
18
|
|
|
* @throws KeyNotFoundException |
19
|
|
|
* @throws \Exception |
20
|
|
|
*/ |
21
|
|
|
public function get($key) |
22
|
|
|
{ |
23
|
|
|
return $this->getValue($key); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Sets the value of a key. |
28
|
|
|
* |
29
|
|
|
* @param string $key |
30
|
|
|
* @param mixed $value Can be any of serializable data type. |
31
|
|
|
* |
32
|
|
|
* @return bool True if the set was successful, false if it was unsuccessful. |
33
|
|
|
* |
34
|
|
|
* @throws \Exception |
35
|
|
|
*/ |
36
|
|
|
public function set($key, $value) |
37
|
|
|
{ |
38
|
|
|
if (!$this->shmProxy->put($this->client, $key, $value)) { |
|
|
|
|
39
|
|
|
throw new \Exception('Shm put error'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return true; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Gets value, watches expiring. |
47
|
|
|
* |
48
|
|
|
* @param string $key |
49
|
|
|
* |
50
|
|
|
* @return mixed |
51
|
|
|
* |
52
|
|
|
* @throws KeyNotFoundException |
53
|
|
|
* @throws \Exception |
54
|
|
|
*/ |
55
|
|
|
protected function getValue($key) |
56
|
|
|
{ |
57
|
|
|
if (!$this->shmProxy->has($this->client, $key)) { |
58
|
|
|
throw new KeyNotFoundException(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$getResult = $this->shmProxy->get($this->client, $key); |
62
|
|
|
$unserialized = @unserialize($getResult); |
63
|
|
|
|
64
|
|
|
if (Util::hasInternalExpireTime($unserialized)) { |
65
|
|
|
$this->handleTtl($key, $unserialized['ts'], $unserialized['s']); |
66
|
|
|
|
67
|
|
|
$getResult = $unserialized['v']; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $getResult; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* If ttl is lesser or equals 0 delete key. |
75
|
|
|
* |
76
|
|
|
* @param string $key |
77
|
|
|
* @param int $expireSetTs |
78
|
|
|
* @param int $expireSec |
79
|
|
|
* |
80
|
|
|
* @return int ttl |
81
|
|
|
* |
82
|
|
|
* @throws KeyNotFoundException |
83
|
|
|
* @throws \Exception |
84
|
|
|
*/ |
85
|
|
|
protected function handleTtl($key, $expireSetTs, $expireSec) |
86
|
|
|
{ |
87
|
|
|
$ttl = $expireSetTs + $expireSec - time(); |
88
|
|
|
if ($ttl <= 0) { |
89
|
|
|
if (!$this->shmProxy->remove($this->client, $key)) { |
90
|
|
|
throw new \Exception('Shm remove error'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
throw new KeyNotFoundException(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $ttl; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: