#fabric #cache #keyval #data #data-structures

fabric-cache-client

Client for interacting with a fabric server

5 releases

new 0.1.4 Jan 30, 2025
0.1.3 Jan 25, 2025
0.1.2 Dec 26, 2024
0.1.1 Dec 26, 2024
0.1.0 Dec 25, 2024

#336 in Caching

Download history 377/week @ 2024-12-22 49/week @ 2024-12-29 13/week @ 2025-01-05 67/week @ 2025-01-19 133/week @ 2025-01-26

213 downloads per month

MIT license

14KB
145 lines

Fabric Client

Client for interacting with a Fabric Cache server.

Install

Use cargo CLI:

cargo install fabric-client

Or manually add it into your Cargo.toml:

[dependencies]
fabric-client = "0.1.4"

Usage

For more thorough information, read the docs.

Simple example for a game leaderboard cache:

use fabric_cache_client::FabricClient;
use serde::{Deserialize, Serialize};

// Dummy data structures just for example
#[derive(Deserialize, Serialize, Debug)]
struct Leaderboard {
    top_3_players: Vec<Player>,
    highest_score: i32,
    last_updated: String,
}
#[derive(Deserialize, Serialize, Debug, Clone)]
struct Player {
    name: String,
    score: i32,
}

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
    // Create a connection with the fabric-cache server
    let mut cache = FabricClient::connect("127.0.0.1:8731").await?;

    // Some dummy data for the example
    let players = vec![
        Player {
            name: "Leeroooy Jenkins".into(),
            score: 2830,
        },
        Player {
            name: "kinda l33t".into(),
            score: 2315,
        },
        Player {
            name: "Some Other Player".into(),
            score: 1950,
        },
    ];
    let leaderboard = Leaderboard {
        top_3_players: players.clone(),
        highest_score: players.first().map(|player| player.score).unwrap_or(0),
        last_updated: "{current timestamp}".into(),
    };

    // Insert the data into cache
    cache.set("myGamesLeaderboard", &leaderboard).await?;

    // Retrieve the data from cache
    let _leaderboard: Leaderboard = cache.get("myGamesLeaderboard").await?;

    // Update data in cache
    let mut leaderboard: Leaderboard = cache.get("myGamesLeaderboard").await?;
    leaderboard.top_3_players = vec![
        Player {
            name: "kinda l33t".into(),
            score: 2910,
        },
        Player {
            name: "Leeroooy Jenkins".into(),
            score: 2830,
        },
        Player {
            name: "Some Other Player".into(),
            score: 2100,
        },
    ];
    cache.set("myGamesLeaderboard", &leaderboard).await?;

    // Delete data in cache
    cache.remove("myGamesLeaderboard").await?;

    Ok(())
}

Dependencies

~3–9.5MB
~90K SLoC