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

Add the "entry" and "entry_by_ref" APIs #193

Merged
merged 10 commits into from
Nov 7, 2022
Merged

Add the "entry" and "entry_by_ref" APIs #193

merged 10 commits into from
Nov 7, 2022

Conversation

tatsuya6502
Copy link
Member

@tatsuya6502 tatsuya6502 commented Nov 1, 2022

Add entry and entry_by_ref APIs to sync and future caches. They return a struct OwnedKeyEntrySelector or RefKeyEntrySelector respectively. You can use their methods like or_insert to select or insert an entry of a cache.

These methods on the entry selectors return an Entry, a snapshot of key and value, carrying additional information is_fresh to indicates if the value was freshly computed or cached.

use moka::sync::Cache;

let cache: Cache<String, u32> = Cache::new(100);
let key = "key1".to_string();

let entry = cache.entry_by_ref(&key).or_insert(3);
assert!(entry.is_fresh());
assert_eq!(entry.key(), &key);
assert_eq!(entry.into_value(), 3);

let entry = cache.entry(key).or_insert(6);
// Not fresh because the value was already in the cache.
assert!(!entry.is_fresh());
assert_eq!(entry.into_value(), 3);

OwnedKeyEntrySelector and RefKeyEntrySelector structs provide the following methods:

  • or_default
  • or_insert
  • or_insert_with
  • or_insert_with_if
  • or_optionally_insert_with
  • or_try_insert_with

Also deprecate get_with_if methods in sync and future caches. The same functionality is provided by the entry selector's or_insert_with_if method.

@tatsuya6502 tatsuya6502 added this to the v0.10.0 milestone Nov 1, 2022
@tatsuya6502 tatsuya6502 added the enhancement New feature or request label Nov 1, 2022
@tatsuya6502 tatsuya6502 self-assigned this Nov 1, 2022
- Add `Entry` struct with the following methods:
    - `is_fresh`
    - `key`
    - `value`
    - `into_value`
- Add `OwnedKeyEntrySelector` and `RefKeyEntrySelector` structs to `sync` and `future`
  modules. They provide the following methods for now:
    - `or_default`
    - `or_insert`
    - `or_insert_with`
    - `or_insert_with_if`
- Add `entry` and `entry_by_ref` methods to `sync` and `future` caches that return
  `OwnedKeyEntrySelector` and `RefKeyEntrySelector` respectively.
- Deprecate `get_with_if` methods in `sync` and `future` caches.
Add `or_optionally_insert_with` method to `OwnedKeyEntrySelector` and
`RefKeyEntrySelector` structs of `sync` and `future` modules.
Add `or_try_insert_with` method to `OwnedKeyEntrySelector` and `RefKeyEntrySelector`
structs of `sync` and `future` modules.
Write some documentation for the `OwnedKeyEntrySelector` and
`RefKeyEntrySelector` structs.
@tatsuya6502 tatsuya6502 marked this pull request as ready for review November 2, 2022 14:21
Conflicts:
  src/future/cache.rs
  src/sync/cache.rs
  src/sync/segment.rs
  src/sync_base/base_cache.rs
Follow up the previous git merge.
Update the test cases for the entry APIs.
Copy link
Member Author

@tatsuya6502 tatsuya6502 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments.

src/future/cache.rs Outdated Show resolved Hide resolved
src/future/cache.rs Outdated Show resolved Hide resolved
src/future/cache.rs Outdated Show resolved Hide resolved
src/future/cache.rs Outdated Show resolved Hide resolved
Cosmetic changes on the doc, a function signature and variable names.
Copy link
Member Author

@tatsuya6502 tatsuya6502 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Request: Some way to determine if future::Cache::get_with() returned cached or fresh value
1 participant