Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(service/moka): bump moka from 0.10.4 to 0.12.1 #3711

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ md-5 = "0.10"
metrics = { version = "0.20", optional = true }
mini-moka = { version = "0.10", optional = true }
minitrace = { version = "0.6", optional = true }
moka = { version = "0.10", optional = true, features = ["future"] }
moka = { version = "0.12", optional = true, features = ["future", "sync"] }
mongodb = { version = "2.7.0", optional = true, features = ["tokio-runtime"] }
mysql_async = { version = "0.32.2", default-features = false, features = ["default-rustls"], optional = true }
once_cell = "1"
Expand Down
8 changes: 8 additions & 0 deletions core/src/docs/upgrade.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Unreleased

## Public API

### Moka Service Configuration

- The `thread_pool_enabled` option has been removed.

# Upgrade to v0.43

## Public API
Expand Down
2 changes: 2 additions & 0 deletions core/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ pub use self::mini_moka::MiniMoka;
mod moka;
#[cfg(feature = "services-moka")]
pub use self::moka::Moka;
#[cfg(feature = "services-moka")]
pub use self::moka::MokaConfig;

#[cfg(feature = "services-obs")]
mod obs;
Expand Down
99 changes: 47 additions & 52 deletions core/src/services/moka/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,68 @@

use std::collections::HashMap;
use std::fmt::Debug;
use std::fmt::Formatter;
use std::time::Duration;

use async_trait::async_trait;
use log::debug;
use moka::sync::CacheBuilder;
use moka::sync::SegmentedCache;
use serde::Deserialize;

use crate::raw::adapters::typed_kv;
use crate::raw::ConfigDeserializer;
use crate::*;

/// [moka](https://github.com/moka-rs/moka) backend support.
#[doc = include_str!("docs.md")]
#[derive(Default, Debug)]
pub struct MokaBuilder {
/// Config for Mokaservices support.
#[derive(Default, Deserialize)]
#[serde(default)]
#[non_exhaustive]
pub struct MokaConfig {
/// Name for this cache instance.
name: Option<String>,
pub name: Option<String>,
/// Sets the max capacity of the cache.
///
/// Refer to [`moka::sync::CacheBuilder::max_capacity`](https://docs.rs/moka/latest/moka/sync/struct.CacheBuilder.html#method.max_capacity)
max_capacity: Option<u64>,
pub max_capacity: Option<u64>,
/// Sets the time to live of the cache.
///
/// Refer to [`moka::sync::CacheBuilder::time_to_live`](https://docs.rs/moka/latest/moka/sync/struct.CacheBuilder.html#method.time_to_live)
time_to_live: Option<Duration>,
pub time_to_live: Option<Duration>,
/// Sets the time to idle of the cache.
///
/// Refer to [`moka::sync::CacheBuilder::time_to_idle`](https://docs.rs/moka/latest/moka/sync/struct.CacheBuilder.html#method.time_to_idle)
time_to_idle: Option<Duration>,
pub time_to_idle: Option<Duration>,
/// Sets the segments number of the cache.
///
/// Refer to [`moka::sync::CacheBuilder::segments`](https://docs.rs/moka/latest/moka/sync/struct.CacheBuilder.html#method.segments)
num_segments: Option<usize>,
/// Decides whether to enable thread pool of the cache.
///
/// Refer to [`moka::sync::CacheBuilder::thread_pool_enabled`](https://docs.rs/moka/latest/moka/sync/struct.CacheBuilder.html#method.thread_pool_enabled)
thread_pool_enabled: Option<bool>,
pub num_segments: Option<usize>,
}

impl Debug for MokaConfig {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MokaConfig")
.field("name", &self.name)
.field("max_capacity", &self.max_capacity)
.field("time_to_live", &self.time_to_live)
.field("time_to_idle", &self.time_to_idle)
.field("num_segments", &self.num_segments)
.finish_non_exhaustive()
}
}

/// [moka](https://github.com/moka-rs/moka) backend support.
#[doc = include_str!("docs.md")]
#[derive(Default, Debug)]
pub struct MokaBuilder {
config: MokaConfig,
}

impl MokaBuilder {
/// Name for this cache instance.
pub fn name(&mut self, v: &str) -> &mut Self {
if !v.is_empty() {
self.name = Some(v.to_owned());
self.config.name = Some(v.to_owned());
}
self
}
Expand All @@ -69,7 +88,7 @@ impl MokaBuilder {
/// Refer to [`moka::sync::CacheBuilder::max_capacity`](https://docs.rs/moka/latest/moka/sync/struct.CacheBuilder.html#method.max_capacity)
pub fn max_capacity(&mut self, v: u64) -> &mut Self {
if v != 0 {
self.max_capacity = Some(v);
self.config.max_capacity = Some(v);
}
self
}
Expand All @@ -79,7 +98,7 @@ impl MokaBuilder {
/// Refer to [`moka::sync::CacheBuilder::time_to_live`](https://docs.rs/moka/latest/moka/sync/struct.CacheBuilder.html#method.time_to_live)
pub fn time_to_live(&mut self, v: Duration) -> &mut Self {
if !v.is_zero() {
self.time_to_live = Some(v);
self.config.time_to_live = Some(v);
}
self
}
Expand All @@ -89,7 +108,7 @@ impl MokaBuilder {
/// Refer to [`moka::sync::CacheBuilder::time_to_idle`](https://docs.rs/moka/latest/moka/sync/struct.CacheBuilder.html#method.time_to_idle)
pub fn time_to_idle(&mut self, v: Duration) -> &mut Self {
if !v.is_zero() {
self.time_to_idle = Some(v);
self.config.time_to_idle = Some(v);
}
self
}
Expand All @@ -99,15 +118,7 @@ impl MokaBuilder {
/// Refer to [`moka::sync::CacheBuilder::segments`](https://docs.rs/moka/latest/moka/sync/struct.CacheBuilder.html#method.segments)
pub fn segments(&mut self, v: usize) -> &mut Self {
assert!(v != 0);
self.num_segments = Some(v);
self
}

/// Decides whether to enable thread pool of the cache.
///
/// Refer to [`moka::sync::CacheBuilder::thread_pool_enabled`](https://docs.rs/moka/latest/moka/sync/struct.CacheBuilder.html#method.thread_pool_enabled)
pub fn thread_pool_enabled(&mut self, v: bool) -> &mut Self {
self.thread_pool_enabled = Some(v);
self.config.num_segments = Some(v);
self
}
}
Expand All @@ -117,45 +128,29 @@ impl Builder for MokaBuilder {
type Accessor = MokaBackend;

fn from_map(map: HashMap<String, String>) -> Self {
let mut builder = MokaBuilder::default();

map.get("name").map(|v| builder.name(v));
map.get("max_capacity")
.map(|v| v.parse::<u64>().map(|v| builder.max_capacity(v)));
map.get("time_to_live").map(|v| {
v.parse::<u64>()
.map(|v| builder.time_to_live(Duration::from_secs(v)))
});
map.get("time_to_idle").map(|v| {
v.parse::<u64>()
.map(|v| builder.time_to_idle(Duration::from_secs(v)))
});
map.get("num_segments")
.map(|v| v.parse::<usize>().map(|v| builder.segments(v)));
map.get("thread_pool_enabled")
.map(|v| v.parse::<bool>().map(|v| builder.thread_pool_enabled(v)));

builder
MokaBuilder {
config: MokaConfig::deserialize(ConfigDeserializer::new(map))
.expect("config deserialize must succeed"),
}
}

fn build(&mut self) -> Result<Self::Accessor> {
debug!("backend build started: {:?}", &self);

let mut builder: CacheBuilder<String, typed_kv::Value, _> =
SegmentedCache::builder(self.num_segments.unwrap_or(1))
.thread_pool_enabled(self.thread_pool_enabled.unwrap_or(false));
SegmentedCache::builder(self.config.num_segments.unwrap_or(1));
// Use entries' bytes as capacity weigher.
builder = builder.weigher(|k, v| (k.len() + v.size()) as u32);
if let Some(v) = &self.name {
if let Some(v) = &self.config.name {
builder = builder.name(v);
}
if let Some(v) = self.max_capacity {
if let Some(v) = self.config.max_capacity {
builder = builder.max_capacity(v)
}
if let Some(v) = self.time_to_live {
if let Some(v) = self.config.time_to_live {
builder = builder.time_to_live(v)
}
if let Some(v) = self.time_to_idle {
if let Some(v) = self.config.time_to_idle {
builder = builder.time_to_idle(v)
}

Expand All @@ -175,7 +170,7 @@ pub struct Adapter {
}

impl Debug for Adapter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Adapter")
.field("size", &self.inner.weighted_size())
.field("count", &self.inner.entry_count())
Expand Down
1 change: 0 additions & 1 deletion core/src/services/moka/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ This service can be used to:
- `time_to_live`: Set the time to live of the cache.
- `time_to_idle`: Set the time to idle of the cache.
- `num_segments`: Set the segments number of the cache.
- `thread_pool_enabled`: Decides whether to enable thread pool of the cache.

You can refer to [`MokaBuilder`]'s docs for more information

Expand Down
1 change: 1 addition & 0 deletions core/src/services/moka/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@

mod backend;
pub use backend::MokaBuilder as Moka;
pub use backend::MokaConfig;
Loading