Skip to content
/ hitbox Public

A high-performance caching framework suitable for single-machine and for distributed applications in Rust

License

Notifications You must be signed in to change notification settings

hit-box/hitbox

This branch is 86 commits ahead of main.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

5bb628c · Feb 16, 2025
Aug 27, 2023
Aug 14, 2023
Aug 26, 2024
Nov 24, 2024
Nov 24, 2024
Feb 16, 2025
Nov 24, 2024
Nov 24, 2024
Sep 1, 2024
Aug 26, 2024
Nov 24, 2024
Sep 13, 2021
Aug 31, 2024
Oct 2, 2019
May 29, 2021
May 29, 2021

Repository files navigation

hitbox

Build status Coverage Status

Hitbox is an asynchronous caching framework supporting multiple backends and suitable for distributed and for single-machine applications.

Framework integrations

Features

  • Automatic cache key generation.
  • Multiple cache backend implementations:
  • Stale cache mechanics.
  • Cache locks for dogpile effect preventions.
  • Distributed cache locks.
  • Detailed metrics out of the box.

Backend implementations

  • Redis
  • In-memory backend

Feature flags

  • derive - Support for Cacheable trait derive macros.
  • metrics - Support for metrics.

Restrictions

Default cache key implementation based on serde_qs crate and have some restrictions.

Documentation

Example

Dependencies:

[dependencies]
hitbox = "0.1"

Code:

First, you should derive Cacheable trait for your struct or enum:

use hitbox::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Cacheable, Serialize)] // With features=["derive"]
struct Ping {
    id: i32,
}

Or implement that trait manually:

use hitbox::{Cacheable, CacheError};
struct Ping { id: i32 }
impl Cacheable for Ping {
    fn cache_key(&self) -> Result<String, CacheError> {
        Ok(format!("{}::{}", self.cache_key_prefix(), self.id))
    }

    fn cache_key_prefix(&self) -> String { "Ping".to_owned() }
}