#![allow(unused_variables)]
#![allow(unused_imports)]
extern crate fred;
extern crate tokio_core;
extern crate tokio_timer;
extern crate futures;
use fred::RedisClient;
use fred::sync::borrowed::RedisClientRemote;
use fred::types::*;
use fred::error::*;
use tokio_core::reactor::Core;
use futures::Future;
use futures::stream::{
self,
Stream
};
use futures::future::{
self,
Either
};
use tokio_timer::Timer;
use std::time::Duration;
use std::thread;
const KEY: &'static str = "bar";
fn main() {
let sync_client = RedisClientRemote::new();
let t_sync_client = sync_client.clone();
let event_loop_jh = thread::spawn(move || {
let mut core = Core::new().unwrap();
let handle = core.handle();
let config = RedisConfig::default();
let client = RedisClient::new(config);
let connect_ft = client.on_connect().and_then(|_| {
println!("Client connected.");
Ok(())
});
let connection = client.connect(&handle);
let remote = t_sync_client.init(client);
let composed = connection
.join(remote)
.join(connect_ft);
let _ = core.run(composed).unwrap();
});
let _ = sync_client.on_connect().wait();
let reader_client = sync_client.clone();
let reader_jh = thread::spawn(move || {
loop {
let value = match reader_client.get(KEY).wait() {
Ok(v) => v,
Err(e) => {
println!("Error reading key: {:?}", e);
break;
}
};
if let Some(value) = value {
println!("Reader read key {:?} with value {:?}", KEY, value);
}
thread::sleep(Duration::from_millis(2000));
}
});
let writer_client = sync_client.clone();
let writer_jh = thread::spawn(move || {
loop {
let value = match writer_client.incr(KEY).wait() {
Ok(v) => v,
Err(e) => {
println!("Error incrementing key: {:?}", e);
break;
}
};
println!("Writer incremented key {:?} to {:?}", KEY, value);
thread::sleep(Duration::from_millis(2000));
}
});
let _ = event_loop_jh.join();
let _ = reader_jh.join();
let _ = writer_jh.join();
}