#![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::owned::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 = "foo";
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 || {
let timer = Timer::default();
let dur = Duration::from_millis(2000);
let timer_ft = timer.interval(dur).from_err::<RedisError>().fold(reader_client, |reader_client, _| {
reader_client.get(KEY).and_then(|(client, value)| {
if let Some(value) = value {
println!("Reader read key {:?} with value {:?}", KEY, value);
}
Ok(client)
})
});
let _ = timer_ft.wait();
});
let writer_client = sync_client.clone();
let writer_jh = thread::spawn(move || {
let timer = Timer::default();
let dur = Duration::from_millis(2000);
let timer_ft = timer.interval(dur).from_err::<RedisError>().fold(writer_client, |writer_client, _| {
writer_client.incr(KEY).and_then(|(client, value)| {
println!("Writer incremented key {:?} to {:?}", KEY, value);
Ok(client)
})
});
let _ = timer_ft.wait();
});
let _ = event_loop_jh.join();
let _ = reader_jh.join();
let _ = writer_jh.join();
}