redis_sessions v0.1.2 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

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

Wipe all deprecated sessions

Types

ip :: {integer, integer, integer, integer}
session :: %{id: String.t, r: integer, w: integer, idle: integer, ttl: integer, d: Map.t}

Functions

activity(app, dt \\ 600, server \\ node())

Specs

activity(app, integer, node) ::
  {:ok, integer} |
  {:error, String.t}

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}}
create(app, id, ip, ttl \\ 3600, data \\ nil, server \\ node())

Specs

create(app, id, ip, integer, Map.t, node) :: boolean

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, server \\ node())

Specs

get(app, token, node) ::
  {:ok, session} |
  {:error, String.t}

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"} }}
kill(app, token, server \\ node())

Specs

kill(app, token, node) ::
  {:kill, integer} |
  {:error, String.t}

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, server \\ node())

Specs

killall(app, node) ::
  {:kill, integer} |
  {:error, String.t}

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, server \\ node())

Specs

killsoid(app, id, node) ::
  {:kill, integer} |
  {:error, String.t}

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, server \\ node())

Specs

set(app, token, Map.t, node) ::
  {:ok, session} |
  {:error, String.t}

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 long
  • d (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, server \\ node())

Specs

soapp(app, integer, node) ::
  {:ok, [session]} |
  {:error, String.t}

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, server \\ node())

Specs

soid(app, id, node) ::
  {:ok, [session]} |
  {:error, String.t}

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()

Specs

start_link :: true

start genserver

wipe(server \\ node())

Specs

wipe(node) :: :ok

Wipe all deprecated sessions

Parameters