Skip to content

Commit

Permalink
Add: Function for set and get the nvtcache key. This should contain t…
Browse files Browse the repository at this point in the history
…he feed version

Also a function for resetting the nvtcache (delete and release).
  • Loading branch information
jjnicola committed Nov 23, 2022
1 parent 23a541e commit b859521
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
18 changes: 18 additions & 0 deletions rust/nvtcache/src/nvtcache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,23 @@ pub mod nvtcache {
pub fn is_init(&mut self) -> bool {
self.init == true
}

/// Reset the NVT Cache and release the redis namespace
pub fn reset(&mut self) -> Result<()> {
let _ = self.cache.delete_namespace()?;
Ok(())
}

/// Set the key nvtcache
pub fn set_nvtcache_version(&mut self, feed_version: &str) -> Result<()> {
let _ = self.cache.redis_set_key("nvticache", feed_version)?;
Ok(())
}

/// Get the key nvtcache, which has the feed version
pub fn get_nvtcache_version(&mut self) -> Result<String> {
let version = self.cache.redis_get_key("nvticache")?;
Ok(version)
}
}
}
8 changes: 3 additions & 5 deletions rust/nvtcache/src/redisconnector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,9 @@ pub mod redisconnector {
Ok(())
}

pub fn redis_get_key(&mut self, key: &str) -> String {
match self.kb.get(key) {
Ok(x) => return x,
Err(e) => e.to_string(),
}
pub fn redis_get_key(&mut self, key: &str) -> Result<String> {
let ret = self.kb.get(key)?;
Ok(ret)
}
}
}
21 changes: 18 additions & 3 deletions rust/nvtcache/tests/nvtcache_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ mod test {
Ok(_) => println!("Key {} set with {}", key, val),
Err(e) => println!("Error:{}", e),
}
println!("{}", nvtcache.cache.redis_get_key(key));
let res = nvtcache.cache.redis_get_key(key);
match res {
Ok(k) => println!("{}", k),
Err(_) => println!("Error"),
}

key = "key string value";
let val = "Some string";
Expand All @@ -46,8 +50,19 @@ mod test {
Err(e) => println!("Error:{}", e),
}

println!("{}", nvtcache.cache.redis_get_key(key));
let res = nvtcache.cache.redis_get_key(key);
match res {
Ok(k) => println!("{}", k),
Err(_) => println!("Error"),
}

let _ = nvtcache.set_nvtcache_version("202212101125");
let version = nvtcache.get_nvtcache_version();
match version {
Ok(v) => assert_eq!(v, "202212101125"),
Err(_) => println!("Error"),
}

let _ = nvtcache.cache.delete_namespace();
let _ = nvtcache.reset();
}
}

0 comments on commit b859521

Please sign in to comment.