11 releases
0.2.7 | Dec 31, 2024 |
---|---|
0.2.6 | Dec 30, 2024 |
0.1.2 | Dec 26, 2024 |
0.0.1 | Dec 26, 2024 |
#52 in Caching
877 downloads per month
31KB
604 lines
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
- Support for generic values using valu3
Installation
Add the following to your Cargo.toml
:
[dependencies]
quickleaf = "0.2"
Usage
Here's a basic example of how to use Quickleaf Cache:
use quickleaf::{Quickleaf, ListProps, Order, Filter};
use quickleaf::valu3::value::Value;
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.to_value()));
assert_eq!(cache.get("key3"), Some(&3.to_value()));
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;
use quickleaf::valu3::value::Value;
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.to_value())
);
assert_eq!(
items[1],
Event::insert("key2".to_string(), 2.to_value())
);
assert_eq!(
items[2],
Event::insert("key3".to_string(), 3.to_value())
);
}
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.
Modules
error
Defines custom error types used in the library.
filter
Defines the Filter
enum used for filtering cache entries.
list_props
Defines the ListProps
struct used for specifying properties when listing cache entries.
quickleaf
Defines the Quickleaf
struct which implements the cache functionality.
Running Tests
To run the tests, use the following command:
cargo test
License
This project is licensed under the Apache 2.0 License. See the LICENSE file for more information.
Dependencies
~5.5–7.5MB
~136K SLoC