From 1813e6a7b27bc1b364120313a02f8b94e10e69fa Mon Sep 17 00:00:00 2001 From: Orne Brocaar Date: Thu, 22 Dec 2022 21:04:40 +0000 Subject: [PATCH] Fix redis_key implementation. --- chirpstack/src/storage/mod.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/chirpstack/src/storage/mod.rs b/chirpstack/src/storage/mod.rs index e004fa60..8825bdd9 100644 --- a/chirpstack/src/storage/mod.rs +++ b/chirpstack/src/storage/mod.rs @@ -38,6 +38,7 @@ pub type PgPoolConnection = PooledConnection>; lazy_static! { static ref PG_POOL: RwLock> = RwLock::new(None); static ref REDIS_POOL: RwLock> = RwLock::new(None); + static ref REDIS_PREFIX: RwLock = RwLock::new("".to_string()); } pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations"); @@ -210,6 +211,11 @@ pub async fn setup() -> Result<()> { set_redis_pool(RedisPool::Client(pool)); } + if !conf.redis.key_prefix.is_empty() { + info!(prefix = %conf.redis.key_prefix, "Setting Redis prefix"); + *REDIS_PREFIX.write().unwrap() = conf.redis.key_prefix.clone(); + } + Ok(()) } @@ -244,7 +250,8 @@ pub fn set_redis_pool(p: RedisPool) { } pub fn redis_key(s: String) -> String { - s + let prefix = REDIS_PREFIX.read().unwrap(); + format!("{}{}", prefix, s) } #[cfg(test)] @@ -264,3 +271,23 @@ pub async fn reset_redis() -> Result<()> { redis::cmd("FLUSHALL").query(&mut *c)?; Ok(()) } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_prefix_no_prefix() { + *REDIS_PREFIX.write().unwrap() = "".to_string(); + assert_eq!("lora:test:key", redis_key("lora:test:key".to_string())); + } + + #[test] + fn test_prefix() { + *REDIS_PREFIX.write().unwrap() = "foobar:".to_string(); + assert_eq!( + "foobar:lora:test:key", + redis_key("lora:test:key".to_string()) + ); + } +}