pocketcache 0.0.1

A simple in-memory cache for Rust.
Documentation
  • Coverage
  • 40%
    6 out of 15 items documented6 out of 8 items with examples
  • Size
  • Source code size: 11.6 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.7 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • shooontan

pocketcache

Crates.io Test

A simple in-memory cache for Rust.

Install

[dependencies]
pocketcache = "0.0.1"

Usage

use pocketcache::cache::Cache;
use pocketcache::time::Expiration;

#[derive(Debug)]
struct Data {
  messages: Vec<String>,
}

fn main() {
  let expiration = Expiration::Hour(3);
  let mut cache = Cache::<Data>::new(expiration);

  // set
  cache.set(
    "fruit",
    Data {
      messages: vec![String::from("peach")],
    },
  );

  // get
  let fruit = cache.get("fruit");
  println!("{:#?}", fruit); // Some( Data { messages: { ["peach"] } } )

  // delete
  cache.delete("fruit");
  let fruit = cache.get("fruit");
  println!("{:#?}", fruit); // None
}

API

Expiration

use pocketcache::time::Expiration;

let expiration = Expiration::Second(30); // 30ces
let expiration = Expiration::Minute(5); // 5min
let expiration = Expiration::Hour(3); // 3 hour
let expiration = Expiration::Default; // 1 hour

Cache

use pocketcache::cache::Cache;
use pocketcache::time::Expiration;

let mut cache = Cache::<&str>::new(Expiration::Default);

set

cache.set("fruit", "banana");
cache.set("vegetable", "carrot");
cache.set("meat", "crab");

get

let mut cache = Cache::<&str>::new(Expiration::Default);

let fruit = cache.get("fruit");
println!("{:#?}", fruit); // None

cache.set("fruit", "banana");
let fruit = cache.get("fruit");
println!("{:#?}", fruit); // Some("banana")

cache.set("fruit", "peach");
let fruit = cache.get("fruit");
println!("{:#?}", fruit); // Some("peach")

// after 1 hour...

let fruit = cache.get("fruit");
println!("{:#?}", fruit); // None

delete

cache.set("fruit", "banana");
cache.delete("fruit");

let fruit = cache.get("fruit");
println!("{:#?}", fruit); // None

clear

cache.set("fruit", "banana");
cache.set("vegetable", "carrot");
cache.set("meat", "crab");
cache.clear();

println!("{:#?}", cache.get("fruit")); // None
println!("{:#?}", cache.get("vegetable")); // None
println!("{:#?}", cache.get("meat")); // None