Spencerjibz commited on
Commit
c762f9c
·
1 Parent(s): c25cd9c

use redis pipeline to set multiple values at once

Browse files
Files changed (1) hide show
  1. src/cache/redis_cacher.rs +13 -7
src/cache/redis_cacher.rs CHANGED
@@ -118,14 +118,18 @@ impl RedisCache {
118
  /// on a failure.
119
  pub async fn cache_json(
120
  &mut self,
121
- json_results: &str,
122
- key: &str,
123
  ) -> Result<(), Report<CacheError>> {
124
  self.current_connection = Default::default();
 
125
 
126
- let mut result: Result<(), RedisError> = self.connection_pool
127
- [self.current_connection as usize]
128
- .set_ex(key, json_results, self.cache_ttl.into())
 
 
 
129
  .await;
130
 
131
  // Code to check whether the current connection being used is dropped with connection error
@@ -145,8 +149,10 @@ impl RedisCache {
145
  CacheError::PoolExhaustionWithConnectionDropError,
146
  ));
147
  }
148
- result = self.connection_pool[self.current_connection as usize]
149
- .set_ex(key, json_results, 60)
 
 
150
  .await;
151
  continue;
152
  }
 
118
  /// on a failure.
119
  pub async fn cache_json(
120
  &mut self,
121
+ json_results: impl Iterator<Item = String>,
122
+ keys: impl Iterator<Item = String>,
123
  ) -> Result<(), Report<CacheError>> {
124
  self.current_connection = Default::default();
125
+ let mut pipeline = redis::Pipeline::with_capacity(3);
126
 
127
+ for (key, json_result) in keys.zip(json_results) {
128
+ pipeline.set_ex(key, json_result, self.cache_ttl.into());
129
+ }
130
+
131
+ let mut result: Result<(), RedisError> = pipeline
132
+ .query_async(&mut self.connection_pool[self.current_connection as usize])
133
  .await;
134
 
135
  // Code to check whether the current connection being used is dropped with connection error
 
149
  CacheError::PoolExhaustionWithConnectionDropError,
150
  ));
151
  }
152
+ result = pipeline
153
+ .query_async(
154
+ &mut self.connection_pool[self.current_connection as usize],
155
+ )
156
  .await;
157
  continue;
158
  }