Skip to content
Snippets Groups Projects
Verified Commit 933870cd authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

[Cache] Use RwLock instead of Mutex

parent 56728236
No related branches found
No related tags found
No related merge requests found
...@@ -17,7 +17,7 @@ use crate::BASE_NAME; ...@@ -17,7 +17,7 @@ use crate::BASE_NAME;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::{Mutex, MutexGuard}; use std::sync::{RwLock, RwLockReadGuard};
use libc::{geteuid, seteuid, getpwnam, uid_t}; use libc::{geteuid, seteuid, getpwnam, uid_t};
use std::ffi::{CStr, CString}; use std::ffi::{CStr, CString};
...@@ -144,7 +144,7 @@ impl Cache { ...@@ -144,7 +144,7 @@ impl Cache {
} }
} }
pub fn load_user_token(&mut self, owner: &String) -> Option<&BasicTokenResponse> { pub fn load_user_token(&self, owner: &String) -> Option<&BasicTokenResponse> {
if !self.user_tokens.contains_key(owner) { if !self.user_tokens.contains_key(owner) {
debug!("No token for {} in memory, trying to load from file", owner); debug!("No token for {} in memory, trying to load from file", owner);
...@@ -160,7 +160,7 @@ impl Cache { ...@@ -160,7 +160,7 @@ impl Cache {
match new_token { match new_token {
Some(t) => { Some(t) => {
self.user_tokens.insert(owner.to_string(), t); CACHE.write().unwrap().user_tokens.insert(owner.to_string(), t);
self.user_tokens.get(owner) self.user_tokens.get(owner)
}, },
None => None None => None
...@@ -171,8 +171,8 @@ impl Cache { ...@@ -171,8 +171,8 @@ impl Cache {
} }
} }
pub fn save_user_token(&mut self, owner: &String, token: BasicTokenResponse) -> Result<(), io::Error> { pub fn save_user_token(&self, owner: &String, token: BasicTokenResponse) -> Result<(), io::Error> {
self.user_tokens.insert(owner.to_string(), token.clone()); CACHE.write().unwrap().user_tokens.insert(owner.to_string(), token.clone());
debug!("Saved token for {} in memory", owner); debug!("Saved token for {} in memory", owner);
// Try to write user's token cache file // Try to write user's token cache file
...@@ -190,8 +190,8 @@ impl Cache { ...@@ -190,8 +190,8 @@ impl Cache {
return res; return res;
} }
pub fn delete_user_token(&mut self, owner: &String) { pub fn delete_user_token(&self, owner: &String) {
self.user_tokens.remove(owner); CACHE.write().unwrap().user_tokens.remove(owner);
debug!("Token for {} removed from memory", owner); debug!("Token for {} removed from memory", owner);
// Try to remove user's token cache file // Try to remove user's token cache file
...@@ -227,9 +227,9 @@ fn save_json<O: Serialize>(path: PathBuf, obj: O) -> Result<(), io::Error> { ...@@ -227,9 +227,9 @@ fn save_json<O: Serialize>(path: PathBuf, obj: O) -> Result<(), io::Error> {
} }
lazy_static! { lazy_static! {
static ref CACHE: Mutex<Cache> = Mutex::new(Cache::new()); static ref CACHE: RwLock<Cache> = RwLock::new(Cache::new());
} }
pub fn get_cache() -> MutexGuard<'static, Cache> { pub fn get_cache() -> RwLockReadGuard<'static, Cache> {
CACHE.lock().unwrap() CACHE.read().unwrap()
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment