-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to delete all redbird sessions
* Prepends all session keys with "redbird_sessions" so that it will be easy to identify which keys in the users redis database were set through Redbird * Adds the ExRedis `keys` option to find all keys that start with the redbird session * Adds `delete_all_sessions` to delete all matching keys
- Loading branch information
1 parent
2c5f3c6
commit 3ba4922
Showing
5 changed files
with
107 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule Mix.Tasks.Redbird.DeleteAllSessions do | ||
use Mix.Task | ||
|
||
@shortdoc "Deletes all redbird sessions" | ||
|
||
@moduledoc """ | ||
Deletes all redbird sessions. | ||
mix redbird.delete_all_sessions my_app | ||
The first argument is the app specific key_namespace you set in your plug | ||
session config. If no argument is given, it will delete all redbird sessions. | ||
""" | ||
def run(_args) do | ||
Redbird.start(nil, nil) | ||
Plug.Session.REDIS.namespace | ||
|> delete_all_sessions | ||
end | ||
|
||
def delete_all_sessions(namespace) do | ||
Redbird.Redis.keys("#{namespace}*") | ||
|> Redbird.Redis.del | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
defmodule Mix.Tasks.Redbird.DeleteAllSessionsTest do | ||
use ExUnit.Case | ||
alias Plug.Session.REDIS | ||
|
||
setup do | ||
on_exit fn -> | ||
Mix.Tasks.Redbird.DeleteAllSessions.run([]) | ||
end | ||
end | ||
|
||
test "deletes all redbird session keys" do | ||
key = "redis_session" | ||
conn = %{} | ||
options = [] | ||
REDIS.put(conn, key, %{foo: :bar}, options) | ||
|
||
assert {nil, %{}} = REDIS.get(conn, key, options) | ||
end | ||
|
||
test "deletes user defined namespaced session keys" do | ||
Application.put_env(:redbird, :key_namespace, "test_") | ||
conn = %{} | ||
key = "redis_session" | ||
options = [] | ||
REDIS.put(conn, key, %{foo: :bar}, options) | ||
|
||
Mix.Tasks.Redbird.DeleteAllSessions.run([]) | ||
|
||
assert {nil, %{}} = REDIS.get(conn, "test_" <> key, options) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters