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: introduce CacheBuilder for in-memory cache #350

Merged
merged 2 commits into from
Apr 16, 2024
Merged

feat: introduce CacheBuilder for in-memory cache #350

merged 2 commits into from
Apr 16, 2024

Conversation

MrCroxx
Copy link
Collaborator

@MrCroxx MrCroxx commented Apr 16, 2024

What's changed and what's your intention?

Please explain IN DETAIL what the changes are in this PR and why they are needed. :D

Introduce CacheBuilder to make in-memory cache build much easier.

Thanks @wenym1 for help. 🙏

Before (with default args):

    let cache = Cache::new(
        LfuCacheConfig {
            capacity: 1000,
            shards: 1,
            eviction_config: LfuConfig {
                window_capacity_ratio: 0.1,
                protected_capacity_ratio: 0.8,
                cmsketch_eps: 0.001,
                cmsketch_confidence: 0.9,
            },
            object_pool_capacity: 100,
            hash_builder: RandomState::default(),
            event_listener: DefaultCacheEventListener::default(),
        }
        .into(),
    );

After (with default args):

    let cache = CacheBuilder::new(1000).build();

Checklist

  • I have written the necessary rustdoc comments
  • I have added the necessary unit tests and integration tests
  • I have passed make all (or make fast instead if the old tests are not modified) in my local environment.

Related issues or PRs (optional)

#327

Signed-off-by: MrCroxx <mrcroxx@outlook.com>
@MrCroxx MrCroxx added feature New feature or request refactor labels Apr 16, 2024
@MrCroxx MrCroxx requested a review from wenym1 April 16, 2024 06:22
@MrCroxx MrCroxx self-assigned this Apr 16, 2024
@MrCroxx
Copy link
Collaborator Author

MrCroxx commented Apr 16, 2024

cc @xiaguan

Copy link

codecov bot commented Apr 16, 2024

Codecov Report

Attention: Patch coverage is 80.82192% with 28 lines in your changes are missing coverage. Please review.

Project coverage is 76.74%. Comparing base (5f4c91f) to head (e0741dd).

Files Patch % Lines
foyer-memory/src/cache.rs 78.94% 28 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #350      +/-   ##
==========================================
+ Coverage   76.68%   76.74%   +0.05%     
==========================================
  Files          53       53              
  Lines        6343     6376      +33     
==========================================
+ Hits         4864     4893      +29     
- Misses       1479     1483       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@wenym1 wenym1 left a comment

Choose a reason for hiding this comment

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

LGTM!

self
}

pub fn with_eviction_config(mut self, eviction_config: EvictionConfig) -> Self {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can even be fn with_eviction_config(mut self: eviction_config: impl Into<EvictionConfig>).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sensei!!!

Signed-off-by: MrCroxx <mrcroxx@outlook.com>
@MrCroxx MrCroxx merged commit 8f94d52 into main Apr 16, 2024
13 checks passed
@MrCroxx MrCroxx deleted the xx/builder branch April 16, 2024 07:59
@xiaguan
Copy link
Contributor

xiaguan commented Apr 16, 2024

👍

MrCroxx added a commit that referenced this pull request Apr 17, 2024
* feat: introduce `CacheBuilder` for in-memory cache

Signed-off-by: MrCroxx <mrcroxx@outlook.com>

* refactor: further simplify builds

Signed-off-by: MrCroxx <mrcroxx@outlook.com>

---------

Signed-off-by: MrCroxx <mrcroxx@outlook.com>
let eviction_config = self.eviction_config.unwrap_or(Self::DEFAULT_EVICTION_CONFIG);
let object_pool_capacity = self
.object_pool_capacity
.unwrap_or(capacity / Self::DEFAULT_OBJECT_POOL_CAPACITY_RATIO_RECIPROCAL);
Copy link
Contributor

Choose a reason for hiding this comment

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

Due to DEFAULT_OBJECT_POOL_CAPACITY_RATIO_RECIPROCAL==10 , the capacity must greater than 9. Otherwise, it will panic due to zero object pool capacity.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fotgot the AsyncQueue capacity cannot be 0. Let me open an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request refactor
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

3 participants