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.

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

Specs

app() :: String.t()

Specs

id() :: String.t()

Specs

ip() :: {integer(), integer(), integer(), integer()}

Specs

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

Specs

token() :: String.t()

Link to this section Functions

Link to this function

activity(app, dt \\ 600)

Specs

activity(app(), integer()) :: {: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}}
Link to this function

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

create(app, id, ip, ttl \\ 3600, data \\ nil)

Specs

create(app(), id(), ip(), integer(), Map.t()) :: 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"} )
Link to this function

get(app, token)

Specs

get(app(), token()) :: {: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"} }}

Callback implementation for GenServer.init/1.

Link to this function

kill(app, token)

Specs

kill(app(), token()) :: {: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} }

Specs

killall(app()) :: {: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 }}
Link to this function

killsoid(app, id)

Specs

killsoid(app(), id()) :: {: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 }}
Link to this function

set(app, token, data \\ nil)

Specs

set(app(), token(), Map.t()) :: {: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"} }}
Link to this function

soapp(app, dt \\ 600)

Specs

soapp(app(), integer()) :: {: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 } ] } }

Specs

soid(app(), id()) :: {: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 } ] }
Link to this function

start_link(args)

Specs

start_link(any()) :: true

start genserver

Specs

wipe() :: :ok

Wipe all deprecated sessions

Parameters