use std::hash::{BuildHasher, Hasher};
use fibre_cache::{AsyncCache, Cache, CacheBuilder};
#[derive(Clone, Default)]
pub struct ShardControllingHasher;
impl BuildHasher for ShardControllingHasher {
type Hasher = TestHasher;
fn build_hasher(&self) -> Self::Hasher {
TestHasher(0)
}
}
pub struct TestHasher(u64);
impl Hasher for TestHasher {
fn finish(&self) -> u64 {
self.0
}
fn write(&mut self, _: &[u8]) {
unimplemented!()
}
fn write_i32(&mut self, i: i32) {
self.0 = i as u64;
}
}
pub fn build_test_cache(shards: usize) -> Cache<i32, String, ShardControllingHasher> {
CacheBuilder::new()
.shards(shards)
.hasher(ShardControllingHasher)
.build()
.unwrap()
}
pub fn build_test_cache_with_cap(shards: usize, capacity: u64) -> Cache<i32, String, ShardControllingHasher> {
CacheBuilder::new()
.shards(shards)
.capacity(capacity)
.hasher(ShardControllingHasher)
.build()
.unwrap()
}
pub fn build_test_async_cache(shards: usize) -> AsyncCache<i32, String, ShardControllingHasher> {
CacheBuilder::new()
.shards(shards)
.hasher(ShardControllingHasher)
.build_async()
.unwrap()
}