1
|
|
|
<?php namespace AdammBalogh\KeyValueStore\Implementation; |
2
|
|
|
|
3
|
|
|
use AdammBalogh\KeyValueStore\Adapter\Util; |
4
|
|
|
use AdammBalogh\KeyValueStore\Exception\InternalException; |
5
|
|
|
use AdammBalogh\KeyValueStore\Exception\KeyNotFoundException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
9
|
|
|
*/ |
10
|
|
|
trait KeyTrait |
11
|
|
|
{ |
12
|
|
|
use AdapterTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Removes a key. |
16
|
|
|
* |
17
|
|
|
* @param string $key |
18
|
|
|
* |
19
|
|
|
* @return bool True if the deletion was successful, false if the deletion was unsuccessful. |
20
|
|
|
* |
21
|
|
|
* @throws \InvalidArgumentException |
22
|
|
|
* @throws InternalException |
23
|
|
|
*/ |
24
|
|
|
public function delete($key) |
25
|
|
|
{ |
26
|
|
|
Util::checkArgString($key); |
27
|
|
|
|
28
|
|
|
try { |
29
|
|
|
return $this->getAdapter()->delete($key); |
30
|
|
|
} catch (\Exception $e) { |
31
|
|
|
throw new InternalException($e->getMessage(), $e->getCode(), $e); |
32
|
|
|
} |
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 \InvalidArgumentException |
44
|
|
|
* @throws InternalException |
45
|
|
|
*/ |
46
|
|
|
public function expire($key, $seconds) |
47
|
|
|
{ |
48
|
|
|
Util::checkArgString($key); |
49
|
|
|
Util::checkArgInteger($seconds); |
50
|
|
|
|
51
|
|
|
try { |
52
|
|
|
return $this->getAdapter()->expire($key, $seconds); |
53
|
|
|
} catch (\Exception $e) { |
54
|
|
|
throw new InternalException($e->getMessage(), $e->getCode(), $e); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns the remaining time to live of a key that has a timeout. |
60
|
|
|
* |
61
|
|
|
* @param string $key |
62
|
|
|
* |
63
|
|
|
* @return int Ttl in seconds. |
64
|
|
|
* |
65
|
|
|
* @throws \InvalidArgumentException |
66
|
|
|
* @throws KeyNotFoundException |
67
|
|
|
* @throws InternalException |
68
|
|
|
*/ |
69
|
|
|
public function getTtl($key) |
70
|
|
|
{ |
71
|
|
|
Util::checkArgString($key); |
72
|
|
|
|
73
|
|
|
try { |
74
|
|
|
return $this->getAdapter()->getTtl($key); |
75
|
|
|
} catch (KeyNotFoundException $e) { |
76
|
|
|
throw $e; |
77
|
|
|
} catch (\Exception $e) { |
78
|
|
|
throw new InternalException($e->getMessage(), $e->getCode(), $e); |
79
|
|
|
} |
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 \InvalidArgumentException |
90
|
|
|
* @throws InternalException |
91
|
|
|
*/ |
92
|
|
|
public function has($key) |
93
|
|
|
{ |
94
|
|
|
Util::checkArgString($key); |
95
|
|
|
|
96
|
|
|
try { |
97
|
|
|
return $this->getAdapter()->has($key); |
98
|
|
|
} catch (\Exception $e) { |
99
|
|
|
throw new InternalException($e->getMessage(), $e->getCode(), $e); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Removes the existing timeout on key, turning the key from volatile (a key with an expire set) |
105
|
|
|
* to persistent (a key that will never expire as no timeout is associated). |
106
|
|
|
* |
107
|
|
|
* @param string $key |
108
|
|
|
* |
109
|
|
|
* @return bool True if the persist was success, false if the persis was unsuccessful. |
110
|
|
|
* |
111
|
|
|
* @throws \InvalidArgumentException |
112
|
|
|
* @throws InternalException |
113
|
|
|
*/ |
114
|
|
|
public function persist($key) |
115
|
|
|
{ |
116
|
|
|
Util::checkArgString($key); |
117
|
|
|
|
118
|
|
|
try { |
119
|
|
|
return $this->getAdapter()->persist($key); |
120
|
|
|
} catch (\Exception $e) { |
121
|
|
|
throw new InternalException($e->getMessage(), $e->getCode(), $e); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|