// Copyright 2022 tison . // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use anyhow::Result; use aredis::{command::SetOption, Client}; use crate::Utf8String; struct Cache { client: Client, } impl Cache { pub fn new(client: Client) -> Self { Cache { client } } pub async fn set(&mut self, key: In0, value: In1) -> Result<()> where In0: Into>, In1: Into>, { self.client.set(key, value, SetOption::default()).await?; Ok(()) } pub async fn get(&mut self, key: In) -> Result> where In: Into>, Out: From>, { Ok(self.client.get(key).await?) } pub async fn update(&mut self, key: In0, value: In1) -> Result> where In0: Into>, In1: Into>, Out: From>, { let option = SetOption::default(); Ok(self.client.get_set(key, value, option).await?) } } #[tokio::test] #[serial_test::serial] async fn test_cache() -> Result<()> { let client = crate::client().await?; let mut cache = Cache::new(client); let key = "greeting-page"; let first = "

hello world

"; let second = "

good morning

"; let got: Option = cache.get(key).await?; assert!(got.is_none()); cache.set(key, first).await?; let got: Option = cache.get(key).await?; assert!(got.is_some()); assert_eq!(got.unwrap(), first.into()); let got: Option = cache.update(key, second).await?; assert!(got.is_some()); assert_eq!(got.unwrap(), first.into()); let got: Option = cache.get(key).await?; assert!(got.is_some()); assert_eq!(got.unwrap(), second.into()); Ok(()) }