qhwa

qhwa

Data_fetcher - a small library that can ease your fetch-and-cache jobs

data_fetcher is a small library that can ease fetch-and-cache jobs for your Elixir projects.

Occasionally we need some data, typically used as configurations, from an external source. Chances are these configurations won’t change too often, so we may want to cache them and refresh the cached data periodically.

We may think these kinds of fetch-and-cache jobs are too simple for a library. Well, I thought that too, until I found myself repeating myself tackling the same challenges:

  • to set up the scheduler, either by sending a message to a scheduler with Process.send_after or send messages to the GenServer itself
  • to handle the failures in fetching the data, usually, just restart the job
  • to prevent immediately hitting the external source if there are simultaneous requests and the cache expires at the same time for them
  • to serve the simultaneous requests while we are fetching the data at the first time
  • no to block the whole application from booting up as the fetch job may be slow and the data is required by some of the endpoints only
  • to have a decent performance, no matter how big the data size is

So, I made a library for myself called data_fetcher, which solves the above problems. Please check it out if you have the same need.

I am grateful for the help from @wojtekmach who gave me a guide on the original idea and pointed out the problems on the first “design”, which I think most new developers may have too when not familiar with OTP. I made some mistakes on the first working version and (after reading related chapters from Elixir in Action) got it rewritten now. :stuck_out_tongue:

Again, please check it out and any feedback is welcome!

Cheers!

Ref:
Online doc | Github

Most Liked

dimitarvp

dimitarvp

Cool. I always enjoy libraries that wrap OTP boilerplate. Thanks for this.

As a suggestion, I’d also add a function that can generate a stereotypical child spec, like this:

def child_spec_for_function_and_interval(func, minutes)
    when is_function(func, 1) and is_integer(minutes) and minutes > 0 do
  {
      DataFetcher,
      name: :my_fetcher,
      fetcher: func,
      interval: :timer.minutes(minutes)
    }
end

It’s going to be best if the name is shorter though.

qhwa

qhwa

Aha, I’m not the only one thinking that the helper function is too clumsy! Thanks for the advice. Would you think it will be better?

Supervisor.init([
  {DataFetcher, fetcher_options()}
  ...
], strategy: :one_for_one)

...
defp fetcher_options,
  do: [
    name: :my_fetcher,
    fetcher: my_func,
    interval: :timer.minutes(20)
  ]
...

Where Next?

Popular in Libraries Top

tompave
Hello there, I would like to share a feature toggles library (AKA feature flags) I’ve been working on. The main package is FunWithFlags...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
mcrumm
If you would like to migrate away from node/npm/webpack while still using sass, the dart_sass package provides a installer and runner for...
New
michalmuskala
Another small library today. PersistentEts Hex: persistent_ets | Hex GitHub: GitHub - michalmuskala/persistent_ets Ets table backed by...
New
mbuhot
EctoJob A transactional job queue built with Ecto, PostgreSQL and GenStage Available on Hex.pm: ecto_job | Hex Docs: API Reference — ec...
New
Antrater
Hi everyone! I’m thrilled to announce a huge thing. We have been developing Elixir Moon Design System for quite a while. We are finally ...
New
engineeringdept
I’ve just released the first version of Snap, an Elasticsearch client. It borrows ideas about application structure and process managemen...
New
OvermindDL1
I created a new library (rather I pulled out a couple files from my big project), it manages an operating system PID file for the BEAM. ...
New
anshuman23
Hello all, I have been working on my proposed project called Tensorflex as part of Google Summer of Code 2018.. Tensorflex can be used f...
New
wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New

Other popular topics Top

sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Sub Categories:

We're in Beta

About us Mission Statement