1use crate::redis::Redis;
2use json::{JsonValue};
3use std::sync::RwLock;
4use once_cell::sync::Lazy;
5pub use crate::config::{CacheMode, Config, Connection};
6
7pub mod config;
8mod redis;
9
10static GLOBAL_CONFIG: Lazy<RwLock<Config>> = Lazy::new(|| {
11 RwLock::new(Config::default())
12});
13
14#[derive(Clone)]
15pub enum Cache {
16 Redis(Redis),
17 None,
18}
19
20impl Cache {
21 pub fn new(config: Config) -> Self {
23 {
24 let mut data = GLOBAL_CONFIG.write().unwrap();
25 data.clone_from(&config);
26 }
27 let config = GLOBAL_CONFIG.read().unwrap();
28 let connection = config.connections.get(config.default.as_str()).unwrap().clone();
29 match connection.mode {
30 CacheMode::Redis => {
31 match Redis::connect(connection) {
32 Ok(e) => Cache::Redis(e),
33 Err(_) => Cache::None
34 }
35 }
36 CacheMode::None => Cache::None
37 }
38 }
39
40 pub fn create(name: &str, connection: Connection) -> Self {
42 {
43 let mut data = GLOBAL_CONFIG.write().unwrap();
44 if !data.connections.contains_key(name) {
45 data.connections.insert(name.to_string(), connection);
46 }
47 data.default = name.to_string();
48 }
49 let config = GLOBAL_CONFIG.read().unwrap();
50 let connection = config.connections.get(config.default.as_str()).unwrap().clone();
51 match connection.mode {
52 CacheMode::Redis => {
53 match Redis::connect(connection) {
54 Ok(e) => Cache::Redis(e),
55 Err(_) => Cache::None
56 }
57 }
58 CacheMode::None => Cache::None
59 }
60 }
61 pub fn connections(&mut self) -> JsonValue {
63 let mut connections = vec![];
64 let data = GLOBAL_CONFIG.read().unwrap();
65 for (item, mut value) in data.connections.clone() {
66 if value.mode.str().is_empty() {
67 continue;
68 }
69 let mut t = value.json();
70 t["name"] = item.into();
71 connections.push(t);
72 }
73 connections.into()
74 }
75 pub fn connection(&mut self, name: &str) -> Self {
77 let mut data = GLOBAL_CONFIG.write().unwrap();
78 if data.connections.contains_key(name) {
79 if name == data.default {
80 return self.clone();
81 }
82 data.default = name.to_string();
83 let connection = data.connections.get(data.default.as_str()).unwrap().clone();
84 match connection.mode {
85 CacheMode::Redis => {
86 match Redis::connect(connection) {
87 Ok(e) => Cache::Redis(e),
88 Err(_) => Cache::None
89 }
90 }
91 CacheMode::None => Cache::None
92 }
93 } else {
94 Cache::None
95 }
96 }
97}
98
99
100pub trait CacheBase {
101 fn db(&mut self, db: i8) -> &mut Self;
103
104 fn key_exists(&mut self, key: &str) -> Result<bool, String>;
106 fn key_del(&mut self, key: &str) -> Result<bool, String>;
108 fn key_ttl(&mut self, key: &str) -> Result<i64, String>;
110
111 fn key_set_expireat(&mut self, key: &str, timestamp: i64) -> Result<bool, String>;
115 fn key_set_seconds(&mut self, key: &str, s: i64) -> Result<bool, String>;
118 fn key_del_expire(&mut self, key: &str) -> Result<bool, String>;
120 fn key_query(&mut self, key: &str) -> Result<JsonValue, String>;
123
124 fn add(&mut self, key: &str, value: JsonValue, expiration_date: u64) -> Result<bool, String>;
128 fn get(&mut self, key: &str) -> Result<JsonValue, String>;
130
131 fn set_add(&mut self, key: &str, value: JsonValue, expiry_s: i64) -> Result<bool, String>;
133 fn set_count(&mut self, key: &str) -> Result<usize, String>;
135 fn set_get(&mut self, key: &str) -> Result<JsonValue, String>;
137 fn set_delete(&mut self, key: &str, value: JsonValue) -> Result<bool, String>;
139 fn set_get_sinter(&mut self, keys: Vec<&str>) -> Result<JsonValue, String>;
141 fn set_get_sunion(&mut self, keys: Vec<&str>) -> Result<JsonValue, String>;
143
144 fn list_add(&mut self, key: &str, value: JsonValue, expiry_s: i64) -> Result<bool, String>;
146 fn list_del(&mut self, key: &str, value: JsonValue) -> Result<bool, String>;
148 fn hash_get(&mut self, key: &str) -> Result<JsonValue, String>;
150 fn hash_add(&mut self, key: &str, field: &str, value: JsonValue) -> Result<bool, String>;
152 fn hash_get_field_value(&mut self, key: &str, field: &str) -> Result<JsonValue, String>;
154 fn hash_get_fields(&mut self, key: &str) -> Result<JsonValue, String>;
156 fn hash_delete(&mut self, key: &str, field: &str) -> Result<bool, String>;
158 fn hash_get_values(&mut self, key: &str) -> Result<JsonValue, String>;
160 fn geo_add(&mut self, key: &str, longitude: f64, latitude: f64, value: JsonValue) -> Result<bool, String>;
162 fn geo_get(&mut self, key: &str, value: JsonValue) -> Result<JsonValue, String>;
164 fn geo_dist(&mut self, key: &str, value1: JsonValue, value2: JsonValue) -> Result<JsonValue, String>;
166 fn geo_radius(&mut self, key: &str, value: JsonValue, radius: &str) -> Result<JsonValue, String>;
168
169 fn stream_add(&mut self, key: &str, msg_id: &str, field: &str, value: JsonValue) -> Result<String, String>;
171 fn stream_count(&mut self, key: &str) -> Result<usize, String>;
173 fn stream_get(&mut self, key: &str) -> Result<JsonValue, String>;
175 fn stream_del(&mut self, key: &str, id: &str) -> Result<bool, String>;
177
178 fn stream_group_create(&mut self, key: &str, group: &str) -> Result<bool, String>;
180 fn stream_group_add_user(&mut self, key: &str, group: &str, user: &str) -> Result<bool, String>;
181 fn stream_group_del_user(&mut self, key: &str, group: &str, user: &str) -> Result<bool, String>;
182 fn stream_group_del(&mut self, key: &str, group: &str) -> Result<bool, String>;
184 fn stream_group_msg(&mut self, key: &str, group: &str, user: &str) -> Result<JsonValue, String>;
186 fn stream_get_group(&mut self, key: &str, group: &str) -> Result<bool, String>;
188
189 fn stream_get_stream(&mut self, key: &str) -> Result<JsonValue, String>;
190}
191
192impl Cache {
193 pub fn db(&mut self, db: i8) -> &mut Self {
194 match self {
195 Cache::Redis(e) => {
196 e.db(db);
197 }
198 Cache::None => {}
199 }
200 self
201 }
202
203 pub fn key_exists(&mut self, key: &str) -> Result<bool, String> {
204 Ok(match self {
205 Cache::Redis(e) => e.key_exists(key)?,
206 Cache::None => return Err("error".to_string()),
207 })
208 }
209
210 pub fn key_del(&mut self, key: &str) -> Result<bool, String> {
211 Ok(match self {
212 Cache::Redis(e) => e.key_del(key)?,
213 Cache::None => return Err("error".to_string()),
214 })
215 }
216
217 pub fn key_ttl(&mut self, key: &str) -> Result<i64, String> {
218 Ok(match self {
219 Cache::Redis(e) => e.key_ttl(key)?,
220 Cache::None => return Err("error".to_string()),
221 })
222 }
223
224
225 pub fn key_set_expireat(&mut self, key: &str, timestamp: i64) -> Result<bool, String> {
226 Ok(match self {
227 Cache::Redis(e) => e.key_set_expireat(key, timestamp)?,
228 Cache::None => return Err("error".to_string()),
229 })
230 }
231
232 pub fn key_set_seconds(&mut self, key: &str, s: i64) -> Result<bool, String> {
233 Ok(match self {
234 Cache::Redis(e) => e.key_set_seconds(key, s)?,
235 Cache::None => return Err("error".to_string()),
236 })
237 }
238
239 pub fn key_del_expire(&mut self, key: &str) -> Result<bool, String> {
240 Ok(match self {
241 Cache::Redis(e) => e.key_del_expire(key)?,
242 Cache::None => return Err("error".to_string()),
243 })
244 }
245
246 pub fn key_query(&mut self, key: &str) -> Result<JsonValue, String> {
247 let res = match self {
248 Cache::Redis(e) => e.key_query(key)?,
249 Cache::None => return Err("error".to_string()),
250 };
251 Ok(res)
252 }
253 pub fn add(&mut self, key: &str, value: JsonValue, expiration_date: u64) -> Result<bool, String> {
254 Ok(match self {
255 Cache::Redis(e) => e.add(key, value, expiration_date)?,
256 Cache::None => return Err("error".to_string()),
257 })
258 }
259 pub fn get(&mut self, key: &str) -> Result<JsonValue, String> {
260 let res = match self {
261 Cache::Redis(e) => e.get(key)?,
262 Cache::None => return Err("error".to_string()),
263 };
264 Ok(res)
265 }
266
267
268 pub fn set_add(&mut self, key: &str, value: JsonValue, expiry_s: i64) -> Result<bool, String> {
269 let res = match self {
270 Cache::Redis(e) => e.set_add(key, value, expiry_s)?,
271 Cache::None => return Err("error".to_string()),
272 };
273 Ok(res)
274 }
275 pub fn set_count(&mut self, key: &str) -> Result<usize, String> {
276 let res = match self {
277 Cache::Redis(e) => e.set_count(key)?,
278 Cache::None => return Err("error".to_string()),
279 };
280 Ok(res)
281 }
282 pub fn set_get(&mut self, key: &str) -> Result<JsonValue, String> {
283 let res = match self {
284 Cache::Redis(e) => e.set_get(key)?,
285 Cache::None => return Err("error".to_string()),
286 };
287 Ok(res)
288 }
289
290 pub fn set_delete(&mut self, key: &str, value: JsonValue) -> Result<bool, String> {
291 Ok(match self {
292 Cache::Redis(e) => e.set_delete(key, value)?,
293 Cache::None => return Err("error".to_string()),
294 })
295 }
296
297 pub fn set_get_sinter(&mut self, keys: Vec<&str>) -> Result<JsonValue, String> {
298 Ok(match self {
299 Cache::Redis(e) => e.set_get_sinter(keys)?,
300 Cache::None => return Err("error".to_string()),
301 })
302 }
303
304 pub fn set_get_sunion(&mut self, keys: Vec<&str>) -> Result<JsonValue, String> {
305 Ok(match self {
306 Cache::Redis(e) => e.set_get_sunion(keys)?,
307 Cache::None => return Err("error".to_string()),
308 })
309 }
310
311 pub fn list_add(&mut self, key: &str, value: JsonValue, expiry_s: i64) -> Result<bool, String> {
312 Ok(match self {
313 Cache::Redis(e) => e.list_add(key, value, expiry_s)?,
314 Cache::None => return Err("error".to_string()),
315 })
316 }
317
318 pub fn list_del(&mut self, key: &str, value: JsonValue) -> Result<bool, String> {
319 Ok(match self {
320 Cache::Redis(e) => e.list_del(key, value)?,
321 Cache::None => return Err("error".to_string()),
322 })
323 }
324
325
326 pub fn hash_get(&mut self, key: &str) -> Result<JsonValue, String> {
328 let res = match self {
329 Cache::Redis(e) => e.hash_get(key)?,
330 Cache::None => return Err("error".to_string()),
331 };
332 Ok(res)
333 }
334 pub fn hash_add(&mut self, key: &str, field: &str, value: JsonValue) -> Result<bool, String> {
336 Ok(match self {
337 Cache::Redis(e) => e.hash_add(key, field, value)?,
338 Cache::None => return Err("error".to_string()),
339 })
340 }
341 pub fn hash_get_field_value(&mut self, key: &str, field: &str) -> Result<JsonValue, String> {
343 let res = match self {
344 Cache::Redis(e) => e.hash_get_field_value(key, field)?,
345 Cache::None => return Err("error".to_string()),
346 };
347 Ok(res)
348 }
349 pub fn hash_get_fields(&mut self, key: &str) -> Result<JsonValue, String> {
351 Ok(match self {
352 Cache::Redis(e) => e.hash_get_fields(key)?,
353 Cache::None => return Err("error".to_string()),
354 })
355 }
356 pub fn hash_delete(&mut self, key: &str, field: &str) -> Result<bool, String> {
357 Ok(match self {
358 Cache::Redis(e) => e.hash_delete(key, field)?,
359 Cache::None => return Err("error".to_string()),
360 })
361 }
362 pub fn hash_get_values(&mut self, key: &str) -> Result<JsonValue, String> {
364 Ok(match self {
365 Cache::Redis(e) => e.hash_get_values(key)?,
366 Cache::None => return Err("error".to_string()),
367 })
368 }
369
370 pub fn geo_add(&mut self, key: &str, longitude: f64, latitude: f64, value: JsonValue) -> Result<bool, String> {
371 Ok(match self {
372 Cache::Redis(e) => e.geo_add(key, longitude, latitude, value)?,
373 Cache::None => return Err("error".to_string()),
374 })
375 }
376
377
378 pub fn geo_get(&mut self, key: &str, value: JsonValue) -> Result<JsonValue, String> {
379 Ok(match self {
380 Cache::Redis(e) => e.geo_get(key, value)?,
381 Cache::None => return Err("error".to_string()),
382 })
383 }
384
385 pub fn geo_dist(&mut self, key: &str, value1: JsonValue, value2: JsonValue) -> Result<JsonValue, String> {
386 Ok(match self {
387 Cache::Redis(e) => e.geo_dist(key, value1, value2)?,
388 Cache::None => return Err("error".to_string()),
389 })
390 }
391
392 pub fn geo_radius(&mut self, key: &str, value: JsonValue, radius: &str) -> Result<JsonValue, String> {
393 Ok(match self {
394 Cache::Redis(e) => e.geo_radius(key, value, radius)?,
395 Cache::None => return Err("error".to_string()),
396 })
397 }
398 pub fn stream_add(&mut self, key: &str, msg_id: &str, field: &str, value: JsonValue) -> Result<String, String> {
399 match self {
400 Cache::Redis(e) => e.stream_add(key, msg_id, field, value),
401 Cache::None => Err("error".to_string()),
402 }
403 }
404 pub fn stream_count(&mut self, key: &str) -> Result<usize, String> {
405 Ok(match self {
406 Cache::Redis(e) => e.stream_count(key)?,
407 Cache::None => return Err("error".to_string()),
408 })
409 }
410
411 pub fn stream_get(&mut self, key: &str) -> Result<JsonValue, String> {
412 Ok(match self {
413 Cache::Redis(e) => e.stream_get(key)?,
414 Cache::None => return Err("error".to_string()),
415 })
416 }
417
418
419 pub fn stream_del(&mut self, key: &str, id: &str) -> Result<bool, String> {
420 Ok(match self {
421 Cache::Redis(e) => e.stream_del(key, id)?,
422 Cache::None => return Err("error".to_string()),
423 })
424 }
425
426 pub fn stream_group_create(&mut self, key: &str, group: &str) -> Result<bool, String> {
427 Ok(match self {
428 Cache::Redis(e) => e.stream_group_create(key, group)?,
429 Cache::None => return Err("error".to_string()),
430 })
431 }
432
433 pub fn stream_group_add_user(&mut self, key: &str, group: &str, user: &str) -> Result<bool, String> {
434 Ok(match self {
435 Cache::Redis(e) => e.stream_group_add_user(key, group, user)?,
436 Cache::None => return Err("error".to_string()),
437 })
438 }
439
440 pub fn stream_group_del_user(&mut self, key: &str, group: &str, user: &str) -> Result<bool, String> {
441 Ok(match self {
442 Cache::Redis(e) => e.stream_group_del_user(key, group, user)?,
443 Cache::None => return Err("error".to_string()),
444 })
445 }
446
447 pub fn stream_group_del(&mut self, key: &str, group: &str) -> Result<bool, String> {
448 Ok(match self {
449 Cache::Redis(e) => e.stream_group_del(key, group)?,
450 Cache::None => return Err("error".to_string()),
451 })
452 }
453
454 pub fn stream_group_msg(&mut self, key: &str, group: &str, user: &str) -> Result<JsonValue, String> {
455 Ok(match self {
456 Cache::Redis(e) => e.stream_group_msg(key, group, user)?,
457 Cache::None => return Err("error".to_string()),
458 })
459 }
460
461 pub fn stream_get_group(&mut self, key: &str, group: &str) -> Result<bool, String> {
462 Ok(match self {
463 Cache::Redis(e) => e.stream_get_group(key, group)?,
464 Cache::None => return Err("error".to_string()),
465 })
466 }
467
468 pub fn stream_get_stream(&mut self, key: &str) -> Result<JsonValue, String> {
469 Ok(match self {
470 Cache::Redis(e) => e.stream_get_stream(key)?,
471 Cache::None => return Err("error".to_string()),
472 })
473 }
474}