/
sync_backend.rs
107 lines (82 loc) · 2.65 KB
/
sync_backend.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*use actix::prelude::*;
use hitbox::dev::{Backend, BackendError, Delete, DeleteStatus, Get, Lock, LockStatus, Set};
use hitbox_actix::prelude::*;
use serde::{Deserialize, Serialize};
struct UpstreamActor;
impl Actor for UpstreamActor {
type Context = Context<Self>;
}
#[derive(MessageResponse, Deserialize, Serialize, Debug)]
struct Pong(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 {
"Pong".to_owned()
}
}
#[derive(Message)]
#[rtype(result = "Result<Pong, ()>")]
struct Ping {
pub id: i32,
}
impl Handler<Ping> for UpstreamActor {
type Result = ResponseFuture<<Ping as Message>::Result>;
fn handle(&mut self, msg: Ping, _ctx: &mut Self::Context) -> Self::Result {
Box::pin(async move {
actix_rt::time::sleep(core::time::Duration::from_secs(3)).await;
Ok(Pong(msg.id))
})
}
}
struct DummySyncBackend;
impl Actor for DummySyncBackend {
type Context = SyncContext<Self>;
}
impl Backend for DummySyncBackend {
type Actor = Self;
type Context = SyncContext<Self>;
}
impl Handler<Get> for DummySyncBackend {
type Result = Result<Option<Vec<u8>>, BackendError>;
fn handle(&mut self, _msg: Get, _: &mut Self::Context) -> Self::Result {
log::warn!("Dummy sync backend GET");
Ok(None)
}
}
impl Handler<Set> for DummySyncBackend {
type Result = Result<String, BackendError>;
fn handle(&mut self, _msg: Set, _: &mut Self::Context) -> Self::Result {
log::warn!("Dummy sync backend SET");
Ok("42".to_owned())
}
}
impl Handler<Delete> for DummySyncBackend {
type Result = Result<DeleteStatus, BackendError>;
fn handle(&mut self, _msg: Delete, _: &mut Self::Context) -> Self::Result {
log::warn!("Dummy sync backend Delete");
Ok(DeleteStatus::Missing)
}
}
impl Handler<Lock> for DummySyncBackend {
type Result = Result<LockStatus, BackendError>;
fn handle(&mut self, _msg: Lock, _: &mut Self::Context) -> Self::Result {
log::warn!("Dummy sync backend Lock");
Ok(LockStatus::Acquired)
}
}
#[actix::main]
async fn main() -> Result<(), CacheError> {
env_logger::builder()
.filter_level(log::LevelFilter::Debug)
.init();
let dummy_sync_backend = { SyncArbiter::start(3, move || DummySyncBackend) };
let cache = CacheActor::builder().finish(dummy_sync_backend).start();
let upstream = UpstreamActor.start();
let msg = Ping { id: 42 };
let _ = cache.send(msg.into_cache(&upstream)).await??;
Ok(())
}*/
fn main() {
}