Giter Club home page Giter Club logo

Comments (9)

Hanspagh avatar Hanspagh commented on July 19, 2024 2

@doomspork Yes exactly. My idea was to have a gen server in front of the DB as a cache layer, and then only save to the database once in a while. This would enable the users to still be able to invalidate tokens without the cost of doing a db lookup every time

from guardian_db.

doomspork avatar doomspork commented on July 19, 2024 2

This is based on #129 & #131, an adapter that merges both together for a DB-cache combo.

This is untested

defmodule Guardian.DB.CachedEctoAdapter do
  @moduledoc """
  An experimental Adapter that leverages both Ecto and ETS for a cached backed
  storage lookup.
  """

  @behaviour Guardian.DB.Adapter

  alias Guardian.DB.{EctoAdapter, ETSAdapter, Token}

  @impl true
  def insert(changeset, opts) do
    with {:ok, token} <- EctoAdapter.insert(changeset, opts),
         {:ok, _} <- ETSAdapter.insert(token, opts) do
      {:ok, token}
    end
  end

  @doc """
  Attempt to fetch our token from our ETS cache first, if that fails look up 
  in the database and store the retrieved token in the cache
  """
  @impl true
  def one(claims, opts) do
    with nil <- ETSAdapter.one(claims, opts),
         %Token{} = token <- EctoAdapter.one(claims, opts),
         {:ok, _} <- ETSAdapter.insert(token, opts) do
      token
    else
      _ -> nil
    end
  end

  @impl true
  def delete(token, opts) do
    ETSAdapter.delete(token, opts)
    EctoAdapter.delete(token, opts)
  end

  @impl true
  def delete_by_sub(sub, opts) do
    ETSAdapter.delete_by_sub(sub, opts)
    EctoAdapter.delete(sub, opts)
  end

  @impl true
  def purge_expired_tokens(exp, opts) do
    ETSAdapter.purge_expired_tokens(exp, opts)
    EctoAdapter.purge_expired_tokens(exp, opts)
  end
end

from guardian_db.

doomspork avatar doomspork commented on July 19, 2024 1

Awesome! I am going to be leveraging GuardianDB in a project at work and will keep an eye on the number of queries and how to reduce them (something I've been trying to been doing with our legacy systems already).

from guardian_db.

alexfilatov avatar alexfilatov commented on July 19, 2024 1

really nice idea with GenServer, I did this but with Redis, maybe this will help someone https://github.com/alexfilatov/guardian_db_redis (also maybe make this as a PR #119)

from guardian_db.

aleDsz avatar aleDsz commented on July 19, 2024 1

I was thinking about the issue #119 and concepting the idea of an Adapter for the DB layer. As we can improve the access to database, with good indexes to reduce the reading strategy, we can make the GenServer became real and load all tokens at startup.

from guardian_db.

yordis avatar yordis commented on July 19, 2024

@Hanspagh could you add some description to it? Otherwise in the PR would be good as well.

from guardian_db.

doomspork avatar doomspork commented on July 19, 2024

@Hanspagh are you thinking we leverage a GenServer as a sort of cache to avoid having to go to the DB for every check?

from guardian_db.

doomspork avatar doomspork commented on July 19, 2024

With the new Adapter pattern @alexfilatov put together this is now possible with a custom adapter that wraps a cache around ecto. After we work through #129 I'll take a look at tossing together a ETS Adapter and a CachedEcto adapter.

from guardian_db.

yordis avatar yordis commented on July 19, 2024

Lets move the conversation over #131

The Adapter is already there with the Ecto implementation by default

from guardian_db.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.