Comments (9)
@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.
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.
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.
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.
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.
@Hanspagh could you add some description to it? Otherwise in the PR would be good as well.
from guardian_db.
@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.
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.
Lets move the conversation over #131
The Adapter is already there with the Ecto implementation by default
from guardian_db.
Related Issues (20)
- separated token schemes HOT 4
- hash of tokens HOT 2
- Revoking Token thats already been removed throws error rather than returning error HOT 1
- Dependency Update
- Changelog updates since 0.8 HOT 3
- How to add more columns to 'guardian_tokens' table? HOT 5
- Compatibility with Guardian 2.0 HOT 2
- Update ecto_sql(currently at 3.1.0) dependency to the latest version HOT 1
- Do not fix ecto version HOT 2
- Documentation: need to update migrations if using non-default table name
- Guardian DB Redis Repo HOT 11
- Ecto.StaleEntryError when using Guardian.DB.on_revoke HOT 8
- cut a new release? HOT 1
- Add a column to optionally store the jti of the refresh token used to generate an access token. HOT 2
- Publish new release HOT 1
- `Guardian.DB.Sweeper` incompatible with old worker API HOT 1
- function Guardian.DB.on_refresh/2 is undefined or private HOT 3
- guardian_db fails to compile with Ecto 3 rc (undefined function migrations_path/1) HOT 20
- how to use in two or more servers with login just one time? share pwd HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from guardian_db.