-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from jjcomer/redis
Add redis caching and ability to configure db caches
- Loading branch information
Showing
10 changed files
with
467 additions
and
305 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ doc = false | |
|
||
[package] | ||
name = 'hogan' | ||
version = '0.13.1' | ||
version = '0.13.2' | ||
authors = [ | ||
'Jonathan Morley <[email protected]>', | ||
'Josh Comer <[email protected]>', | ||
|
@@ -27,6 +27,7 @@ log = '0.4' | |
lru_time_cache = '0.11' | ||
lru = '0.12' | ||
parking_lot = '0.12' | ||
redis = '0.23' | ||
riker = '0.4' | ||
riker-patterns = '0.4' | ||
serde_derive = '1.0' | ||
|
@@ -38,11 +39,12 @@ tempfile = '3' | |
thiserror = '1.0' | ||
url = '2' | ||
walkdir = '2' | ||
which = '4.4' | ||
which = '5.0' | ||
zip = '0.6' | ||
|
||
|
||
[dependencies.rusqlite] | ||
version = '0.29' | ||
version = '0.30' | ||
features = ['bundled'] | ||
|
||
[dependencies.git2] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ pub mod cache; | |
pub mod lru; | ||
pub mod multi; | ||
pub mod sqlite; | ||
pub mod redis; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use super::cache; | ||
use crate::storage::cache::Cache; | ||
use anyhow::Result; | ||
use hogan::config::Environment; | ||
use hogan::config::EnvironmentDescription; | ||
use redis; | ||
|
||
use redis::Commands as _; | ||
use std::sync::Arc; | ||
|
||
pub struct RedisCache { | ||
client: redis::Client, | ||
id: String, | ||
ttl: usize, | ||
} | ||
|
||
impl RedisCache { | ||
pub fn new(id: &str, connection_string: &str, ttl: usize) -> Result<Self> { | ||
let client = redis::Client::open(connection_string)?; | ||
Ok(RedisCache { | ||
client, | ||
id: id.to_owned(), | ||
ttl, | ||
}) | ||
} | ||
} | ||
|
||
impl Cache for RedisCache { | ||
fn id(&self) -> &str { | ||
&self.id | ||
} | ||
|
||
fn clean(&self, _max_age: usize) -> Result<()> { | ||
Ok(()) | ||
} | ||
|
||
fn read_env(&self, env: &str, sha: &str) -> Result<Option<Arc<Environment>>> { | ||
let key = gen_env_key(sha, env); | ||
let mut connection = self.client.get_connection()?; | ||
let serialized_data: Vec<u8> = match connection.get(&key) { | ||
Ok(data) => { | ||
connection.expire(&key, self.ttl)?; | ||
data | ||
} | ||
Err(_e) => return Ok(None), | ||
}; | ||
|
||
let data = cache::deserialize_env(serialized_data)?; | ||
|
||
Ok(Some(Arc::new(data))) | ||
} | ||
|
||
fn write_env(&self, env: &str, sha: &str, data: &Environment) -> Result<()> { | ||
let key = gen_env_key(sha, env); | ||
let serialized_data = cache::serialize_env(data)?; | ||
let mut connection = self.client.get_connection()?; | ||
|
||
let opts = redis::SetOptions::default().with_expiration(redis::SetExpiry::EX(self.ttl)); | ||
|
||
connection.set_options(&key, serialized_data, opts)?; | ||
debug!("Wrote env {} to redis.", key); | ||
Ok(()) | ||
} | ||
|
||
fn read_env_listing(&self, sha: &str) -> Result<Option<Arc<Vec<EnvironmentDescription>>>> { | ||
let key = gen_env_listing_key(sha); | ||
let mut connection = self.client.get_connection()?; | ||
let serialized_data: Vec<u8> = match connection.get(&key) { | ||
Ok(data) => { | ||
connection.expire(&key, self.ttl)?; | ||
data | ||
} | ||
Err(_e) => return Ok(None), | ||
}; | ||
let data = cache::deserialize_env_listing(serialized_data)?; | ||
Ok(Some(Arc::new(data))) | ||
} | ||
|
||
fn write_env_listing(&self, sha: &str, data: &[EnvironmentDescription]) -> Result<()> { | ||
let key = gen_env_listing_key(sha); | ||
let serialized_data = cache::serialize_env_listing(data)?; | ||
let mut connection = self.client.get_connection()?; | ||
|
||
let opts = redis::SetOptions::default().with_expiration(redis::SetExpiry::EX(self.ttl)); | ||
connection.set_options(key, serialized_data, opts)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn gen_env_key(sha: &str, env: &str) -> String { | ||
format!("env::{}::{}", sha, env) | ||
} | ||
|
||
fn gen_env_listing_key(sha: &str) -> String { | ||
format!("listing::{}", sha) | ||
} |
Oops, something went wrong.