Skip to content

feat: [#174] add cache tags with memory driver support - #1579

Open
JGamboa wants to merge 2 commits into
goravel:masterfrom
JGamboa:feat/cache-tags
Open

JGamboa wants to merge 2 commits into
goravel:masterfrom
JGamboa:feat/cache-tags

Conversation

@JGamboa

@JGamboa JGamboa commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

📑 Description

Closes goravel/goravel#174

Adds cache tags, like Laravel's Cache::tags(), and implements them in the memory driver, as suggested in the issue ("just add a mapping for tag and key"):

tagged, err := facades.Cache().Tags("people", "artists")
tagged.Put("john", user, time.Hour)
tagged.Get("john")

people, _ := facades.Cache().Tags("people")
people.Flush() // removes every item stored with the "people" tag
  • Contract (contracts/cache):
    • Cache.Tags(names ...string) (TaggedCache, error) on the default store.
    • A new optional interface, DriverWithTags.
    • TaggedCache: Add, Increment/Decrement, Forever, Forget, Flush, Get*, Has, Put, Pull, Remember, RememberForever. It leaves out Lock, WithContext and Docker, which are store-level.
  • No breaking change: Driver is untouched, so custom and external drivers keep compiling. A store that doesn't implement DriverWithTags returns CacheTagsNotSupported. For a non-default store: Store("x").(cache.DriverWithTags).
  • Semantics, as in Laravel:
    • Items are namespaced by sha1(join(tags, "|")), so tag order matters.
    • Flushing any one tag removes every item stored under it.
  • Memory driver:
    • The tag → keys index is guarded by a mutex shared by tagged writes and flushes, so a flush can't race with a write and leave an unindexed item.
    • The store Flush clears the index too.
    • The periodic cleanup prunes keys that no longer exist.
  • New errors CacheTagsNotSupported and CacheTagsRequired. Mocks are regenerated.

Redis (follow-up PR in goravel/redis):

  • Item keys would be prefix:sha1(tags):key.
  • Each tag keeps a sorted set prefix:tag:{name}:entries of item keys, scored by expiry.
  • Put does SET + ZADD in one pipeline. A tag flush reads the set with ZRANGE, deletes the items in chunks, then deletes the set.

While looking at this I noticed that today Flush in the Redis driver uses FLUSHALL, which wipes the whole Redis database and not only the cache keys. I can open an issue for that separately.

✅ Checks

  • Added test cases for my code
    • cache/memory_tags_test.go: put/get, the same key under different tags, flushing by any tag, flushing an unknown tag, Add, Increment/Decrement, Forget/Pull, expiration, Remember, a store flush clearing the tags, pruning of the index, and concurrent put + flush. Also TestApplicationTags for the facade and the unsupported-driver error.

Add Cache.Tags, the optional DriverWithTags interface and the TaggedCache contract. The memory driver namespaces tagged items by their tags and keeps a tag to key mapping so flushing a tag removes every item stored under it. Drivers that don't implement DriverWithTags make Cache.Tags return CacheTagsNotSupported.
@codecov

codecov Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.05825% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.99%. Comparing base (7ffe4a8) to head (32ff7e2).

Files with missing lines Patch % Lines
cache/memory_tags.go 97.72% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1579      +/-   ##
==========================================
+ Coverage   72.91%   72.99%   +0.08%     
==========================================
  Files         412      413       +1     
  Lines       26893    26992      +99     
==========================================
+ Hits        19608    19703      +95     
- Misses       7283     7287       +4     
  Partials        2        2              

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@JGamboa
JGamboa marked this pull request as ready for review October 18, 2026 23:47
@JGamboa
JGamboa requested a review from a team as a code owner October 18, 2026 23:47
@JGamboa

JGamboa commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

Review Ready

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.

✨ [Feature] Redis Cache Tag

1 participant