CastLouis

CastLouis

ETS Cache multiple tables from Ecto

Hi
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?

Marked As Solved

OvermindDL1

OvermindDL1

First, I’d be absolutely sure that cacheing this is good, postgresql is very very fast and you have to worry about concurrency issues and staleness with caches.

Second, I’d recommend using CacheX or so, you can set up a resolver so you ask cachex for something, if it does not exist then it goes to, say, your DB to fetch it and cache it. It has timeout support, janitors, etc… All built on ETS.

Third, if you want to key something by a table with an identifier, than wrap those up in a tuple in the key position, so like:

:ets.insert(table, {{:first_table,1}, :other_data})
:ets.insert(table, {{:first_table,5}, :other_data})
:ets.insert(table, {{:second_table,1}, :other_data})

Also Liked

aseigo

aseigo

Looking at the code, it doesn’t seem to de-dup-fetch-on-cache-miss. This is understandable imho as it is unclear from the library’s limited view into your application when the fallback should be run only once, or called on each and every miss no matter what. Buuuuut … it does look like you could serialize this yourself. There are various ways the serialization could be achieved without much effort through the semicreative use of processes and messages :slight_smile:

OvermindDL1

OvermindDL1

It incurs no built-in limits of the sort. If you use the direct interface then it will be as multi-threaded as it can possibly be. If you use CacheX Transactions (basically a ‘get’ with also saying what key’s you want to ‘lock’ during the transaction) then you can prevent concurrent access and have a single one done always. In general CacheX is designed to be as fast as possible, calling out to no other processes, direct ETS access, secondary janitor processes, etc… etc…

EDIT: And of course you can always have the fallback/resolver call out to a genserver or something too, that way no changes to user code are necessary.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement