Socket
Book a DemoInstallSign in
Socket

aba-cache

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

aba-cache

0.1.1
Source
Cargo
Version published
Maintainers
1
Created
Source

ABA Cache

Simple cache written for reducing frequent access / compute expensive operation (as of now, it serves only as experimental, not intended for production yet)

This implementation is based on "LRU Cache", with another inspiration comes from "Writing a doubly linked list in Rust is easy".

Example

Basic LRU Cache

Add following dependencies to Cargo.toml

[dependencies]
aba-cache = { version = "0.1.0", default-features = false }

on your main.rs

use aba_cache as cache;

fn main() {
    // create Cache, with multiple_cap set to 2
    // and entry will be timeout after 10 seconds
    let mut cache = cache::LruCache::<usize, &str>::new(2, 10);

    cache.put(1, "a");
    cache.put(2, "b");
    cache.put(2, "c");
    cache.put(3, "d");

    assert_eq!(cache.get(&1), Some(&"a"));
    assert_eq!(cache.get(&2), Some(&"c"));
    assert_eq!(cache.get(&3), Some(&"d"));
}

Async LRU Cache

Add following dependencies to Cargo.toml

[dependencies]
aba-cache = { version = "0.1.0" }
tokio = { version = "0.2", features = ["macros", "rt-core"] }

on your main.rs

use aba_cache as cache;

#[tokio::main]
async fn main() {
    // create Cache, with multiple_cap set to 2
    // and entry will be timeout after 10 seconds
    // additionally, this setup runtime daemon to evict outdate entry
    // every 10 seconds
    let cache = cache::LruAsyncCache::<usize, &str>::new(2, 10);

    cache.put(1, "a").await;
    cache.put(2, "b").await;
    cache.put(2, "c").await;
    cache.put(3, "d").await;

    assert_eq!(cache.get(&1).await, Some("a"));
    assert_eq!(cache.get(&2).await, Some("c"));
    assert_eq!(cache.get(&3).await, Some("d"));
}

References

FAQs

Package last updated on 10 Mar 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.