1use std::sync::mpsc::{Sender};
2use crate::redis::Redis;
3use json::{JsonValue};
4use std::sync::RwLock;
5use once_cell::sync::Lazy;
6pub use crate::config::{CacheMode, Config, Connection};
7
8pub mod config;
9mod redis;
10
11static GLOBAL_CONFIG: Lazy<RwLock<Config>> = Lazy::new(|| {
12 RwLock::new(Config::default())
13});
14
15#[derive(Clone)]
16pub enum Cache {
17 Redis(Redis),
18 None,
19}
20
21impl Cache {
22 pub fn new(config: Config) -> Self {
24 {
25 let mut data = GLOBAL_CONFIG.write().unwrap();
26 data.clone_from(&config);
27 }
28 let config = GLOBAL_CONFIG.read().unwrap();
29 let connection = config.connections.get(config.default.as_str()).unwrap().clone();
30 match connection.mode {
31 CacheMode::Redis => {
32 match Redis::connect(connection) {
33 Ok(e) => Cache::Redis(e),
34 Err(_) => Cache::None
35 }
36 }
37 CacheMode::None => Cache::None
38 }
39 }
40
41 pub fn create(name: &str, connection: Connection) -> Self {
43 {
44 let mut data = GLOBAL_CONFIG.write().unwrap();
45 if !data.connections.contains_key(name) {
46 data.connections.insert(name.to_string(), connection);
47 }
48 data.default = name.to_string();
49 }
50 let config = GLOBAL_CONFIG.read().unwrap();
51 let connection = config.connections.get(config.default.as_str()).unwrap().clone();
52 match connection.mode {
53 CacheMode::Redis => {
54 match Redis::connect(connection) {
55 Ok(e) => Cache::Redis(e),
56 Err(_) => Cache::None
57 }
58 }
59 CacheMode::None => Cache::None
60 }
61 }
62 pub fn connections(&mut self) -> JsonValue {
64 let mut connections = vec![];
65 let data = GLOBAL_CONFIG.read().unwrap();
66 for (item, mut value) in data.connections.clone() {
67 if value.mode.str().is_empty() {
68 continue;
69 }
70 let mut t = value.json();
71 t["name"] = item.into();
72 connections.push(t);
73 }
74 connections.into()
75 }
76 pub fn connection(&mut self, name: &str) -> Self {
78 let mut data = GLOBAL_CONFIG.write().unwrap();
79 if data.connections.contains_key(name) {
80 if name == data.default {
81 return self.clone();
82 }
83 data.default = name.to_string();
84 let connection = data.connections.get(data.default.as_str()).unwrap().clone();
85 match connection.mode {
86 CacheMode::Redis => {
87 match Redis::connect(connection) {
88 Ok(e) => Cache::Redis(e),
89 Err(_) => Cache::None
90 }
91 }
92 CacheMode::None => Cache::None
93 }
94 } else {
95 Cache::None
96 }
97 }
98}
99
100
101pub trait CacheBase {
102 fn db(&mut self, db: i8) -> &mut Self;
104
105 fn key_exists(&mut self, key: &str) -> Result<bool, String>;
107 fn key_del(&mut self, key: &str) -> Result<bool, String>;
109 fn key_ttl(&mut self, key: &str) -> Result<i64, String>;
111
112 fn key_set_expireat(&mut self, key: &str, timestamp: i64) -> Result<bool, String>;
116 fn key_set_seconds(&mut self, key: &str, s: i64) -> Result<bool, String>;
119 fn key_del_expire(&mut self, key: &str) -> Result<bool, String>;
121 fn key_query(&mut self, key: &str) -> Result<JsonValue, String>;
124
125 fn add(&mut self, key: &str, value: JsonValue, expiration_date: u64) -> Result<bool, String>;
129 fn get(&mut self, key: &str) -> Result<JsonValue, String>;
131
132 fn set_add(&mut self, key: &str, value: JsonValue, expiry_s: i64) -> Result<bool, String>;
134 fn set_count(&mut self, key: &str) -> Result<usize, String>;
136 fn set_get(&mut self, key: &str) -> Result<JsonValue, String>;
138 fn set_delete(&mut self, key: &str, value: JsonValue) -> Result<bool, String>;
140 fn set_get_sinter(&mut self, keys: Vec<&str>) -> Result<JsonValue, String>;
142 fn set_get_sunion(&mut self, keys: Vec<&str>) -> Result<JsonValue, String>;
144
145 fn list_add(&mut self, key: &str, value: JsonValue, expiry_s: i64) -> Result<bool, String>;
147 fn list_del(&mut self, key: &str, value: JsonValue) -> Result<bool, String>;
149 fn list_lpush(&mut self, key: &str, value: JsonValue, expiry_s: i64) -> Result<bool, String>;
151 fn list_rpush(&mut self, key: &str, value: JsonValue, expiry_s: i64) -> Result<bool, String>;
153 fn list_lpop(&mut self, key: &str, count: usize) -> Result<Vec<JsonValue>, String>;
156 fn list_rpop(&mut self, key: &str, count: usize) -> Result<Vec<JsonValue>, String>;
158 fn list_len(&mut self, key: &str) -> Result<usize, String>;
160 fn list_range(&mut self, key: &str, start: isize, stop: isize) -> Result<Vec<JsonValue>, String>;
162 fn list_all(&mut self, key: &str) -> Result<Vec<JsonValue>, String>;
164 fn list_get(&mut self, key: &str, index: isize) -> Result<JsonValue, String>;
166 fn list_trim(&mut self, key: &str, start: isize, stop: isize) -> Result<bool, String>;
168 fn list_set(&mut self, key: &str, index: isize, value: JsonValue) -> Result<bool, String>;
170 fn list_remove(&mut self, key: &str, value: JsonValue, count: isize) -> Result<isize, String>;
172 fn hash_get(&mut self, key: &str) -> Result<JsonValue, String>;
174 fn hash_add(&mut self, key: &str, field: &str, value: JsonValue) -> Result<bool, String>;
176 fn hash_get_field_value(&mut self, key: &str, field: &str) -> Result<JsonValue, String>;
178 fn hash_get_fields(&mut self, key: &str) -> Result<JsonValue, String>;
180 fn hash_delete(&mut self, key: &str, field: &str) -> Result<bool, String>;
182 fn hash_get_values(&mut self, key: &str) -> Result<JsonValue, String>;
184 fn geo_add(&mut self, key: &str, longitude: f64, latitude: f64, value: JsonValue) -> Result<bool, String>;
186 fn geo_get(&mut self, key: &str, value: JsonValue) -> Result<JsonValue, String>;
188 fn geo_dist(&mut self, key: &str, value1: JsonValue, value2: JsonValue) -> Result<JsonValue, String>;
190 fn geo_radius(&mut self, key: &str, value: JsonValue, radius: &str) -> Result<JsonValue, String>;
192
193 fn stream_add(&mut self, key: &str, msg_id: &str, field: &str, value: JsonValue) -> Result<String, String>;
195 fn stream_count(&mut self, key: &str) -> Result<usize, String>;
197 fn stream_get(&mut self, key: &str) -> Result<JsonValue, String>;
199 fn stream_del(&mut self, key: &str, id: &str) -> Result<bool, String>;
201
202 fn stream_group_create(&mut self, key: &str, group: &str) -> Result<bool, String>;
204 fn stream_group_add_user(&mut self, key: &str, group: &str, user: &str) -> Result<bool, String>;
205 fn stream_group_del_user(&mut self, key: &str, group: &str, user: &str) -> Result<bool, String>;
206 fn stream_group_del(&mut self, key: &str, group: &str) -> Result<bool, String>;
208 fn stream_group_msg(&mut self, key: &str, group: &str, user: &str) -> Result<JsonValue, String>;
210 fn stream_get_group(&mut self, key: &str, group: &str) -> Result<bool, String>;
212
213 fn stream_get_stream(&mut self, key: &str) -> Result<JsonValue, String>;
214
215 fn subscribe(&mut self, key: &str, tx: Sender<JsonValue>) -> Result<(), String>;
217 fn publish(&mut self, key: &str, value: JsonValue) -> Result<bool, String>;
219}
220
221impl Cache {
222 pub fn db(&mut self, db: i8) -> &mut Self {
223 match self {
224 Cache::Redis(e) => {
225 e.db(db);
226 }
227 Cache::None => {}
228 }
229 self
230 }
231
232 pub fn key_exists(&mut self, key: &str) -> Result<bool, String> {
233 Ok(match self {
234 Cache::Redis(e) => e.key_exists(key)?,
235 Cache::None => return Err("error".to_string()),
236 })
237 }
238
239 pub fn key_del(&mut self, key: &str) -> Result<bool, String> {
240 Ok(match self {
241 Cache::Redis(e) => e.key_del(key)?,
242 Cache::None => return Err("error".to_string()),
243 })
244 }
245
246 pub fn key_ttl(&mut self, key: &str) -> Result<i64, String> {
247 Ok(match self {
248 Cache::Redis(e) => e.key_ttl(key)?,
249 Cache::None => return Err("error".to_string()),
250 })
251 }
252
253
254 pub fn key_set_expireat(&mut self, key: &str, timestamp: i64) -> Result<bool, String> {
255 Ok(match self {
256 Cache::Redis(e) => e.key_set_expireat(key, timestamp)?,
257 Cache::None => return Err("error".to_string()),
258 })
259 }
260
261 pub fn key_set_seconds(&mut self, key: &str, s: i64) -> Result<bool, String> {
262 Ok(match self {
263 Cache::Redis(e) => e.key_set_seconds(key, s)?,
264 Cache::None => return Err("error".to_string()),
265 })
266 }
267
268 pub fn key_del_expire(&mut self, key: &str) -> Result<bool, String> {
269 Ok(match self {
270 Cache::Redis(e) => e.key_del_expire(key)?,
271 Cache::None => return Err("error".to_string()),
272 })
273 }
274
275 pub fn key_query(&mut self, key: &str) -> Result<JsonValue, String> {
276 let res = match self {
277 Cache::Redis(e) => e.key_query(key)?,
278 Cache::None => return Err("error".to_string()),
279 };
280 Ok(res)
281 }
282 pub fn add(&mut self, key: &str, value: JsonValue, expiration_date: u64) -> Result<bool, String> {
283 Ok(match self {
284 Cache::Redis(e) => e.add(key, value, expiration_date)?,
285 Cache::None => return Err("error".to_string()),
286 })
287 }
288 pub fn get(&mut self, key: &str) -> Result<JsonValue, String> {
289 let res = match self {
290 Cache::Redis(e) => e.get(key)?,
291 Cache::None => return Err("error".to_string()),
292 };
293 Ok(res)
294 }
295
296
297 pub fn set_add(&mut self, key: &str, value: JsonValue, expiry_s: i64) -> Result<bool, String> {
298 let res = match self {
299 Cache::Redis(e) => e.set_add(key, value, expiry_s)?,
300 Cache::None => return Err("error".to_string()),
301 };
302 Ok(res)
303 }
304 pub fn set_count(&mut self, key: &str) -> Result<usize, String> {
305 let res = match self {
306 Cache::Redis(e) => e.set_count(key)?,
307 Cache::None => return Err("error".to_string()),
308 };
309 Ok(res)
310 }
311 pub fn set_get(&mut self, key: &str) -> Result<JsonValue, String> {
312 let res = match self {
313 Cache::Redis(e) => e.set_get(key)?,
314 Cache::None => return Err("error".to_string()),
315 };
316 Ok(res)
317 }
318
319 pub fn set_delete(&mut self, key: &str, value: JsonValue) -> Result<bool, String> {
320 Ok(match self {
321 Cache::Redis(e) => e.set_delete(key, value)?,
322 Cache::None => return Err("error".to_string()),
323 })
324 }
325
326 pub fn set_get_sinter(&mut self, keys: Vec<&str>) -> Result<JsonValue, String> {
327 Ok(match self {
328 Cache::Redis(e) => e.set_get_sinter(keys)?,
329 Cache::None => return Err("error".to_string()),
330 })
331 }
332
333 pub fn set_get_sunion(&mut self, keys: Vec<&str>) -> Result<JsonValue, String> {
334 Ok(match self {
335 Cache::Redis(e) => e.set_get_sunion(keys)?,
336 Cache::None => return Err("error".to_string()),
337 })
338 }
339
340 pub fn list_add(&mut self, key: &str, value: JsonValue, expiry_s: i64) -> Result<bool, String> {
341 Ok(match self {
342 Cache::Redis(e) => e.list_add(key, value, expiry_s)?,
343 Cache::None => return Err("error".to_string()),
344 })
345 }
346
347 pub fn list_del(&mut self, key: &str, value: JsonValue) -> Result<bool, String> {
348 Ok(match self {
349 Cache::Redis(e) => e.list_del(key, value)?,
350 Cache::None => return Err("error".to_string()),
351 })
352 }
353
354 pub fn list_lpush(&mut self, key: &str, value: JsonValue, expiry_s: i64) -> Result<bool, String> {
356 Ok(match self {
357 Cache::Redis(e) => e.list_lpush(key, value, expiry_s)?,
358 Cache::None => return Err("Redis连接未初始化".to_string()),
359 })
360 }
361
362 pub fn list_rpush(&mut self, key: &str, value: JsonValue, expiry_s: i64) -> Result<bool, String> {
364 Ok(match self {
365 Cache::Redis(e) => e.list_rpush(key, value, expiry_s)?,
366 Cache::None => return Err("Redis连接未初始化".to_string()),
367 })
368 }
369
370 pub fn list_lpop(&mut self, key: &str, count: usize) -> Result<Vec<JsonValue>, String> {
372 Ok(match self {
373 Cache::Redis(e) => e.list_lpop(key, count)?,
374 Cache::None => return Err("Redis连接未初始化".to_string()),
375 })
376 }
377
378 pub fn list_rpop(&mut self, key: &str, count: usize) -> Result<Vec<JsonValue>, String> {
380 Ok(match self {
381 Cache::Redis(e) => e.list_rpop(key, count)?,
382 Cache::None => return Err("Redis连接未初始化".to_string()),
383 })
384 }
385
386 pub fn list_len(&mut self, key: &str) -> Result<usize, String> {
388 Ok(match self {
389 Cache::Redis(e) => e.list_len(key)?,
390 Cache::None => return Err("Redis连接未初始化".to_string()),
391 })
392 }
393
394 pub fn list_range(&mut self, key: &str, start: isize, stop: isize) -> Result<Vec<JsonValue>, String> {
396 Ok(match self {
397 Cache::Redis(e) => e.list_range(key, start, stop)?,
398 Cache::None => return Err("Redis连接未初始化".to_string()),
399 })
400 }
401
402 pub fn list_all(&mut self, key: &str) -> Result<Vec<JsonValue>, String> {
404 Ok(match self {
405 Cache::Redis(e) => e.list_all(key)?,
406 Cache::None => return Err("Redis连接未初始化".to_string()),
407 })
408 }
409
410 pub fn list_get(&mut self, key: &str, index: isize) -> Result<JsonValue, String> {
412 Ok(match self {
413 Cache::Redis(e) => e.list_get(key, index)?,
414 Cache::None => return Err("Redis连接未初始化".to_string()),
415 })
416 }
417
418 pub fn list_trim(&mut self, key: &str, start: isize, stop: isize) -> Result<bool, String> {
420 Ok(match self {
421 Cache::Redis(e) => e.list_trim(key, start, stop)?,
422 Cache::None => return Err("Redis连接未初始化".to_string()),
423 })
424 }
425
426 pub fn list_set(&mut self, key: &str, index: isize, value: JsonValue) -> Result<bool, String> {
428 Ok(match self {
429 Cache::Redis(e) => e.list_set(key, index, value)?,
430 Cache::None => return Err("Redis连接未初始化".to_string()),
431 })
432 }
433
434 pub fn list_remove(&mut self, key: &str, value: JsonValue, count: isize) -> Result<isize, String> {
436 Ok(match self {
437 Cache::Redis(e) => e.list_remove(key, value, count)?,
438 Cache::None => return Err("Redis连接未初始化".to_string()),
439 })
440 }
441
442
443 pub fn hash_get(&mut self, key: &str) -> Result<JsonValue, String> {
445 let res = match self {
446 Cache::Redis(e) => e.hash_get(key)?,
447 Cache::None => return Err("error".to_string()),
448 };
449 Ok(res)
450 }
451 pub fn hash_add(&mut self, key: &str, field: &str, value: JsonValue) -> Result<bool, String> {
453 Ok(match self {
454 Cache::Redis(e) => e.hash_add(key, field, value)?,
455 Cache::None => return Err("error".to_string()),
456 })
457 }
458 pub fn hash_get_field_value(&mut self, key: &str, field: &str) -> Result<JsonValue, String> {
460 let res = match self {
461 Cache::Redis(e) => e.hash_get_field_value(key, field)?,
462 Cache::None => return Err("error".to_string()),
463 };
464 Ok(res)
465 }
466 pub fn hash_get_fields(&mut self, key: &str) -> Result<JsonValue, String> {
468 Ok(match self {
469 Cache::Redis(e) => e.hash_get_fields(key)?,
470 Cache::None => return Err("error".to_string()),
471 })
472 }
473 pub fn hash_delete(&mut self, key: &str, field: &str) -> Result<bool, String> {
474 Ok(match self {
475 Cache::Redis(e) => e.hash_delete(key, field)?,
476 Cache::None => return Err("error".to_string()),
477 })
478 }
479 pub fn hash_get_values(&mut self, key: &str) -> Result<JsonValue, String> {
481 Ok(match self {
482 Cache::Redis(e) => e.hash_get_values(key)?,
483 Cache::None => return Err("error".to_string()),
484 })
485 }
486
487 pub fn geo_add(&mut self, key: &str, longitude: f64, latitude: f64, value: JsonValue) -> Result<bool, String> {
488 Ok(match self {
489 Cache::Redis(e) => e.geo_add(key, longitude, latitude, value)?,
490 Cache::None => return Err("error".to_string()),
491 })
492 }
493
494
495 pub fn geo_get(&mut self, key: &str, value: JsonValue) -> Result<JsonValue, String> {
496 Ok(match self {
497 Cache::Redis(e) => e.geo_get(key, value)?,
498 Cache::None => return Err("error".to_string()),
499 })
500 }
501
502 pub fn geo_dist(&mut self, key: &str, value1: JsonValue, value2: JsonValue) -> Result<JsonValue, String> {
503 Ok(match self {
504 Cache::Redis(e) => e.geo_dist(key, value1, value2)?,
505 Cache::None => return Err("error".to_string()),
506 })
507 }
508
509 pub fn geo_radius(&mut self, key: &str, value: JsonValue, radius: &str) -> Result<JsonValue, String> {
510 Ok(match self {
511 Cache::Redis(e) => e.geo_radius(key, value, radius)?,
512 Cache::None => return Err("error".to_string()),
513 })
514 }
515 pub fn stream_add(&mut self, key: &str, msg_id: &str, field: &str, value: JsonValue) -> Result<String, String> {
516 match self {
517 Cache::Redis(e) => e.stream_add(key, msg_id, field, value),
518 Cache::None => Err("error".to_string()),
519 }
520 }
521 pub fn stream_count(&mut self, key: &str) -> Result<usize, String> {
522 Ok(match self {
523 Cache::Redis(e) => e.stream_count(key)?,
524 Cache::None => return Err("error".to_string()),
525 })
526 }
527
528 pub fn stream_get(&mut self, key: &str) -> Result<JsonValue, String> {
529 Ok(match self {
530 Cache::Redis(e) => e.stream_get(key)?,
531 Cache::None => return Err("error".to_string()),
532 })
533 }
534
535
536 pub fn stream_del(&mut self, key: &str, id: &str) -> Result<bool, String> {
537 Ok(match self {
538 Cache::Redis(e) => e.stream_del(key, id)?,
539 Cache::None => return Err("error".to_string()),
540 })
541 }
542
543 pub fn stream_group_create(&mut self, key: &str, group: &str) -> Result<bool, String> {
544 Ok(match self {
545 Cache::Redis(e) => e.stream_group_create(key, group)?,
546 Cache::None => return Err("error".to_string()),
547 })
548 }
549
550 pub fn stream_group_add_user(&mut self, key: &str, group: &str, user: &str) -> Result<bool, String> {
551 Ok(match self {
552 Cache::Redis(e) => e.stream_group_add_user(key, group, user)?,
553 Cache::None => return Err("error".to_string()),
554 })
555 }
556
557 pub fn stream_group_del_user(&mut self, key: &str, group: &str, user: &str) -> Result<bool, String> {
558 Ok(match self {
559 Cache::Redis(e) => e.stream_group_del_user(key, group, user)?,
560 Cache::None => return Err("error".to_string()),
561 })
562 }
563
564 pub fn stream_group_del(&mut self, key: &str, group: &str) -> Result<bool, String> {
565 Ok(match self {
566 Cache::Redis(e) => e.stream_group_del(key, group)?,
567 Cache::None => return Err("error".to_string()),
568 })
569 }
570
571 pub fn stream_group_msg(&mut self, key: &str, group: &str, user: &str) -> Result<JsonValue, String> {
572 Ok(match self {
573 Cache::Redis(e) => e.stream_group_msg(key, group, user)?,
574 Cache::None => return Err("error".to_string()),
575 })
576 }
577
578 pub fn stream_get_group(&mut self, key: &str, group: &str) -> Result<bool, String> {
579 Ok(match self {
580 Cache::Redis(e) => e.stream_get_group(key, group)?,
581 Cache::None => return Err("error".to_string()),
582 })
583 }
584
585 pub fn stream_get_stream(&mut self, key: &str) -> Result<JsonValue, String> {
586 Ok(match self {
587 Cache::Redis(e) => e.stream_get_stream(key)?,
588 Cache::None => return Err("error".to_string()),
589 })
590 }
591
592 pub fn subscribe(&mut self, key: &str, tx: Sender<JsonValue>) -> Result<(), String> {
593 let _: () = match self {
594 Cache::Redis(e) => e.subscribe(key, tx)?,
595 Cache::None => return Err("error".to_string()),
596 };
597 Ok(())
598 }
599 pub fn publish(&mut self, key: &str, value: JsonValue) -> Result<bool, String> {
600 Ok(match self {
601 Cache::Redis(e) => e.publish(key, value)?,
602 Cache::None => return Err("error".to_string()),
603 })
604 }
605}