Skip to content

Commit

Permalink
Update usage example for store base with Cachex
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultzer committed Jan 3, 2020
1 parent d38efab commit 32b0d5a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* Added mailer rate limitation section to [production checklist guide](guides/production_checklist.md)
* [`Pow.Plug.Session`] Added section on session expiration to the docs
* Updated instructions in [umbrella project guide](guides/umbrella_project.md) to Elixir 1.9
* [`Pow.Store.Backend.Base`] Updated usage example with Cachex

## v1.0.15 (2019-11-20)

Expand Down
57 changes: 54 additions & 3 deletions lib/pow/store/backend/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,61 @@ defmodule Pow.Store.Backend.Base do
## Usage
defmodule MyApp.RedisCache do
@behaviour Base
This is an example using [Cachex](https://hex.pm/packages/cachex):
# ...
defmodule MyApp.CachexCache do
@behaviour Pow.Store.Backend.Base
alias Pow.Config
@cachex_tab __MODULE__
@impl true
def put(config, record_or_records) do
records =
record_or_records
|> List.wrap()
|> Enum.map(fn {key, value} ->
{wrap_namespace(config, key), value}
end)
Cachex.put_many(@cachex_tab, records, ttl: Config.get(config, :ttl))
end
@impl true
def delete(config, key) do
key = wrap_namespace(config, key)
Cachex.del(@cachex_tab, key)
end
@impl true
def get(config, key) do
key = wrap_namespace(config, key)
case Cachex.get(@cachex_tab, key) do
{:ok, nil} -> :not_found
{:ok, value} -> value
end
end
@impl true
def all(config, match_spec) do
query = Cachex.Query.create(match_spec, :key)
@cachex_tab
|> Cachex.stream!(query)
|> Enum.map(fn {key, value} -> {unwrap_namespace(key), value} end)
end
defp wrap_namespace(config, key) do
namespace = Config.get(config, :namespace, "cache")
[namespace | List.wrap(key)]
end
defp unwrap_namespace([_namespace, key]), do: key
defp unwrap_namespace([_namespace | key]), do: key
end
"""
alias Pow.Config
Expand Down

0 comments on commit 32b0d5a

Please sign in to comment.