Skip to content

Add an in-memory cache with per-entry expiry - #3

Open
JohnCampionJr wants to merge 1 commit into
mainfrom
feat/cache
Open

Add an in-memory cache with per-entry expiry#3
JohnCampionJr wants to merge 1 commit into
mainfrom
feat/cache

Conversation

@JohnCampionJr

@JohnCampionJr JohnCampionJr commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

User description

A small cache for values that expire after a fixed lifetime, set once at construction.

Get treats an expired entry as absent. Len counts everything held, including entries that have expired but not yet been evicted.


Generated description

Below is a concise technical summary of the changes proposed in this PR:
Add an in-memory Cache with a construction-time TTL, supporting value insertion, expiry-aware retrieval, and entry counting. Verify normal reads and expired-entry behavior with tests.

Latest Contributors(1)
UserCommitDate
john@brightshore.ioAdd an in-memory cache...August 27, 2026
Review this PR on Baz | Customize your next review

Summary by cubic

Adds a small in-memory cache where entries expire after a fixed lifetime set at construction.

Get treats an expired entry as absent, while Len still counts entries that have expired but not yet been evicted.

Written for commit 3865546. Summary will update on new commits.

Review in cubic

Note

Add in-memory cache with TTL-based per-entry expiry in internal/cache

  • Introduces cache.Cache backed by a map[string]entry, where each entry stores a value and an expiration timestamp set to now + TTL on Put
  • Get returns false for missing or expired keys, treating expired entries as absent without removing them from the map
  • Includes tests for basic put/get and immediate-expiry behavior using a negative TTL
  • Risk: Len returns the raw map size, so it includes expired-but-not-evicted entries; callers relying on Len for live counts may over-report

Macroscope summarized 3865546.

Entries live for a fixed ttl set at construction. Get treats an expired entry as
absent.
@coderabbitai

coderabbitai Bot commented Aug 27, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: b513c0c9-365e-49d5-8c69-bdd2d8c6f2cd

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

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

@githail

githail Bot commented Aug 27, 2026

Copy link
Copy Markdown

@coderabbitai review

@githail
githail Bot requested a lite review from Copilot August 27, 2026 12:13
@githail

githail Bot commented Aug 27, 2026

Copy link
Copy Markdown

@macroscopeapp review

Copilot AI 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.

Pull request overview

This PR introduces a new internal/cache package that provides a small in-memory string cache with a construction-time TTL, where reads treat expired entries as absent.

Changes:

  • Added cache.Cache backed by a map[string]entry with per-entry expiration timestamps.
  • Implemented Put, expiry-aware Get, and Len for entry counting.
  • Added unit tests covering basic put/get and expiry behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
internal/cache/cache.go Adds the cache implementation, including TTL handling, Put, Get, and Len.
internal/cache/cache_test.go Adds tests for basic retrieval and expired-entry behavior.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread internal/cache/cache.go
Comment on lines +29 to +32
it, ok := c.items[key]
if !ok || time.Now().After(it.expires) {
return "", false
}
Comment on lines +17 to +24
func TestExpiry(t *testing.T) {
c := New(-time.Second) // already expired
c.Put("k", "v")

if _, ok := c.Get("k"); ok {
t.Error("an expired entry should not be returned")
}
}

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3865546c68

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/cache/cache.go
Comment on lines +30 to +31
if !ok || time.Now().After(it.expires) {
return "", false

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Evict expired entries when Get observes them

When a workload inserts many unique keys and later calls Get after they expire, this branch reports each entry as absent but never deletes it from c.items. The map therefore retains every expired key indefinitely and can grow without bound, contrary to Len's documented promise that expired entries are evicted by Get; delete the expired entry before returning.

Useful? React with 👍 / 👎.

Comment thread internal/cache/cache.go
Comment on lines +23 to +24
func (c *Cache) Put(key, value string) {
c.items[key] = entry{value: value, expires: time.Now().Add(c.ttl)}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Synchronize access to the shared item map

When a Cache is shared by multiple goroutines, concurrent calls involving Put and any of Put, Get, or Len access this map without synchronization. This creates data races and can terminate the process with fatal error: concurrent map read and map write or concurrent map writes; protect all map accesses with a mutex or explicitly provide another concurrency-safe implementation.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants