Skip to content

Add try_get_with_entry to have access to whether an entry is fresh or not - #589

Open
calavera wants to merge 3 commits into
moka-rs:mainfrom
tensorlakeai:main
Open

calavera wants to merge 3 commits into
moka-rs:mainfrom
tensorlakeai:main

Conversation

@calavera

@calavera calavera commented Mar 26, 2026

Copy link
Copy Markdown

👋 I have some code that requires to know whether an entry is fresh from the cache or not.

I decided to expose that functionality in a new public function try_get_with_entry. I'll appreciate it if this code could make it back into upstream. I think it's pretty trivial to maintain since it's just the code in try_get_with without the map exposed in a public function.


Open with Devin

Summary by CodeRabbit

  • New Features

    • Added a cache query that returns full entry objects (value plus metadata) for advanced caching scenarios.
  • Refactor

    • Updated the existing value-only retrieval to delegate to the new entry-aware flow, simplifying control flow and making initialization and hashing paths consistent.

@coderabbitai

coderabbitai Bot commented Mar 26, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: c6ae000d-becb-4aa1-82ae-2667a778dcbb

📥 Commits

Reviewing files that changed from the base of the PR and between 863392b and 6faed13.

📒 Files selected for processing (1)
  • src/future/cache.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/future/cache.rs

📝 Walkthrough

Walkthrough

Added try_get_with_entry, an async method that returns Entry<K, V> (with init metadata) and refactored try_get_with to delegate to it and return only the value. Explicit generic where-clauses for F and E were added to try_get_with.

Changes

Cohort / File(s) Summary
Cache Entry Accessor
src/future/cache.rs
Added pub async fn try_get_with_entry<F, E>(&self, key: K, init: F) -> Result<Entry<K, V>, Arc<E>>; refactored pub async fn try_get_with<F, E>(&self, key: K, init: F) -> Result<V, Arc<E>> to call try_get_with_entry(...).await.map(Entry::into_value); introduced explicit bounds F: Future<Output = Result<V, E>> and E: Send + Sync + 'static.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 I hopped into cache, nose twitching with glee,

I found an Entry hiding a secret for me,
One method now yields, the other just peeks,
Values and metadata, tidy techniques,
🥕 code crumbs scattered, a rabbit's small feat.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding a new try_get_with_entry method that allows callers to determine whether a cache entry is fresh or not.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

devin-ai-integration[bot]

This comment was marked as resolved.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/future/cache.rs (1)

1305-1333: ⚠️ Potential issue | 🔴 Critical

try_get_with_entry can return an Entry that panics on key()

At Line 1331, need_key is false, so this new public API can return Entry<K, V> with no key materialized. Calling Entry::key() then panics (Bug: Key is None), which is a correctness issue for the exposed method.

🔧 Suggested fix
 pub async fn try_get_with<F, E>(&self, key: K, init: F) -> Result<V, Arc<E>>
 where
     F: Future<Output = Result<V, E>>,
     E: Send + Sync + 'static,
 {
-    self.try_get_with_entry(key, init).await.map(Entry::into_value)
+    futures_util::pin_mut!(init);
+    let hash = self.base.hash(&key);
+    let key = Arc::new(key);
+    self.get_or_try_insert_with_hash_and_fun(key, hash, init, false)
+        .await
+        .map(Entry::into_value)
 }
 
 pub async fn try_get_with_entry<F, E>(&self, key: K, init: F) -> Result<Entry<K, V>, Arc<E>>
 where
     F: Future<Output = Result<V, E>>,
     E: Send + Sync + 'static,
 {
     futures_util::pin_mut!(init);
     let hash = self.base.hash(&key);
     let key = Arc::new(key);
-    self.get_or_try_insert_with_hash_and_fun(key, hash, init, false)
+    self.get_or_try_insert_with_hash_and_fun(key, hash, init, true)
         .await
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/future/cache.rs` around lines 1305 - 1333, The public method
try_get_with_entry currently calls get_or_try_insert_with_hash_and_fun with
need_key set to false, which can return an Entry lacking the key and cause
Entry::key() to panic; change the call in try_get_with_entry to request the key
(set need_key = true) so the returned Entry always contains the key
materialized, ensuring Entry::key() is safe for callers (adjust
get_or_try_insert_with_hash_and_fun signature/usage if needed to accept the true
flag).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@src/future/cache.rs`:
- Around line 1305-1333: The public method try_get_with_entry currently calls
get_or_try_insert_with_hash_and_fun with need_key set to false, which can return
an Entry lacking the key and cause Entry::key() to panic; change the call in
try_get_with_entry to request the key (set need_key = true) so the returned
Entry always contains the key materialized, ensuring Entry::key() is safe for
callers (adjust get_or_try_insert_with_hash_and_fun signature/usage if needed to
accept the true flag).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 75eaad1c-ac86-4633-b64c-7fed5144cd04

📥 Commits

Reviewing files that changed from the base of the PR and between 7006d8c and 863392b.

📒 Files selected for processing (1)
  • src/future/cache.rs

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant