Crates.io | eternity |
lib.rs | eternity |
version | 0.1.0 |
source | src |
created_at | 2021-03-29 07:29:37.961441 |
updated_at | 2021-03-29 07:29:37.961441 |
description | A Rust library to limit requests and cache results. |
homepage | https://github.com/Lakelezz/eternity |
repository | https://github.com/Lakelezz/eternity.git |
max_upload_size | |
id | 375022 |
size | 59,356 |
Eternity is a Rust library to rate limit and optionally cache keyed results.
Two use cases:
View the examples on how to use this library for these cases.
A basic limiter for endpoints:
use eternity::{Bucket, BucketBuilder};
use eternity::multi_bucket::{CachedLimitedEnums, ToBucket};
#[derive(Hash, PartialEq, Clone, Eq)]
enum Route {
GetUser(u64),
GetStats,
GetGuild(u64),
}
impl ToBucket<Route, String> for Route {
fn to_bucket(&self) -> Option<Bucket<Route, String>> {
match self {
Self::GetUser(_) => Some(BucketBuilder::new()
.limit(4)
.time_span(10)
.build()),
_ => None,
}
}
}
#[tokio::main]
async fn main() {
let mut limiter: CachedLimitedEnums<Route, String> = CachedLimitedEnums::new();
let result = limiter.cache_or(&Route::GetUser(1), get_user(1));
}
async fn get_user(user_id: u64) -> Option<String> {
Some(format!("eternity-{}", user_id))
}
Here are examples to see what this library can be used for.
There are two features and they are not enabled by default.
cache
: Enables functionality to cache values.tokio_0_2
: By default this crate supports tokio
v1
, this feature
enables v0.2
support.Add the following to your Cargo.toml
file:
[dependencies]
eternity = "0.1"
Eternity supports a minimum of Rust 1.48.