Nebulex.Cache.to_map and Cache.get

iex> Blog.Cache.set 1, "value", ttl: 10
"value"
iex> Blog.Cache.set 2, "value", ttl: 10
"value"
iex> Blog.Cache.set 3, "value", ttl: 10
"value" 
iex> Blog.Cache.to_map
%{1 => "value", 2 => "value", 3 => "value"}
iex> Blog.Cache.get 1
nil
iex> Blog.Cache.to_map
%{2 => "value", 3 => "value"}

Why is it that only after running Blog.Cache.get key, Blog.Cache.to_map is updated? How do I refresh the stored data? Are there any computer science concept related with on-memory cache involved?

since this deals with ttl (of 10 seconds) you should post a function akin to the one in the readme, that uses :timer.sleep and then tell us what the unexpected behaviour is:

# Now it is ready to be used from any other module. Here is an example:
defmodule MyApp.Test do
  alias MyApp.Cache
  alias Nebulex.Object

  def test do
    Cache.set "foo", "bar", ttl: 2

    "bar" = Cache.get "foo"

    true = Cache.has_key? "foo"

    %Object{key: "foo", value: "bar"} = Cache.get "foo", return: :object

    :timer.sleep(2000)

    nil = Cache.get "foo"

    nil =
      "foo"
      |> Cache.set("bar", return: :key)
      |> Cache.delete(return: :key)
      |> Cache.get
  end
end
1 Like