4 releases

0.2.3 Apr 29, 2023
0.2.2 Apr 14, 2023
0.2.1 Apr 14, 2023
0.2.0 Apr 13, 2023
0.1.0 Apr 12, 2023

#2077 in Parser implementations

MIT license

17KB
311 lines

Object Cache

Build Version License: MIT

  • A simple cache util that takes any object reference and caches it in memory or in a file.
  • This crate uses Sqlite db to store objects.
  • It converts the object into JSON format and stores it for the given key.
  • The reverse happens when you retrieve the object.

Usage


async fn sample(){
    // build Cache with name (Could be In memory or File based cache)
    let cache = Cache::build(true, "obj_cache").await;

    let data = TestStruct {
        name: "dinesh".to_owned(),
        email: "dinesh.nuke.mars@gmail.com".to_owned(),
        ph_no: 9999999999u64,
    };

    // pass key and object
    cache.save_obj("TestData", &data).await.unwrap();

    // visualize the data
    cache.pretty_print_all_cache();

    // get object anywhere in the project using key
    let cached_data: TestStruct = cache.get_obj("TestData").await.unwrap();
}

Main Functions


Cache {
    pub async fn build(in_memory: bool, cache_file_name: &str) -> Self 
    pub async fn save_obj<T>(&self, key: &str, obj: &T) -> Result<(), CacheError
    pub async fn save_obj_if_not_exist<T>(&self, key: &str, obj: &T) -> Result<(), CacheError
    pub async fn get_obj<T>(&self, key: &str) -> Result<T, CacheError
    pub async fn get_all_objs(&self) -> Result<Vec<CacheData>, CacheError> 
    pub async fn pretty_print_all_cache(&self) 
    pub async fn clear_cache(&self) 
}

For more detailed usage instructions, visit the GitHub repository.

Contributing

We welcome contributions! Please see the contributing guidelines for more information.

License

This project is licensed under the terms of the MIT License. See the LICENSE file for more details.


Made with ♥ by dinesh.nuke.mars@gmail.comand contributors.

Dependencies

~34–49MB
~866K SLoC