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