Skip to content

Instantly share code, notes, and snippets.

@tsloughter
Created July 14, 2011 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsloughter/1083068 to your computer and use it in GitHub Desktop.
Save tsloughter/1083068 to your computer and use it in GitHub Desktop.
%%%-------------------------------------------------------------------
%%% @author Tristan Sloughter <>
%%% @copyright (C) 2011, Tristan Sloughter
%%% @doc
%%%
%%% @end
%%% Created : 5 Jul 2011 by Tristan Sloughter <>
%%%-------------------------------------------------------------------
-module(ct_model_sessions).
-include("ct_model.hrl").
%% API
-export([is_valid/1]).
-record(ct_model_sessions, {id=uuid() :: ct_model_types:ct_key(),
created=idioms:time_in_seconds() :: integer()}).
%% never expire session
-define(INFINTE_SESSION, true).
-define(EXPIRE, infinity).
%%%===================================================================
%%% API
%%%===================================================================
-spec is_valid(list()) -> true | false.
is_valid(SessionID) ->
case ct_db:get(?MODULE, SessionID) of
not_found ->
false;
Session ->
not(expired(Session))
end.
%%%===================================================================
%%% Internal functions
%%%===================================================================
uuid() ->
<<I:224/integer>> = sha2:hexdigest224(term_to_binary({make_ref(), now()})),
erlang:integer_to_list(I, 16).
-ifdef(INFINITE_SESSION).
expired(_Session) ->
false.
-else.
expired(Session) ->
(idioms:time_in_seconds() - Session#ct_model_sessions.created) > ?EXPIRE.
-endif.
%%%===================================================================
%%% Test functions
%%%===================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment