Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/con_cache/operations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
6 changes: 6 additions & 0 deletions test/con_cache_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down