https://github.com/rambler-digital-solutions/actix-cache
Actix cache is a proxy actor and infrastructure for asynchronous and clear cache interaction for Actix actor and Actix-web frameworks.
https://github.com/rambler-digital-solutions/actix-cache
actix actix-cache cache hacktoberfest rust
Last synced: about 2 months ago
JSON representation
Actix cache is a proxy actor and infrastructure for asynchronous and clear cache interaction for Actix actor and Actix-web frameworks.
- Host: GitHub
- URL: https://github.com/rambler-digital-solutions/actix-cache
- Owner: rambler-digital-solutions
- License: mit
- Created: 2020-01-15T14:31:47.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-05-31T12:25:21.000Z (about 4 years ago)
- Last Synced: 2025-04-01T16:07:46.425Z (3 months ago)
- Topics: actix, actix-cache, cache, hacktoberfest, rust
- Language: Rust
- Homepage:
- Size: 165 KB
- Stars: 9
- Watchers: 8
- Forks: 3
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# actix-cache
[](https://travis-ci.org/rambler-digital-solutions/actix-cache)
[](https://coveralls.io/github/rambler-digital-solutions/actix-cache?branch=master)**Actix-Cache framework development moved to the [Hitbox](https://github.com/hit-box/hitbox) repository.
Actix-Cache is not supported now.**Actix cache is a proxy actor and infrastructure for asynchronous and clear cache interaction for Actix actor and Actix-web frameworks.
## Features
* Async/Sync cache backend support.
* [Dogpile] effect prevention.
* Stale cache mechanics.
* Automatic cache key generation.
* Detailed Prometheus metrics out of the box.## Backend implementations
At this time supported or planned next cache backend implementation:
- [x] Redis backend (actix-cache-redis)
- [ ] In-memory backend## Feature flags
* redis - Enabled by default. Add support for redis backend.
* derive - Support for [Cacheable] trait derive macros.
* metrics - Support for Prometheus metrics.## Example
Dependencies:
```toml
[dependencies]
actix-cache = "0.2"
```Code:
First of all, you should derive [Cacheable] trait for your actix Message:
> **_NOTE:_** Default cache key implementation based on serde_qs crate
> and have some [restrictions](https://docs.rs/serde_qs/latest/serde_qs/#supported-types).```rust
use actix::prelude::*;
use actix_cache::Cacheable; // With features=["derive"]
use actix_derive::Message;
use serde::{Deserialize, Serialize};
struct Pong;#[derive(Message, Cacheable, Serialize)]
#[rtype(result = "Result")]
struct Ping {
id: i32,
}
```
Or implement that trait manually:```rust
use actix_cache::{Cacheable, CacheError};struct Ping { id: i32 }
impl Cacheable for Ping {
fn cache_message_key(&self) -> Result {
Ok(format!("{}::{}", self.cache_key_prefix(), self.id))
}
fn cache_key_prefix(&self) -> String { "Ping".to_owned() }
}
```
Next step is to instantiate [CacheActor] with default backend:```rust
use actix::prelude::*;
use actix_cache::{CacheError, Cache};#[actix_rt::main]
async fn main() -> Result<(), CacheError> {
let cache = Cache::new()
.await?
.start();
Ok(())
}
```And the last step is using cache in your code (actix-web handler for example).
This full example and other examples you can see on [github.com](https://github.com/rambler-digital-solutions/actix-cache/blob/master/examples/actix_web.rs)```rust
use actix::prelude::*;
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use actix_cache::{Cache, Cacheable};
use serde::Serialize;struct FibonacciActor;
impl Actor for FibonacciActor { type Context = Context; }
#[derive(Message, Cacheable, Serialize)]
#[rtype(result = "u64")]
struct GetNumber {
number: u8
}impl Handler for FibonacciActor {
type Result = ::Result;fn handle(&mut self, msg: GetNumber, _ctx: &mut Self::Context) -> Self::Result {
42
}
}async fn index(
fib: web::Data>,
cache: web::Data>
) -> impl Responder {
let query = GetNumber { number: 40 };
let number = cache
.send(query.into_cache(&fib))
.await
.unwrap()
.unwrap();
HttpResponse::Ok().body(format!("Generate Fibonacci number {}", number))
}
```[Dogpile]: https://www.sobstel.org/blog/preventing-dogpile-effect/
[Cacheable]: https://docs.rs/actix-cache/latest/actix-cache/cache/trait.Cacheable.html
[CacheActor]: https://docs.rs/actix-cache/latest/actix-cache/actor/struct.CacheActor.html