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

Feature/disk cache sync on change and sled connection config #194

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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

## [Unreleased]
## Added
- Add `DiskCacheBuilder::set_sync_to_disk_on_cache_change` to specify that the cache changes should be written to disk on every cache change.
- Add `sync_to_disk_on_cache_change` to `#[io_cached]` to allow setting `DiskCacheBuilder::set_sync_to_disk_on_cache_change` from the proc macro.
- Add `DiskCacheBuilder::set_connection_config` to give more control over the sled connection.
- Add `connection_config` to `#[io_cached]` to allow setting `DiskCacheBuilder::set_connection_config` from the proc macro.
- Add `DiskCache::connection()` and `DiskCache::connection_mut()` to give access to the underlying sled connection.
## Changed
- [Breaking] `type` attribute is now `ty`
- Upgrade to syn2
- Signature or `DiskCache::remove_expired_entries`: this now returns `Result<(), DiskCacheError>` instead of `()`, returning an `Err(sled::Error)` on removing and flushing from the connection.
## Removed

## [0.49.3]
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ optional = true
version = "0.1"

[dev-dependencies]
copy_dir = "0.1.3"
googletest = "0.11.0"
tempfile = "3.10.1"

Expand Down
48 changes: 44 additions & 4 deletions cached_proc_macro/src/io_cached.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use proc_macro::TokenStream;
use quote::quote;
use syn::spanned::Spanned;
use syn::{
parse_macro_input, parse_str, Block, ExprClosure, GenericArgument, Ident, ItemFn,
parse_macro_input, parse_str, Block, Expr, ExprClosure, GenericArgument, Ident, ItemFn,
PathArguments, ReturnType, Type,
};

Expand Down Expand Up @@ -36,6 +36,10 @@ struct IOMacroArgs {
ty: Option<String>,
#[darling(default)]
create: Option<String>,
#[darling(default)]
sync_to_disk_on_cache_change: Option<bool>,
#[darling(default)]
connection_config: Option<String>,
}

pub fn io_cached(args: TokenStream, input: TokenStream) -> TokenStream {
Expand Down Expand Up @@ -173,9 +177,11 @@ pub fn io_cached(args: TokenStream, input: TokenStream) -> TokenStream {
&args.cache_prefix_block,
&args.ty,
&args.create,
&args.sync_to_disk_on_cache_change,
&args.connection_config,
) {
// redis
(true, false, time, time_refresh, cache_prefix, ty, cache_create) => {
(true, false, time, time_refresh, cache_prefix, ty, cache_create, _, _) => {
let cache_ty = match ty {
Some(ty) => {
let ty = parse_str::<Type>(ty).expect("unable to parse cache type");
Expand Down Expand Up @@ -242,7 +248,17 @@ pub fn io_cached(args: TokenStream, input: TokenStream) -> TokenStream {
(cache_ty, cache_create)
}
// disk
(false, true, time, time_refresh, _, ty, cache_create) => {
(
false,
true,
time,
time_refresh,
_,
ty,
cache_create,
sync_to_disk_on_cache_change,
connection_config,
) => {
let cache_ty = match ty {
Some(ty) => {
let ty = parse_str::<Type>(ty).expect("unable to parse cache type");
Expand All @@ -253,6 +269,14 @@ pub fn io_cached(args: TokenStream, input: TokenStream) -> TokenStream {
quote! { cached::DiskCache<#cache_key_ty, #cache_value_ty> }
}
};
let connection_config = match connection_config {
Some(connection_config) => {
let connection_config = parse_str::<Expr>(connection_config)
.expect("unable to parse connection_config block");
Some(quote! { #connection_config })
}
None => None,
};
let cache_create = match cache_create {
Some(cache_create) => {
if time.is_some() || time_refresh.is_some() {
Expand Down Expand Up @@ -285,6 +309,22 @@ pub fn io_cached(args: TokenStream, input: TokenStream) -> TokenStream {
}
}
};
let create = match sync_to_disk_on_cache_change {
None => create,
Some(sync_to_disk_on_cache_change) => {
quote! {
(#create).set_sync_to_disk_on_cache_change(#sync_to_disk_on_cache_change)
}
}
};
let create = match connection_config {
None => create,
Some(connection_config) => {
quote! {
(#create).set_connection_config(#connection_config)
}
}
};
let create = match args.disk_dir {
None => create,
Some(disk_dir) => {
Expand All @@ -296,7 +336,7 @@ pub fn io_cached(args: TokenStream, input: TokenStream) -> TokenStream {
};
(cache_ty, cache_create)
}
(_, _, time, time_refresh, cache_prefix, ty, cache_create) => {
(_, _, time, time_refresh, cache_prefix, ty, cache_create, _, _) => {
let cache_ty = match ty {
Some(ty) => {
let ty = parse_str::<Type>(ty).expect("unable to parse cache type");
Expand Down
5 changes: 5 additions & 0 deletions cached_proc_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ pub fn once(args: TokenStream, input: TokenStream) -> TokenStream {
/// `key` or `ty` must also be set.
/// - `with_cached_flag`: (optional, bool) If your function returns a `cached::Return` or `Result<cached::Return, E>`,
/// the `cached::Return.was_cached` flag will be updated when a cached value is returned.
/// - `sync_to_disk_on_cache_change`: (optional, bool) in the case of `DiskCache` specify whether to synchronize the cache to disk each
/// time the cache changes.
/// - connection_config: (optional, string expr) specify an expression which returns a `sled::Config`
/// to give more control over the connection to the disk cache, i.e. useful for controlling the rate at which the cache syncs to disk.
/// See the docs of `cached::stores::DiskCacheBuilder::connection_config` for more info.
///
/// ## Note
/// The `ty`, `create`, `key`, and `convert` attributes must be in a `String`
Expand Down
Loading
Loading