Spaces:
Runtime error
Runtime error
✨ Config option to customize the cache invalidation/expiry time (#403)
Browse files- src/cache/cacher.rs +3 -4
- src/cache/redis_cacher.rs +5 -1
- src/config/parser.rs +18 -0
- websurfx/config.lua +1 -1
src/cache/cacher.rs
CHANGED
@@ -79,7 +79,7 @@ impl Cacher for RedisCache {
|
|
79 |
"Initialising redis cache. Listening to {}",
|
80 |
&config.redis_url
|
81 |
);
|
82 |
-
RedisCache::new(&config.redis_url, 5)
|
83 |
.await
|
84 |
.expect("Redis cache configured")
|
85 |
}
|
@@ -113,13 +113,12 @@ pub struct InMemoryCache {
|
|
113 |
#[cfg(feature = "memory-cache")]
|
114 |
#[async_trait::async_trait]
|
115 |
impl Cacher for InMemoryCache {
|
116 |
-
async fn build(
|
117 |
log::info!("Initialising in-memory cache");
|
118 |
|
119 |
InMemoryCache {
|
120 |
cache: MokaCache::builder()
|
121 |
-
.
|
122 |
-
.time_to_live(Duration::from_secs(60))
|
123 |
.build(),
|
124 |
}
|
125 |
}
|
|
|
79 |
"Initialising redis cache. Listening to {}",
|
80 |
&config.redis_url
|
81 |
);
|
82 |
+
RedisCache::new(&config.redis_url, 5, config.cache_expiry_time)
|
83 |
.await
|
84 |
.expect("Redis cache configured")
|
85 |
}
|
|
|
113 |
#[cfg(feature = "memory-cache")]
|
114 |
#[async_trait::async_trait]
|
115 |
impl Cacher for InMemoryCache {
|
116 |
+
async fn build(config: &Config) -> Self {
|
117 |
log::info!("Initialising in-memory cache");
|
118 |
|
119 |
InMemoryCache {
|
120 |
cache: MokaCache::builder()
|
121 |
+
.time_to_live(Duration::from_secs(config.cache_expiry_time.into()))
|
|
|
122 |
.build(),
|
123 |
}
|
124 |
}
|
src/cache/redis_cacher.rs
CHANGED
@@ -18,6 +18,8 @@ pub struct RedisCache {
|
|
18 |
pool_size: u8,
|
19 |
/// It stores the index of which connection is being used at the moment.
|
20 |
current_connection: u8,
|
|
|
|
|
21 |
}
|
22 |
|
23 |
impl RedisCache {
|
@@ -36,6 +38,7 @@ impl RedisCache {
|
|
36 |
pub async fn new(
|
37 |
redis_connection_url: &str,
|
38 |
pool_size: u8,
|
|
|
39 |
) -> Result<Self, Box<dyn std::error::Error>> {
|
40 |
let client = Client::open(redis_connection_url)?;
|
41 |
let mut tasks: Vec<_> = Vec::new();
|
@@ -48,6 +51,7 @@ impl RedisCache {
|
|
48 |
connection_pool: try_join_all(tasks).await?,
|
49 |
pool_size,
|
50 |
current_connection: Default::default(),
|
|
|
51 |
};
|
52 |
Ok(redis_cache)
|
53 |
}
|
@@ -121,7 +125,7 @@ impl RedisCache {
|
|
121 |
|
122 |
let mut result: Result<(), RedisError> = self.connection_pool
|
123 |
[self.current_connection as usize]
|
124 |
-
.set_ex(key, json_results,
|
125 |
.await;
|
126 |
|
127 |
// Code to check whether the current connection being used is dropped with connection error
|
|
|
18 |
pool_size: u8,
|
19 |
/// It stores the index of which connection is being used at the moment.
|
20 |
current_connection: u8,
|
21 |
+
/// It stores the max TTL for keys.
|
22 |
+
cache_ttl: u16,
|
23 |
}
|
24 |
|
25 |
impl RedisCache {
|
|
|
38 |
pub async fn new(
|
39 |
redis_connection_url: &str,
|
40 |
pool_size: u8,
|
41 |
+
cache_ttl: u16,
|
42 |
) -> Result<Self, Box<dyn std::error::Error>> {
|
43 |
let client = Client::open(redis_connection_url)?;
|
44 |
let mut tasks: Vec<_> = Vec::new();
|
|
|
51 |
connection_pool: try_join_all(tasks).await?,
|
52 |
pool_size,
|
53 |
current_connection: Default::default(),
|
54 |
+
cache_ttl,
|
55 |
};
|
56 |
Ok(redis_cache)
|
57 |
}
|
|
|
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
|
src/config/parser.rs
CHANGED
@@ -21,6 +21,9 @@ pub struct Config {
|
|
21 |
/// It stores the redis connection url address on which the redis
|
22 |
/// client should connect.
|
23 |
pub redis_url: String,
|
|
|
|
|
|
|
24 |
/// It stores the option to whether enable or disable production use.
|
25 |
pub aggregator: AggregatorConfig,
|
26 |
/// It stores the option to whether enable or disable logs.
|
@@ -93,6 +96,19 @@ impl Config {
|
|
93 |
}
|
94 |
};
|
95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
Ok(Config {
|
97 |
port: globals.get::<_, u16>("port")?,
|
98 |
binding_ip: globals.get::<_, String>("binding_ip")?,
|
@@ -116,6 +132,8 @@ impl Config {
|
|
116 |
time_limit: rate_limiter["time_limit"],
|
117 |
},
|
118 |
safe_search,
|
|
|
|
|
119 |
})
|
120 |
}
|
121 |
}
|
|
|
21 |
/// It stores the redis connection url address on which the redis
|
22 |
/// client should connect.
|
23 |
pub redis_url: String,
|
24 |
+
#[cfg(any(feature = "redis-cache", feature = "memory-cache"))]
|
25 |
+
/// It stores the max TTL for search results in cache.
|
26 |
+
pub cache_expiry_time: u16,
|
27 |
/// It stores the option to whether enable or disable production use.
|
28 |
pub aggregator: AggregatorConfig,
|
29 |
/// It stores the option to whether enable or disable logs.
|
|
|
96 |
}
|
97 |
};
|
98 |
|
99 |
+
#[cfg(any(feature = "redis-cache", feature = "memory-cache"))]
|
100 |
+
let parsed_cet = globals.get::<_, u16>("cache_expiry_time")?;
|
101 |
+
let cache_expiry_time = match parsed_cet {
|
102 |
+
0..=59 => {
|
103 |
+
log::error!(
|
104 |
+
"Config Error: The value of `cache_expiry_time` must be greater than 60"
|
105 |
+
);
|
106 |
+
log::error!("Falling back to using the value `60` for the option");
|
107 |
+
60
|
108 |
+
}
|
109 |
+
_ => parsed_cet,
|
110 |
+
};
|
111 |
+
|
112 |
Ok(Config {
|
113 |
port: globals.get::<_, u16>("port")?,
|
114 |
binding_ip: globals.get::<_, String>("binding_ip")?,
|
|
|
132 |
time_limit: rate_limiter["time_limit"],
|
133 |
},
|
134 |
safe_search,
|
135 |
+
#[cfg(any(feature = "redis-cache", feature = "memory-cache"))]
|
136 |
+
cache_expiry_time,
|
137 |
})
|
138 |
}
|
139 |
}
|
websurfx/config.lua
CHANGED
@@ -47,7 +47,7 @@ theme = "simple" -- the theme name which should be used for the website
|
|
47 |
|
48 |
-- ### Caching ###
|
49 |
redis_url = "redis://127.0.0.1:8082" -- redis connection url address on which the client should connect on.
|
50 |
-
|
51 |
-- ### Search Engines ###
|
52 |
upstream_search_engines = {
|
53 |
DuckDuckGo = true,
|
|
|
47 |
|
48 |
-- ### Caching ###
|
49 |
redis_url = "redis://127.0.0.1:8082" -- redis connection url address on which the client should connect on.
|
50 |
+
cache_expiry_time = 600 -- This option takes the expiry time of the search results (value in seconds and the value should be greater than or equal to 60 seconds).
|
51 |
-- ### Search Engines ###
|
52 |
upstream_search_engines = {
|
53 |
DuckDuckGo = true,
|