#actix-actor #cache #actix #diesel #actor

actix_diesel_cache

actix_diesel_cache 是一个提供在本地机器上缓存所有数据库条目的 actix 代理的 crate。

1 个不稳定版本

0.1.0 2020 年 12 月 23 日

#26#actix-actor

MIT/Apache

13KB
245

actix_diesel_cache

Docs Crates.io

一个带有代理的库,该代理为数据库中较小且很少变化的表提供缓存。

用法

添加到 Cargo.toml

actix_diesel_cache = "0.1.0"

示例

use diesel::prelude::*;

table! {
    shop (id) {
        id -> Int4,
        name -> Text,
        address -> Text,
    }
}

#[derive(Queryable, Insertable, Clone, Debug, Eq, PartialEq)]
#[table_name = "shop"]
struct Shop {
    id: i32,
    name: String,
    address: String,
}

impl actix_diesel_cache::Cache<SqliteConnection, shop::table> for Shop {
    type Id = i32;
    fn get_id(&self) -> Self::Id {
        s.id
    }
}

async fn example(conn: SqliteConnection) -> actix_diesel_cache::Result<()> {
    let addr = actix_diesel_cache::CacheDbActor::new(conn)?.start();

    let shop = Shop {
        id: 1,
        name: "Adidas",
        address: "Central street",
    };
    addr.send(actix_diesel_cache::Save(shop)).await.unwrap()?;
    let shop1 = addr.send(actix_diesel_cache::Get(shop.id)).await.unwrap()?;;

    assert_eq!(shop, shop1);

    let shops = addr.send(actix_diesel_cache::GetAll::default()).await.unwrap()?;;

    assert_eq!(shops, vec![shop]);
}

依赖项

~11MB
~187K SLoC