Module: Hsdq::Session

Included in:
Hsdq
Defined in:
lib/hsdq/session.rb

Instance Method Summary collapse

Instance Method Details

#hsdq_session(session_id, *keys) ⇒ Array, Hash

Retrieve the session hash from the session layer and return the data (see below)

Parameters:

  • session_id (String)

    used to build the unique namespaced key to retrieve the session hash

  • keys (Array of String)

    either an array of keys or nil or nothing

Returns:

  • (Array)

    of values in the order of the keys passed

  • (Hash)

    in the case of no keys passed, return a hash of all the data stored



23
24
25
26
27
28
29
30
31
# File 'lib/hsdq/session.rb', line 23

def hsdq_session(session_id, *keys)
  if keys.any?
    #get only the provided keys
    cx_session.hmget session_key(session_id), *keys
  else
    # get all keys return a hash
    cx_session.hgetall session_key(session_id)
  end
end

#hsdq_session_del(session_id, *keys) ⇒ Object

delete all keys from the session



34
35
36
# File 'lib/hsdq/session.rb', line 34

def hsdq_session_del(session_id, *keys)
  cx_session.hdel session_key(session_id), *keys.flatten
end

#hsdq_session_destroy(session_id) ⇒ Object

delete the whole session hash



39
40
41
# File 'lib/hsdq/session.rb', line 39

def hsdq_session_destroy(session_id)
  cx_session.del session_key(session_id)
end

#hsdq_session_expire(session_id, in_seconds) ⇒ Object

reset the expiration time for the session



44
45
46
# File 'lib/hsdq/session.rb', line 44

def hsdq_session_expire(session_id, in_seconds)
  cx_session.expire session_key(session_id), in_seconds
end

#hsdq_session_expire_in(session_id) ⇒ Object

return the expiration time remaining before expiration



49
50
51
# File 'lib/hsdq/session.rb', line 49

def hsdq_session_expire_in(session_id)
  cx_session.ttl session_key(session_id)
end

#hsdq_session_key?(session_id, key) ⇒ Boolean

check if a key exist in the session hash

Returns:

  • (Boolean)


54
55
56
# File 'lib/hsdq/session.rb', line 54

def hsdq_session_key?(session_id, key)
  cx_session.hexists session_key(session_id), key
end

#hsdq_session_set(session_id, *key_values) ⇒ Object

Store in the session layer in the session hash one or an array of key values (string or json) Create the session hash if it do not exist.

Parameters:

  • session_id (String)

    Use session_key to create the namespaced key based on session_id

  • key_values (Hash or Array of key/values)


8
9
10
11
12
13
14
15
16
# File 'lib/hsdq/session.rb', line 8

def hsdq_session_set(session_id, *key_values)
  key_values = key_values[0].to_a if 1 == key_values.flatten.size && key_values[0].is_a?(Hash)
  hkey = session_key(session_id)

  cx_session.multi do
    cx_session.hmset hkey,  *key_values.flatten
    cx_session.expire hkey, 259200 #3 days todo set by options
  end
end