I am using ETS to cached a database schema from postegress using ecto here are those examples:
table = :ets.new(:cache_name,[:set, :protected])
and include those registry:
:ets.insert(table,{:first_table,{1}})
:ets.insert(table,{:first_table,{5}})
:ets.insert(table,{:second_table,{1}})
but the second one replace the first one, for that reason i concat the table name and the id to get a unique key :ets.insert(table,{:first_table1,{1}})
for these registry, but at the moment that i want the first registry for the first table i have a problem because i contain the same key for the second and it retreive two registry:
:ets.match_object(table,{:"_",{1}})
how can i specify to ETS that if the key contains the table_name retreive these registry?
I don't think you can match part of the atom's name with :ets.match_object
. I'd suggest using a tuple of table name and id as the key if you want a lookup based on both table and id as well as only table:
table = :ets.new(:cache_name, [:set, :protected])
:ets.insert(table, {{:first_table, 1}, {1}})
:ets.insert(table, {{:first_table, 2}, {5}})
:ets.insert(table, {{:second_table, 1}, {1}})
# Get table = :first_table, id = 1
IO.inspect :ets.lookup(table, {:first_table, 1})
# Get table = :first_table and any id
IO.inspect :ets.match_object(table, {{:first_table, :_}, :_})
Output:
[{{:first_table, 1}, {1}}]
[{{:first_table, 1}, {1}}, {{:first_table, 2}, {5}}]