SN_SESSION

NAME

sn_session - Implement sessions for Extenso

SYNOPSIS

sn_session_get(key:string[, db: string]);

sn_session_set(key:string,value:string[,exp:string,db:string]);

sn_session_del(keys:string[db:string]);

sn_session_exp([db:string]);

DESCRIPTION

These functions are used to implement a session.

session_set get the current session cookie, the key passed in parameter and the expiration date if any and save the value in the database. If the key already exist, it is replace.

The database right now is mariadb and the table is sn_sessions. If the parameter db is used, than the database will be a key/data database which is at least 5 times faster than mariadb.

session_get retrive a value corresponding to the key and the cookie of the user.

session_del is used to delete keys from the database.

session_exp is used to remove expire keys from the database.

key
key to store data.
exp
Expiration date in the format YYYY-MM-DD HH:MM:SS or in the form +... The operations is a +, followed by a number followed by a single character which can be:
value
Value for the key in string format.
withexp
If withexp is true, *session_get* will return an array with value and expiration date.
keys
List of keys to delete
db
Specify a hash database

RETURNS

sn_session_get
Return the value
sn_session_set
return -1 if cookie is not valid, -2 if a sql error occured and null otherwise

EXAMPLES


{{ %include "/extenso/functions/sn_session.snc"; }}

\{{ 
sn_session_set("captcha",c[1], exp:"+300s");
sn_session_get("captcha");
sn_session_del("key1","key2");

a=time();
sn_session_exp(db:"/hash/session.db");
key = random(min:1, max:1000, init:true, seed:1);
value = sn_session_get(db:"/hash/session.db", key);
n=0;
for (i=1;i<10000;++i) do
	key = random(min:1, max:1000);
	value = sn_session_get(db:"/hash/session.db", key);
	if value == null then
		value = random(min:1, max:1000);
		sn_session_set(key,value,db:"/hash/session.db");
	else
		n++;
	endif
endfor
b=time();
d = (b.secs - a.secs) * 1000000;
d += (b.msecs - a.msecs) * 1000;
d += (b.usecs - a.msecs);
s = d / 1000000;
tmp = s * 1000000;
d = d - tmp;
ms = d / 1000;
tmp = ms * 1000;
d = d - tmp;
"test1: "; s; ":"; ms; ":"; d; ","; n;

}}

SEE ALSO

AUTHOR

Written by Pierre Laplante

MODIFICATIONS

1.0 2014-09-09 21:24:14 laplante@sednove.com