Skip to content

Commit 36eb6bf

Browse files
committed
cache: entry TTL in milliseconds
Signed-off-by: Matteo Cafasso <[email protected]>
1 parent d974bd9 commit 36eb6bf

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Extra arguments:
4848

4949
* `x-cache-size`: maximum size for the deduplication cache.
5050
This parameter is mandatory.
51-
* `x-cache-ttl`: amount of time in seconds messages are kept in cache.
51+
* `x-cache-ttl`: amount of time in milliseconds messages are kept in cache.
5252
This parameter is optional.
5353
* `x-cache-persistence`: whether the cache will persist on disk or in memory.
5454
This parameter is optional. Default persistence is memory.
@@ -58,7 +58,7 @@ Message deduplication
5858

5959
Each message containing the `x-deduplication-header` header will not be routed if its value has been already submitted previously and has not expired.
6060

61-
The optional header `x-cache-ttl` will override the default one if provided during the exchange declaration. This parameter controls for how many seconds to deduplicate the message. After the TTL expires, a new message with the same `x-deduplication-header` header will be routed again.
61+
The optional header `x-cache-ttl` will override the default one if provided during the exchange declaration. This parameter controls for how many milliseconds to deduplicate the message. After the TTL expires, a new message with the same `x-deduplication-header` header will be routed again.
6262

6363
Running the tests
6464
-----------------

lib/cache.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ defmodule RabbitMQ.MessageDeduplicationPlugin.Cache do
3535
end
3636

3737
@doc """
38-
Put the given value into the cache.
38+
Put the given entry into the cache.
3939
"""
4040
def put(cache, value, ttl \\ nil) do
4141
GenServer.call(cache, {:put, cache, value, ttl})
@@ -175,16 +175,16 @@ defmodule RabbitMQ.MessageDeduplicationPlugin.Cache do
175175
{:atomic, entries} = Mnesia.transaction(fn -> Mnesia.read(cache, value) end)
176176

177177
case List.keyfind(entries, value, 1) do
178-
{_, _, expiration} -> expiration > Os.system_time(:seconds)
178+
{_, _, expiration} -> expiration > Os.system_time(:millisecond)
179179
nil -> false
180180
end
181181
end
182182

183183
# Remove all expired entries from the Mnesia cache.
184184
defp cache_delete_expired(cache) do
185185
select = fn ->
186-
Mnesia.select(cache, [{{cache, :"$1", :_, :"$3"},
187-
[{:>, Os.system_time(:seconds), :"$3"}],
186+
Mnesia.select(cache, [{{cache, :"$1", :"$2"},
187+
[{:>, Os.system_time(:millisecond), :"$2"}],
188188
[:"$1"]}])
189189
end
190190

@@ -213,8 +213,8 @@ defmodule RabbitMQ.MessageDeduplicationPlugin.Cache do
213213
default = cache_property(cache, :default_ttl)
214214

215215
cond do
216-
ttl != nil -> Os.system_time(:seconds) + ttl
217-
default != nil -> Os.system_time(:seconds) + default
216+
ttl != nil -> Os.system_time(:millisecond) + ttl
217+
default != nil -> Os.system_time(:millisecond) + default
218218
true -> nil
219219
end
220220
end

test/cache_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ defmodule RabbitMQ.MessageDeduplicationPlugin.Cache.Test do
1515
end
1616

1717
cache_options = [size: 1, ttl: nil, persistence: :memory]
18-
cache_ttl_options = [size: 1, ttl: 1, persistence: :memory]
18+
cache_ttl_options = [size: 1, ttl: Timer.seconds(1), persistence: :memory]
1919

2020
start_supervised!(%{id: cache,
2121
start: {MessageCache,
@@ -39,7 +39,7 @@ defmodule RabbitMQ.MessageDeduplicationPlugin.Cache.Test do
3939
test "TTL at insertion", %{cache: cache, cache_ttl: _} do
4040
assert MessageCache.member?(cache, "foo") == false
4141

42-
MessageCache.put(cache, "foo", 1)
42+
MessageCache.put(cache, "foo", Timer.seconds(1))
4343
assert MessageCache.member?(cache, "foo") == true
4444

4545
1 |> Timer.seconds() |> Timer.sleep()

0 commit comments

Comments
 (0)