diff --git a/lib/con_cache/operations.ex b/lib/con_cache/operations.ex index a4cfda9..d4667ca 100644 --- a/lib/con_cache/operations.ex +++ b/lib/con_cache/operations.ex @@ -85,7 +85,12 @@ defmodule ConCache.Operations do end) end - def dirty_insert_new(%ConCache{ets: ets, owner_pid: owner_pid, ttl: ttl} = cache, key, value) do + def dirty_insert_new(cache, key, %ConCache.Item{ttl: ttl, value: value}), + do: perform_insert_new(cache, key, value, ttl) + + def dirty_insert_new(cache, key, value), do: perform_insert_new(cache, key, value, cache.ttl) + + defp perform_insert_new(%ConCache{ets: ets, owner_pid: owner_pid} = cache, key, value, ttl) do if :ets.insert_new(ets, {key, value}) do set_ttl(cache, key, ttl) invoke_callback(cache, {:update, owner_pid, key, value}) diff --git a/test/con_cache_test.exs b/test/con_cache_test.exs index 0602879..375615b 100644 --- a/test/con_cache_test.exs +++ b/test/con_cache_test.exs @@ -77,6 +77,12 @@ defmodule ConCacheTest do assert ConCache.get(cache, :b) == 2 end + test "insert_new with ConCache.Item unwraps the value" do + {:ok, cache} = start_cache() + assert ConCache.insert_new(cache, :b, %ConCache.Item{value: 2, ttl: 20}) == :ok + assert ConCache.get(cache, :b) == 2 + end + test "insert_new after multiple put on bag" do {:ok, cache} = start_cache(ets_options: [:bag]) ConCache.put(cache, :a, 1)