Skip to content

[bug] cache#keys return the expired and not exists keys. #40

Description

@zw963

Hi, I want to use cache shard to count how many people are online on my website.

When user visit my website, will poll peridly to report he is still online.

I want to use Cache::MemoryStore to create items, but expired in 2 minutes.

Then, I can use cache.keys.size to get the current alive users count.

But, It's seem like cache.keys not work as expected.

please check following example code:

require "./src/cache"

cache = Cache::MemoryStore(String, String).new(expires_in: 2.seconds)

cache.fetch("foo") { "foo" }
cache.fetch("bar", expires_in: 5.seconds) { "bar" }

p! cache.keys # Set{"foo", "bar"}, it correct.

sleep 3.seconds

p! cache.exists?("foo") # false
p! cache.exists?("bar") # true

p! cache.keys # => Set{"foo", "bar"}, foo is expired, and not exists?, it should be Set{"bar"}, right?

Thanks!

Activity

  1. changed the title [-]cache#keys not work as expected.[/-] [+][bug] cache#keys return the expired and not exists keys.[/+] on Jul 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions