redis_sessions v0.0.0 RedisSessions.Client
The Client module contains functions to interact with the sessions.
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
Create a session
Get a session for an app and token
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
Types
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.
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.)
@tag :skiptest
Examples
iex>{:ok, token} = RedisSessions.Client.create( "exrs-test", "foo", "127.0.0.1", 3600, %{ foo: "bar"} )
...>RedisSessions.Client.activity( "exrs-test" )
{:ok, 1}
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)
@tag :skiptest
Examples
{:ok, %{token: token}} = RedisSessions.Client.create( "exrs-test", "foo", "127.0.0.1", 3600, %{ foo: "bar", ping: "pong"} )
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
@tag :skiptest
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"} }}
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
@tag :skiptest
Examples
iex>{:ok, token} = RedisSessions.Client.create( "exrs-test", "foo", "127.0.0.1", 3600, %{ foo: "bar"} )
...>RedisSessions.Client.kill( "exrs-test", token )
{:killed, 1}
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.
@tag :skiptest
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" )
{:killed, 3 }
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.
@tag :skiptest
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" )
{:killed, 2 }
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
@tag :skiptest
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"} }}
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.)
@tag :skiptest
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, [ %{ id: "foo", r:1, w:1, idle: 1, ttl: 3600 }, %{ id: "bar", r:1, w:1, idle: 1, ttl: 3600 } ] }
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.
@tag :skiptest
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 } ] }