br_cache/
lib.rs

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    /// 配置文件模式
23    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    /// 非配置文件模式
42    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    /// 获取通道列表
63    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    /// 切换通道
77    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    /// 选择数据库
103    fn db(&mut self, db: i8) -> &mut Self;
104
105    /// 键 判断是否存在
106    fn key_exists(&mut self, key: &str) -> Result<bool, String>;
107    /// 键 删除
108    fn key_del(&mut self, key: &str) -> Result<bool, String>;
109    /// 键 以秒为单位返回 key 的剩余过期时间。
110    fn key_ttl(&mut self, key: &str) -> Result<i64, String>;
111
112    /// 键 设置到期时间
113    /// * key 键
114    /// * timestamp 到期时间戳 10位
115    fn key_set_expireat(&mut self, key: &str, timestamp: i64) -> Result<bool, String>;
116    /// 键 设置到期时间
117    /// * s 过期秒
118    fn key_set_seconds(&mut self, key: &str, s: i64) -> Result<bool, String>;
119    /// 键 删除到期时间
120    fn key_del_expire(&mut self, key: &str) -> Result<bool, String>;
121    /// 查询 KEY
122    /// * key 键 格式 * 或 *#### 模糊查询
123    fn key_query(&mut self, key: &str) -> Result<JsonValue, String>;
124
125    /// 设置缓存
126    ///
127    /// * expiration_date 过期时间 s 秒
128    fn add(&mut self, key: &str, value: JsonValue, expiration_date: u64) -> Result<bool, String>;
129    /// 获取缓存
130    fn get(&mut self, key: &str) -> Result<JsonValue, String>;
131
132    /// 集合 添加
133    fn set_add(&mut self, key: &str, value: JsonValue, expiry_s: i64) -> Result<bool, String>;
134    /// 集合 获取成员数量
135    fn set_count(&mut self, key: &str) -> Result<usize, String>;
136    /// 集合 获取
137    fn set_get(&mut self, key: &str) -> Result<JsonValue, String>;
138    /// 集合 删除
139    fn set_delete(&mut self, key: &str, value: JsonValue) -> Result<bool, String>;
140    /// 集合 获取交集
141    fn set_get_sinter(&mut self, keys: Vec<&str>) -> Result<JsonValue, String>;
142    /// 集合 获取并集
143    fn set_get_sunion(&mut self, keys: Vec<&str>) -> Result<JsonValue, String>;
144
145    /// 列表 添加
146    fn list_add(&mut self, key: &str, value: JsonValue, expiry_s: i64) -> Result<bool, String>;
147    /// 列表 删除元素
148    fn list_del(&mut self, key: &str, value: JsonValue) -> Result<bool, String>;
149    /// 从列表左侧添加元素
150    fn list_lpush(&mut self, key: &str, value: JsonValue, expiry_s: i64) -> Result<bool, String>;
151    /// 从列表右侧添加元素
152    fn list_rpush(&mut self, key: &str, value: JsonValue, expiry_s: i64) -> Result<bool, String>;
153    /// 从列表左侧弹出元素
154    /// count: 0 = 弹出1个(兼容旧版),>0 = 弹出指定数量
155    fn list_lpop(&mut self, key: &str, count: usize) -> Result<Vec<JsonValue>, String>;
156    /// 从列表右侧弹出元素
157    fn list_rpop(&mut self, key: &str, count: usize) -> Result<Vec<JsonValue>, String>;
158    /// 获取列表长度
159    fn list_len(&mut self, key: &str) -> Result<usize, String>;
160    /// 获取列表指定范围的元素
161    fn list_range(&mut self, key: &str, start: isize, stop: isize) -> Result<Vec<JsonValue>, String>;
162    /// 获取列表所有元素
163    fn list_all(&mut self, key: &str) -> Result<Vec<JsonValue>, String>;
164    /// 根据索引获取列表元素
165    fn list_get(&mut self, key: &str, index: isize) -> Result<JsonValue, String>;
166    /// 修剪列表,只保留指定范围内的元素
167    fn list_trim(&mut self, key: &str, start: isize, stop: isize) -> Result<bool, String>;
168    /// 设置指定索引位置的元素值
169    fn list_set(&mut self, key: &str, index: isize, value: JsonValue) -> Result<bool, String>;
170    /// 删除列表中指定值的元素(count=0表示删除所有匹配项)
171    fn list_remove(&mut self, key: &str, value: JsonValue, count: isize) -> Result<isize, String>;
172    /// 获取哈希值
173    fn hash_get(&mut self, key: &str) -> Result<JsonValue, String>;
174    /// 添加哈希
175    fn hash_add(&mut self, key: &str, field: &str, value: JsonValue) -> Result<bool, String>;
176    /// 获取哈希指定key的字段值
177    fn hash_get_field_value(&mut self, key: &str, field: &str) -> Result<JsonValue, String>;
178    /// 获取key下所有的field
179    fn hash_get_fields(&mut self, key: &str) -> Result<JsonValue, String>;
180    /// 删除哈希指定key的字段
181    fn hash_delete(&mut self, key: &str, field: &str) -> Result<bool, String>;
182    /// 获取key下所有的value
183    fn hash_get_values(&mut self, key: &str) -> Result<JsonValue, String>;
184    /// 地理 添加地理位置
185    fn geo_add(&mut self, key: &str, longitude: f64, latitude: f64, value: JsonValue) -> Result<bool, String>;
186    /// 地理 获取地理位置
187    fn geo_get(&mut self, key: &str, value: JsonValue) -> Result<JsonValue, String>;
188    /// 地理 获取两地之间的距离地理位置
189    fn geo_dist(&mut self, key: &str, value1: JsonValue, value2: JsonValue) -> Result<JsonValue, String>;
190    /// 地理 以给定的经纬度为中心, 返回键包含的位置元素当中, 与中心的距离不超过给定最大距离的所有位置元素
191    fn geo_radius(&mut self, key: &str, value: JsonValue, radius: &str) -> Result<JsonValue, String>;
192
193    /// 设置消息队列
194    fn stream_add(&mut self, key: &str, msg_id: &str, field: &str, value: JsonValue) -> Result<String, String>;
195    /// 获取消息长度
196    fn stream_count(&mut self, key: &str) -> Result<usize, String>;
197    /// 获取 过滤已删除的消息队列
198    fn stream_get(&mut self, key: &str) -> Result<JsonValue, String>;
199    /// 删除消息
200    fn stream_del(&mut self, key: &str, id: &str) -> Result<bool, String>;
201
202    /// 创建消费者组
203    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    /// 删除 消费者
207    fn stream_group_del(&mut self, key: &str, group: &str) -> Result<bool, String>;
208    /// 删除 消费者
209    fn stream_group_msg(&mut self, key: &str, group: &str, user: &str) -> Result<JsonValue, String>;
210    /// 获取组存在不
211    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    /// 订阅消息
216    fn subscribe(&mut self, key: &str, tx: Sender<JsonValue>) -> Result<(), String>;
217    /// 发布消息
218    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    /// 从列表左侧添加元素
355    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    /// 从列表右侧添加元素
363    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    /// 从列表左侧弹出多个元素
371    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    /// 从列表右侧弹出多个元素
379    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    /// 获取列表长度
387    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    /// 获取列表指定范围的元素
395    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    /// 获取列表所有元素
403    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    /// 根据索引获取列表元素(返回JsonValue)
411    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    /// 修剪列表,只保留指定范围内的元素
419    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    /// 设置指定索引位置的元素值
427    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    /// 删除列表中指定值的元素
435    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    /// 哈希 获取
444    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    /// 哈希 添加
452    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    /// 哈希-获取指定字段的值
459    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    /// 哈希-获取所有字段
467    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    /// 哈希-获取所有值
480    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}