-
Notifications
You must be signed in to change notification settings - Fork 1
/
PHSD.php
205 lines (189 loc) · 5.29 KB
/
PHSD.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/**
* PHSD is a PHP Library for handling data storage with expiration.
*
* @category Library
* @package PHSD
*/
class PHSD {
private static $data = [];
private static $file = '.env';
/**
* Initialize the data storage from the file.
*/
private static function init() {
if (file_exists(self::$file)) {
self::$data = json_decode(file_get_contents(self::$file), true) ?: [];
} else {
self::$data = [];
}
self::expireAllExpired();
}
/**
* Save the data storage to the file, each data on a new line.
*/
private static function save() {
$jsonData = json_encode(self::$data, JSON_PRETTY_PRINT);
file_put_contents(self::$file, $jsonData . PHP_EOL);
}
/**
* Add a key-value pair to the data storage with optional expiration.
*
* @param string $key The key to add.
* @param mixed $value The value to add.
* @param int|null $expiration Expiration time in minutes (optional).
* @return void
*/
public static function add($key, $value, $expiration = null) {
self::init();
self::$data[$key] = [
'value' => $value,
'expiration' => self::calculateExpiration($expiration)
];
self::save();
}
/**
* Update the value and expiration of an existing key.
*
* @param string $key The key to update.
* @param mixed $value The new value.
* @param int|null $expiration New expiration time in minutes (optional).
* @return void
*/
public static function update($key, $value, $expiration = null) {
self::init();
if (self::exists($key)) {
self::$data[$key]['value'] = $value;
self::$data[$key]['expiration'] = self::calculateExpiration($expiration);
self::save();
}
}
/**
* Remove a key from the data storage.
*
* @param string $key The key to remove.
* @return void
*/
public static function remove($key) {
self::init();
unset(self::$data[$key]);
self::save();
}
/**
* Get the value of a key from the data storage.
*
* @param string $key The key to retrieve.
* @return mixed|null The value or null if the key does not exist or has expired.
*/
public static function get($key) {
self::init();
if (self::exists($key)) {
return self::$data[$key]['value'];
}
return null;
}
/**
* Get all key-value pairs from the data storage.
*
* @return array The entire data storage.
*/
public static function getAll() {
self::init();
return self::$data;
}
/**
* Expire a specific key immediately.
*
* @param string $key The key to expire.
* @return void
*/
public static function expire($key) {
if (self::exists($key)) {
self::$data[$key]['expiration'] = time();
self::save();
}
}
/**
* Expire all keys immediately.
*
* @return void
*/
public static function expireAll() {
foreach (self::$data as $key => $value) {
self::$data[$key]['expiration'] = time();
}
self::save();
}
/**
* Get details of all expired keys.
*
* @return array The expired key-value pairs.
*/
public static function getExpiredDetails() {
$expired = [];
foreach (self::$data as $key => $value) {
if ($value['expiration'] !== null && $value['expiration'] <= time()) {
$expired[$key] = $value;
}
}
return $expired;
}
/**
* Get details of all active keys.
*
* @return array The active key-value pairs.
*/
public static function getActiveDetails() {
$active = [];
foreach (self::$data as $key => $value) {
if ($value['expiration'] === null || $value['expiration'] > time()) {
$active[$key] = $value;
}
}
return $active;
}
/**
* Remove all keys from the data storage.
*
* @return void
*/
public static function removeAll() {
self::$data = [];
self::save();
}
/**
* Remove all expired keys from the data storage.
*
* @return void
*/
public static function expireAllExpired() {
$expiredDetails = self::getExpiredDetails();
foreach ($expiredDetails as $key => $value) {
unset(self::$data[$key]);
}
self::save();
}
/**
* Check if a key exists in the data storage.
*
* @param string $key The key to check.
* @return bool True if the key exists, false otherwise.
*/
private static function exists($key) {
return isset(self::$data[$key]);
}
/**
* Calculate the expiration timestamp based on the given expiration time in minutes.
*
* @param int|null $expiration Expiration time in minutes (optional).
* @return int|null The expiration timestamp or null if no expiration.
*/
private static function calculateExpiration($expiration) {
if ($expiration === null || $expiration === 0) {
return null;
} else {
return time() + ($expiration * 60);
}
}
}
?>