Skip to content

Commit

Permalink
update to associated type
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Aug 3, 2022
1 parent 7ebc09a commit ed69dce
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 133 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "stretto"
version = "0.5.2"
version = "0.6.0"
authors = ["Al Liu <[email protected]>"]
description = "Stretto is a high performance thread-safe memory-bound Rust cache."
homepage = "https://github.com/al8n/stretto"
Expand Down
16 changes: 12 additions & 4 deletions README-zh_hans.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ async fn main() {
#### key_builder

```rust
pub trait KeyBuilder<K: Hash + Eq + ?Sized> {
pub trait KeyBuilder {
type Key: Hash + Eq + ?Sized;

/// hash_index 用于将键哈希运算成一个 u64 值
fn hash_index(&self, key: &K) -> u64;

Expand Down Expand Up @@ -259,7 +261,9 @@ Metrics(度量)应当在需要实时日志记录多种状态信息的时候
#### update_validator

```rust
pub trait UpdateValidator<V>: Send + Sync + 'static {
pub trait UpdateValidator: Send + Sync + 'static {
type Value: Send + Sync + 'static;

/// should_update 在一个已经存在于缓存中的值被更新时调用
fn should_update(&self, prev: &V, curr: &V) -> bool;
}
Expand All @@ -270,7 +274,9 @@ pub trait UpdateValidator<V>: Send + Sync + 'static {
#### callback

```rust
pub trait CacheCallback<V: Send + Sync>: Send + Sync + 'static {
pub trait CacheCallback: Send + Sync + 'static {
type Value: Send + Sync + 'static;

/// on_exit 在一个值被移除 (remove) 出缓存的时候调用。
/// 可以用于实现手动内存释放。
/// 在撤除 (evict) 或者拒绝 (reject) 值的时候亦会被调用
Expand All @@ -293,7 +299,9 @@ CacheCallBack(缓存回调)被用于定制在事件发生时对值的额外
#### coster

```rust
pub trait Coster<V>: Send + Sync + 'static {
pub trait Coster: Send + Sync + 'static {
type Value: Send + Sync + 'static;

/// cost 函数对值进行求值并返回对应的权重,该函数
/// 会在一个新值插入或一个值更新为 0 权值时被调用
fn cost(&self, val: &V) -> i64;
Expand Down
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ For example, if you expect each item to have a cost of 1 and `max_cost` is 100,
#### key_builder

```rust
pub trait KeyBuilder<K: Hash + Eq + ?Sized> {
pub trait KeyBuilder {
type Key: Hash + Eq + ?Sized;

/// hash_index is used to hash the key to u64
fn hash_index(&self, key: &K) -> u64;

Expand Down Expand Up @@ -263,7 +265,9 @@ The Cache will cleanup the expired values every 500ms by default.
#### update_validator

```rust
pub trait UpdateValidator<V>: Send + Sync + 'static {
pub trait UpdateValidator: Send + Sync + 'static {
type Value: Send + Sync + 'static;

/// should_update is called when a value already exists in cache and is being updated.
fn should_update(&self, prev: &V, curr: &V) -> bool;
}
Expand All @@ -275,7 +279,9 @@ this trait is for you to check if the value should be updated.
#### callback

```rust
pub trait CacheCallback<V: Send + Sync>: Send + Sync + 'static {
pub trait CacheCallback: Send + Sync + 'static {
type Value: Send + Sync + 'static;

/// on_exit is called whenever a value is removed from cache. This can be
/// used to do manual memory deallocation. Would also be called on eviction
/// and rejection of the value.
Expand All @@ -299,7 +305,9 @@ CacheCallback is for customize some extra operations on values when related even
#### coster

```rust
pub trait Coster<V>: Send + Sync + 'static {
pub trait Coster: Send + Sync + 'static {
type Value: Send + Sync + 'static;

/// cost evaluates a value and outputs a corresponding cost. This function
/// is ran after insert is called for a new item or an item update with a cost
/// param of 0.
Expand Down
6 changes: 4 additions & 2 deletions benches/ristretto-rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ struct KC {
#[derive(Default)]
struct KH;

impl KeyBuilder<KC> for KH {
impl KeyBuilder for KH {
type Key = KC;

fn hash_index(&self, key: &KC) -> u64 {
key.hash
}
Expand Down Expand Up @@ -86,7 +88,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let c = AsyncCache::builder(12960, 1e6 as i64)
.set_key_builder(KH::default())
.set_metrics(true)
.finalize()
.finalize(tokio::spawn)
.unwrap();

let time = Instant::now();
Expand Down
51 changes: 27 additions & 24 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ macro_rules! impl_builder {
where
K: Hash + Eq,
V: Send + Sync + 'static,
KH: KeyBuilder<K>,
C: Coster<V>,
U: UpdateValidator<V>,
CB: CacheCallback<V>,
KH: KeyBuilder<Key = K>,
C: Coster<Value = V>,
U: UpdateValidator<Value = V>,
CB: CacheCallback<Value = V>,
S: BuildHasher + Clone + 'static,
{
/// Set the number of counters for the Cache.
Expand Down Expand Up @@ -106,7 +106,7 @@ macro_rules! impl_builder {
/// [`TransparentKeyBuilder`]: struct.TransparentKeyBuilder.html
/// [`DefaultKeyBuilder`]: struct.DefaultKeyBuilder.html
#[inline]
pub fn set_key_builder<NKH: KeyBuilder<K>>(
pub fn set_key_builder<NKH: KeyBuilder<Key = K>>(
self,
kh: NKH,
) -> $ty<K, V, NKH, C, U, CB, S> {
Expand All @@ -129,7 +129,10 @@ macro_rules! impl_builder {
///
/// [`Coster`]: trait.Coster.html
#[inline]
pub fn set_coster<NC: Coster<V>>(self, coster: NC) -> $ty<K, V, KH, NC, U, CB, S> {
pub fn set_coster<NC: Coster<Value = V>>(
self,
coster: NC,
) -> $ty<K, V, KH, NC, U, CB, S> {
$ty {
inner: self.inner.set_coster(coster),
}
Expand All @@ -143,7 +146,7 @@ macro_rules! impl_builder {
///
/// [`UpdateValidator`]: trait.UpdateValidator.html
#[inline]
pub fn set_update_validator<NU: UpdateValidator<V>>(
pub fn set_update_validator<NU: UpdateValidator<Value = V>>(
self,
uv: NU,
) -> $ty<K, V, KH, C, NU, CB, S> {
Expand All @@ -158,7 +161,7 @@ macro_rules! impl_builder {
///
/// [`CacheCallback`]: trait.CacheCallback.html
#[inline]
pub fn set_callback<NCB: CacheCallback<V>>(
pub fn set_callback<NCB: CacheCallback<Value = V>>(
self,
cb: NCB,
) -> $ty<K, V, KH, C, U, NCB, S> {
Expand Down Expand Up @@ -191,10 +194,10 @@ macro_rules! impl_cache {
where
K: Hash + Eq,
V: Send + Sync + 'static,
KH: KeyBuilder<K>,
C: Coster<V>,
U: UpdateValidator<V>,
CB: CacheCallback<V>,
KH: KeyBuilder<Key = K>,
C: Coster<Value = V>,
U: UpdateValidator<Value = V>,
CB: CacheCallback<Value = V>,
S: BuildHasher + Clone + 'static,
{
/// `get` returns a `Option<ValueRef<V, SS>>` (if any) representing whether the
Expand Down Expand Up @@ -317,10 +320,10 @@ macro_rules! impl_cache {
where
K: Hash + Eq,
V: Send + Sync + 'static,
KH: KeyBuilder<K>,
C: Coster<V>,
U: UpdateValidator<V>,
CB: CacheCallback<V>,
KH: KeyBuilder<Key = K>,
C: Coster<Value = V>,
U: UpdateValidator<Value = V>,
CB: CacheCallback<Value = V>,
S: BuildHasher + Clone + 'static,
{
fn as_ref(&self) -> &$cache<K, V, KH, C, U, CB, S> {
Expand All @@ -332,10 +335,10 @@ macro_rules! impl_cache {
where
K: Hash + Eq,
V: Send + Sync + 'static,
KH: KeyBuilder<K>,
C: Coster<V>,
U: UpdateValidator<V>,
CB: CacheCallback<V>,
KH: KeyBuilder<Key = K>,
C: Coster<Value = V>,
U: UpdateValidator<Value = V>,
CB: CacheCallback<Value = V>,
S: BuildHasher + Clone + 'static,
{
fn clone(&self) -> Self {
Expand Down Expand Up @@ -364,8 +367,8 @@ macro_rules! impl_cache_processor {
impl<V, U, CB, S> $processor<V, U, CB, S>
where
V: Send + Sync + 'static,
U: UpdateValidator<V>,
CB: CacheCallback<V>,
U: UpdateValidator<Value = V>,
CB: CacheCallback<Value = V>,
S: BuildHasher + Clone + 'static + Send,
{
#[inline]
Expand Down Expand Up @@ -484,8 +487,8 @@ macro_rules! impl_cache_cleaner {
impl<'a, V, U, CB, S> $cleaner<'a, V, U, CB, S>
where
V: Send + Sync + 'static,
U: UpdateValidator<V>,
CB: CacheCallback<V>,
U: UpdateValidator<Value = V>,
CB: CacheCallback<Value = V>,
S: BuildHasher + Clone + 'static + Send,
{
#[inline]
Expand Down
Loading

0 comments on commit ed69dce

Please sign in to comment.