-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathvalkey_utils.rs
306 lines (283 loc) · 10.4 KB
/
valkey_utils.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
use std::fmt::Debug;
use deadpool_redis::redis::aio::ConnectionLike;
use deadpool_redis::redis::cmd;
use deadpool_redis::redis::Arg;
use deadpool_redis::redis::AsyncCommands;
use deadpool_redis::redis::Cmd;
use deadpool_redis::redis::ErrorKind;
use deadpool_redis::redis::Pipeline;
use deadpool_redis::redis::RedisError;
use deadpool_redis::redis::RedisFuture;
use deadpool_redis::redis::ToRedisArgs;
use deadpool_redis::redis::Value;
use deadpool_redis::Config;
use deadpool_redis::Connection;
use deadpool_redis::Pool;
use deadpool_redis::PoolError;
use deadpool_redis::Runtime;
use futures::future;
use futures::FutureExt;
use serde::de::DeserializeOwned;
use serde::Serialize;
use tracing::{debug, span, trace, Level};
use url::Url;
use crate::error::Result;
pub enum ValkeyConnection {
Tokio(Connection),
NoCache,
}
fn no_cache_cmd_handler(cmd: &Cmd) -> std::result::Result<Value, RedisError> {
let cmd_name = cmd
.args_iter()
.next()
.ok_or((ErrorKind::ClientError, "missing a command instruction"))?;
let nb_keys = cmd.args_iter().skip(1).count();
match cmd_name {
Arg::Simple(cmd_name_bytes)
if cmd_name_bytes == "MGET".as_bytes()
|| cmd_name_bytes == "MSET".as_bytes()
|| nb_keys > 1 =>
{
Ok(Value::Array(vec![Value::Nil; nb_keys]))
},
Arg::Simple(_)
if nb_keys == 1 =>
{
Ok(Value::Nil)
},
Arg::Simple(cmd_name_bytes) if cmd_name_bytes == "PING".as_bytes() => Ok(Value::SimpleString("PONG".to_string())),
Arg::Simple(cmd_name_bytes) => unimplemented!(
"valkey command '{}' is not supported by editoast::valkey_utils::ValkeyConnection with '--no-cache'", String::from_utf8(cmd_name_bytes.to_vec())?
),
Arg::Cursor => unimplemented!(
"valkey cursor mode is not supported by editoast::valkey_utils::ValkeyConnection with '--no-cache'"
),
}
}
impl ConnectionLike for ValkeyConnection {
fn req_packed_command<'a>(&'a mut self, cmd: &'a Cmd) -> RedisFuture<'a, Value> {
match self {
ValkeyConnection::Tokio(connection) => connection.req_packed_command(cmd),
ValkeyConnection::NoCache => future::ready(no_cache_cmd_handler(cmd)).boxed(),
}
}
fn req_packed_commands<'a>(
&'a mut self,
cmd: &'a Pipeline,
offset: usize,
count: usize,
) -> RedisFuture<'a, Vec<Value>> {
match self {
ValkeyConnection::Tokio(connection) => {
connection.req_packed_commands(cmd, offset, count)
}
ValkeyConnection::NoCache => {
let responses = cmd
.cmd_iter()
.skip(offset)
.take(count)
.map(no_cache_cmd_handler)
.collect::<std::result::Result<_, RedisError>>();
future::ready(responses).boxed()
}
}
}
fn get_db(&self) -> i64 {
match self {
ValkeyConnection::Tokio(connection) => connection.get_db(),
ValkeyConnection::NoCache => 0,
}
}
}
impl ValkeyConnection {
/// Get a deserializable value from valkey
#[tracing::instrument(name = "cache:json_get", skip(self), err)]
pub async fn json_get<T: DeserializeOwned, K: Debug + ToRedisArgs + Send + Sync>(
&mut self,
key: K,
) -> Result<Option<T>> {
let value: Option<String> = self.get(key).await?;
match value {
Some(v) => match serde_json::from_str(&v) {
Ok(value) => Ok(value),
Err(_) => {
Err(RedisError::from((ErrorKind::TypeError, "Expected valid json")).into())
}
},
None => Ok(None),
}
}
/// Get a list of deserializable value from valkey
#[tracing::instrument(name = "cache:json_get_bulk", skip(self), err)]
pub async fn json_get_bulk<T: DeserializeOwned, K: Debug + ToRedisArgs + Send + Sync>(
&mut self,
keys: &[K],
) -> Result<Vec<Option<T>>> {
// Avoid mget to fail if keys is empty
if keys.is_empty() {
return Ok(vec![]);
}
let values: Vec<Option<String>> = self.mget(keys).await?;
values
.into_iter()
.map(|value| match value {
Some(v) => match serde_json::from_str::<T>(&v) {
Ok(value) => Ok(Some(value)),
Err(_) => {
Err(RedisError::from((ErrorKind::TypeError, "Expected valid json")).into())
}
},
None => Ok(None),
})
.collect()
}
/// Set a serializable value to valkey with expiry time
#[tracing::instrument(name = "cache:json_set", skip(self, value), err)]
pub async fn json_set<K: Debug + ToRedisArgs + Send + Sync, T: Serialize>(
&mut self,
key: K,
value: &T,
) -> Result<()> {
let str_value = match serde_json::to_string(value) {
Ok(value) => value,
Err(_) => {
return Err(RedisError::from((
ErrorKind::IoError,
"An error occurred serializing to json",
))
.into())
}
};
self.set(key, str_value).await?;
Ok(())
}
/// Set a list of serializable values to valkey
#[tracing::instrument(name = "cache:json_set_bulk", skip(self, items), err)]
pub async fn json_set_bulk<K: Debug + ToRedisArgs + Send + Sync, T: Serialize>(
&mut self,
items: &[(K, T)],
) -> Result<()> {
// Avoid mset to fail if keys is empty
if items.is_empty() {
return Ok(());
}
let serialized_items = items
.iter()
.map(|(key, value)| {
serde_json::to_string(value)
.map(|str_value| (key, str_value))
.map_err(|_| {
RedisError::from((
ErrorKind::IoError,
"An error occurred serializing to json",
))
.into()
})
})
.collect::<Result<Vec<_>>>()?;
self.mset(&serialized_items).await?;
Ok(())
}
/// Set a list of compressed serializable values to valkey
#[tracing::instrument(name = "cache:compressed_set_bulk", skip(self, items), err)]
pub async fn compressed_set_bulk<K: Debug + ToRedisArgs + Send + Sync, T: Serialize>(
&mut self,
items: &[(K, T)],
) -> Result<()> {
// Avoid mset to fail if keys is empty
if items.is_empty() {
return Ok(());
}
let compressed_items = span!(Level::INFO, "Compressing data").in_scope(|| {
items
.iter()
.map(|(key, value)| {
// Create a LZ4 encoder.
let mut encoder = lz4_flex::frame::FrameEncoder::new(Vec::new());
// Serialize the `value` into JSON format and write it to the encoder (which compresses it).
serde_json::to_writer(&mut encoder, value)?;
// Finalize the compression process and retrieve the compressed data.
let compressed_value = encoder.finish().map_err(|_| {
RedisError::from((
ErrorKind::IoError,
"An error occured compressing the value",
))
})?;
Ok((key, compressed_value))
})
.collect::<Result<Vec<_>>>()
})?;
// Store the compressed values using mset
span!(Level::INFO, "Sending items to Redis")
.in_scope(|| async move { self.mset(&compressed_items).await })
.await?;
Ok(())
}
/// Retrieves a list of compressed serialized values from Valkey, decompresses them, and deserializes the result.
#[tracing::instrument(name = "cache:compressed_get_bulk", skip(self), err)]
pub async fn compressed_get_bulk<K: Debug + ToRedisArgs + Send + Sync, T: DeserializeOwned>(
&mut self,
keys: &[K],
) -> Result<Vec<Option<T>>> {
// Avoid mget to fail if keys is empty
if keys.is_empty() {
return Ok(vec![]);
}
debug!(nb_keys = keys.len());
// Fetch the values from Redis
let values = span!(Level::INFO, "Fetching values from Redis")
.in_scope(|| async move { self.mget::<_, Vec<Option<Vec<u8>>>>(keys).await })
.await?;
// Decompress each value if it exists
span!(Level::INFO, "Decompressing data").in_scope(|| {
values
.into_iter()
.map(|value| match value {
Some(compressed_data) => {
let mut decoder = lz4_flex::frame::FrameDecoder::new(&compressed_data[..]);
let deserialized: T = serde_json::from_reader(&mut decoder)?;
Ok(Some(deserialized))
}
None => Ok(None),
})
.collect()
})
}
}
#[derive(Clone)]
pub enum ValkeyClient {
Tokio(Pool),
/// This doesn't cache anything. It has no backend.
NoCache,
}
#[derive(Clone)]
pub struct ValkeyConfig {
/// Disables caching. This should not be used in production.
pub no_cache: bool,
pub valkey_url: Url,
}
impl ValkeyClient {
pub fn new(valkey_config: ValkeyConfig) -> Result<ValkeyClient> {
if valkey_config.no_cache {
return Ok(ValkeyClient::NoCache);
}
Ok(ValkeyClient::Tokio(
Config::from_url(valkey_config.valkey_url)
.create_pool(Some(Runtime::Tokio1))
.unwrap(),
))
}
pub async fn get_connection(&self) -> std::result::Result<ValkeyConnection, PoolError> {
match self {
ValkeyClient::Tokio(pool) => Ok(ValkeyConnection::Tokio(pool.get().await?)),
ValkeyClient::NoCache => Ok(ValkeyConnection::NoCache),
}
}
#[tracing::instrument(skip_all)]
pub async fn ping_valkey(&self) -> anyhow::Result<()> {
let mut conn = self.get_connection().await?;
cmd("PING").query_async::<()>(&mut conn).await?;
trace!("Valkey ping successful");
Ok(())
}
}