fireproofsocks

fireproofsocks

Cache package that sits in front of a slow MFA?

I have a pretty simple need – simple enough that it’s not a big deal to implement it as a GenServer or similar, but it’s a common enough use case that someone else must have come up with something that fits the bill. The basic need is to have a cache sit in front of a slow MFA call, e.g. a database query. So the cache acts as the source-of-truth, and the MFA call gets applied periodically so it stays “reasonably” up-to-date, and I can hit the database once every few minutes instead of once every few milliseconds and all would be well with the world.

I’m imagining a GenServer that could be started up something like:

GenCache.start_link(%{name: :db_query1, module: MyApp, function: :slow_db_query1, args: [], refresh_interval_ms: 300_000})

which would call MyApp.slow_db_query1/0 every 5 minutes and cache the result in the GenServer state. I could access the cached data via something like:

GenCache.get(:db_query1)

or force it to refresh via

GenCache.refresh(:db_query1)

and a few other functions – hopefully this expresses the idea.

Is anyone aware of such a package that could be pointed at arbitrary existing functions? I’m not sure if this simple use case would even justify a stand-alone package, but I thought the community would have some valuable thoughts on it. Thanks in advance!

Most Liked

hauleth

hauleth

Cachex for example will fit the bill.

Last Post!

lud

lud

There is a bit of boilerplate but not too much, and then you just have to provide the fallback callback in case of cache miss.

Now this strategy works well but it is different than using whatever is in the cache, and imperatively update the cache regularly.

Where Next?

Popular in Questions Top

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
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement