Expand description
§Quickleaf Cache
Quickleaf Cache is a Rust library that provides a simple and efficient in-memory cache with support for filtering, ordering, limiting results, and event notifications. It is designed to be lightweight and easy to use.
§Features
- Insert and remove key-value pairs
- Retrieve values by key
- Clear the cache
- List cache entries with support for filtering, ordering, and limiting results
- Custom error handling
- Event notifications for cache operations
§Installation
Add the following to your Cargo.toml
:
[dependencies]
quickleaf = "0.1"
§Usage
Here’s a basic example of how to use Quickleaf Cache:
use quickleaf::{Quickleaf, ListProps, Order, Filter};
fn main() {
let mut cache = Quickleaf::new(2);
cache.insert("key1", 1);
cache.insert("key2", 2);
cache.insert("key3", 3);
assert_eq!(cache.get("key1"), None);
assert_eq!(cache.get("key2"), Some(&2));
assert_eq!(cache.get("key3"), Some(&3));
let list_props = ListProps::default()
.order(Order::Asc)
.limit(10);
let result = cache.list(list_props).unwrap();
for (key, value) in result {
println!("{}: {}", key, value);
}
}
§Using Filters
You can use filters to narrow down the results when listing cache entries. Here are some examples:
§Filter by Start With
use quickleaf::{Quickleaf, ListProps, Order, Filter};
fn main() {
let mut cache = Quickleaf::new(10);
cache.insert("apple", 1);
cache.insert("banana", 2);
cache.insert("apricot", 3);
let list_props = ListProps::default()
.order(Order::Asc)
.filter(Filter::StartWith("ap"))
.limit(10);
let result = cache.list(list_props).unwrap();
for (key, value) in result {
println!("{}: {}", key, value);
}
}
§Filter by End With
use quickleaf::{Quickleaf, ListProps, Order, Filter};
fn main() {
let mut cache = Quickleaf::new(10);
cache.insert("apple", 1);
cache.insert("banana", 2);
cache.insert("pineapple", 3);
let list_props = ListProps::default()
.order(Order::Asc)
.filter(Filter::EndWith("apple"))
.limit(10);
let result = cache.list(list_props).unwrap();
for (key, value) in result {
println!("{}: {}", key, value);
}
}
§Filter by Start And End With
use quickleaf::{Quickleaf, ListProps, Order, Filter};
fn main() {
let mut cache = Quickleaf::new(10);
cache.insert("applemorepie", 1);
cache.insert("banana", 2);
cache.insert("pineapplepie", 3);
let list_props = ListProps::default()
.order(Order::Asc)
.filter(Filter::StartAndEndWith("apple", "pie"))
.limit(10);
let result = cache.list(list_props).unwrap();
for (key, value) in result {
println!("{}: {}", key, value);
}
}
§Using Events
You can use events to get notified when cache entries are inserted, removed, or cleared. Here is an example:
use quickleaf::{Quickleaf, Event};
use std::sync::mpsc::channel;
fn main() {
let (tx, rx) = channel();
let mut cache = Quickleaf::with_sender(10, tx);
cache.insert("key1", 1);
cache.insert("key2", 2);
cache.insert("key3", 3);
let mut items = Vec::new();
for data in rx {
items.push(data);
if items.len() == 3 {
break;
}
}
assert_eq!(items.len(), 3);
assert_eq!(
items[0],
Event::insert("key1".to_string(), 1)
);
assert_eq!(
items[1],
Event::insert("key2".to_string(), 2)
);
assert_eq!(
items[2],
Event::insert("key3".to_string(), 3)
);
}
§Event Types
There are three types of events:
Insert
: Triggered when a new entry is inserted into the cache.Remove
: Triggered when an entry is removed from the cache.Clear
: Triggered when the cache is cleared.
Re-exports§
pub use valu3;
Structs§
Enums§
- Represents different data types as an enum.