RedisSessions.Client (redis_sessions v0.3.0)
The Client module contains functions to interact with the sessions.
Link to this section Summary
Functions
Query the amount of active session within the last 10 minutes (600 seconds). Note: Multiple sessions from the same user id will be counted as one.
Returns a specification to start this module under a supervisor.
Create a session
Get a session for an app and token
Callback implementation for GenServer.init/1
.
Kill a session for an app and token.
Kill all sessions of an app
Kill all sessions of an id within an app
Set/Update/Delete custom data for a single session.
All custom data is stored in the d
object which is a simple hash object structure.
Get all sessions of an app there were active within the last 10 minutes (600 seconds).
Get all sessions within an app that belong to a single id. This would be all sessions of a single user in case he is logged in on different browsers / devices.
start genserver
Wipe all deprecated sessions
Link to this section Types
app()
Specs
app() :: String.t()
id()
Specs
id() :: String.t()
ip()
Specs
session()
Specs
token()
Specs
token() :: String.t()
Link to this section Functions
activity(app, dt \\ 600)
Specs
Query the amount of active session within the last 10 minutes (600 seconds). Note: Multiple sessions from the same user id will be counted as one.
Parameters
app
(Binary) The app id (namespace) for this session. Must be [a-zA-Z0-9_-] and 3-20 chars long.dt
(Integer) Delta time. Amount of seconds to check (e.g. 600 for the last 10 min.)
Examples
iex>{:ok, _} = RedisSessions.Client.create( "exrs-test", "foo", "127.0.0.1", 3600, %{ "foo" => "bar"} )
...>RedisSessions.Client.activity( "exrs-test" )
{:ok, %{activity: 1}}
child_spec(init_arg)
Returns a specification to start this module under a supervisor.
See Supervisor
.
create(app, id, ip, ttl \\ 3600, data \\ nil)
Specs
Create a session
Parameters
app
(Binary) The app id (namespace) for this session. Must be [a-zA-Z0-9_-] and 3-20 chars long.id
(Binary) The user id of this user. Note: There can be multiple sessions for the same user id. If the user uses multiple client devices.ip
(Binary) IP address of the user. This is used to show all ips from which the user is logged in.ttl
(Integer) optional The "Time-To-Live" for the session in seconds. Default: 7200.d
(Map) optional Additional data to set for this sessions. (see the "set" method)
Examples
{:ok, %{token: token}} = RedisSessions.Client.create( "exrs-test", "foo", "127.0.0.1", 3600, %{ "foo" => "bar", "ping" => "pong"} )
get(app, token)
Specs
Get a session for an app and token
Parameters
app
(Binary) The app id (namespace) for this session. Must be [a-zA-Z0-9_-] and 3-20 chars long.token
(Binary) The generated session token. Must be [a-zA-Z0-9] and 64 chars long
Examples
{:ok, %{ token: token }} = RedisSessions.Client.create( "exrs-test", "foo", "127.0.0.1", 3600, %{ "foo" => "bar"} )
RedisSessions.Client.get( "exrs-test", token )
# {:ok, %{ id: "foo", r: 2, w: 1, idle: 1, ttl: 3600, ip: "127.0.0.1", d: %{"foo" => "bar"} }}
init(init_arg)
Callback implementation for GenServer.init/1
.
kill(app, token)
Specs
Kill a session for an app and token.
Parameters
app
(Binary) The app id (namespace) for this session. Must be [a-zA-Z0-9_-] and 3-20 chars long.token
(Binary) The generated session token. Must be [a-zA-Z0-9] and 64 chars long
Examples
iex>{:ok, %{token: token}} = RedisSessions.Client.create( "exrs-test", "foo", "127.0.0.1", 3600, %{ "foo" => "bar"} )
...>RedisSessions.Client.kill( "exrs-test", token )
{:ok, %{kill: 1} }
killall(app)
Specs
Kill all sessions of an app
Parameters
app
(Binary) The app id (namespace) for this session. Must be [a-zA-Z0-9_-] and 3-20 chars long.
Examples
iex>{:ok, tokenA1} = RedisSessions.Client.create( "exrs-test", "foo", "127.0.0.1", 3600 )
...>{:ok, tokenB1} = RedisSessions.Client.create( "exrs-test", "bar", "127.0.0.1", 3600 )
...>{:ok, tokenA2} = RedisSessions.Client.create( "exrs-test", "foo", "192.168.0.42", 3600 )
...>{:ok, tokenA2} = RedisSessions.Client.create( "exrs-test2", "foo", "192.168.0.42", 3600 )
...>RedisSessions.Client.killall( "exrs-test" )
{:ok, %{ kill: 3 }}
killsoid(app, id)
Specs
Kill all sessions of an id within an app
Parameters
app
(Binary) The app id (namespace) for this session. Must be [a-zA-Z0-9_-] and 3-20 chars long.id
(Binary) The user id of this user.
Examples
iex>{:ok, tokenA1} = RedisSessions.Client.create( "exrs-test", "foo", "127.0.0.1", 3600 )
...>{:ok, tokenB1} = RedisSessions.Client.create( "exrs-test", "bar", "127.0.0.1", 3600 )
...>{:ok, tokenA2} = RedisSessions.Client.create( "exrs-test", "foo", "192.168.0.42", 3600 )
...>{:ok, tokenA2} = RedisSessions.Client.create( "exrs-test2", "foo", "192.168.0.42", 3600 )
...>RedisSessions.Client.killsoid( "exrs-test", "foo" )
{:ok, %{ kill: 2 }}
set(app, token, data \\ nil)
Specs
Set/Update/Delete custom data for a single session.
All custom data is stored in the d
object which is a simple hash object structure.
d
might contain a map with one or more keys with the following types: binary
, number
, boolean
, nil
.
Keys with all values except nil
will be stored. If a key containts nil
the key will be removed.
Note: If d
already contains keys that are not supplied in the set request then these keys will be untouched.
Parameters
app
(Binary) The app id (namespace) for this session. Must be [a-zA-Z0-9_-] and 3-20 chars long.token
(Binary) The generated session token. Must be [a-zA-Z0-9] and 64 chars longd
(Map) optional Data to set. Must be a map with keys whose values only consist of binaries, numbers, boolean and nil.
Returns
{:ok, session }
the session data after change
Examples
{:ok, token} = RedisSessions.Client.create( "exrs-test", "foo", "127.0.0.1", 3600, %{ "foo" => "bar"} )
RedisSessions.Client.set( "exrs-test", token, %{ "foo" => "buzz"} )
# {:ok, %{ id: "foo", r:1, w:2, idle: 3, ttl: 3600, d: %{"foo" => "buzz"} }}
soapp(app, dt \\ 600)
Specs
Get all sessions of an app there were active within the last 10 minutes (600 seconds).
Parameters
app
(Binary) The app id (namespace) for this session. Must be [a-zA-Z0-9_-] and 3-20 chars long.dt
(Integer) Delta time. Amount of seconds to check (e.g. 600 for the last 10 min.)
Examples
iex>{:ok, tokenA} = RedisSessions.Client.create( "exrs-test", "foo", "127.0.0.1", 3600 )
...>{:ok, tokenB} = RedisSessions.Client.create( "exrs-test", "bar", "127.0.0.1", 3600 )
...>RedisSessions.Client.soapp( "exrs-test" )
{:ok, %{ sessions: [ %{ id: "foo", r: 1, w: 1, idle: 1, ttl: 3600, d: nil }, %{ id: "bar", r: 1, w: 1, idle: 1, ttl: 3600, d: nil } ] } }
soid(app, id)
Specs
Get all sessions within an app that belong to a single id. This would be all sessions of a single user in case he is logged in on different browsers / devices.
Parameters
app
(Binary) The app id (namespace) for this session. Must be [a-zA-Z0-9_-] and 3-20 chars long.id
(Binary) The user id of this user.
Examples
iex>{:ok, tokenA1} = RedisSessions.Client.create( "exrs-test", "foo", "127.0.0.1", 3600 )
...>{:ok, tokenB1} = RedisSessions.Client.create( "exrs-test", "bar", "127.0.0.1", 3600 )
...>{:ok, tokenA2} = RedisSessions.Client.create( "exrs-test", "foo", "192.168.0.42", 3600 )
...>RedisSessions.Client.soid( "exrs-test", "foo" )
{:ok, [ %{ id: "foo", r:1, w:1, idle: 1, ttl: 3600 }, %{ id: "bar", r:1, w:1, idle: 1, ttl: 3600 } ] }
start_link(args)
Specs
start_link(any()) :: true
start genserver
wipe()
Specs
wipe() :: :ok
Wipe all deprecated sessions