class Session::SessionClient

Example:

require 'redis_session'
session = Session::SessionClient.new(:prefix => 'example', :host => '10.0.0.31')

session.save('key', 'value') # save key with the value of value without expire, return true if successful
puts session.restore('key')  # will return "value"

session.save('self_destruct', 'in', 10) # save the key self_destruct with the value of 'in', and will terminates in 10 seconds
sleep 10.1
session.restore('self_destruct')      # returns empty hash
session.restore('self_destruct', nil) # returns default value of nil

session.save('boom', { :bomb => 'ball' }, 10) # saving a ruby object
puts session.ttl('boom') # should return the seconds left to the key to live or -1

session.expire('key', 60)   # the key will be gone in 60 seconds
puts session.restore('key') # prints 'value'

puts 'has value' if session.value? 'key' # check if key has a value

session.delete('key')       # deleted it before time :)
                            # it's alias to remove

puts 'still here' if session.key? 'key' # do we have the key ?

Public Class Methods

new(options={}) click to toggle source

Creates an object of SessionClient

Parameters

:host

the ip address or host name of the redis server default localhost

:path

the path to unix socket of redis (instead of :host)

:port

the port number for the host - default 6379

:db

the Redis database number - default 0

:prefix

a prefix string for storing and retriving information

:expire

global expiry of keys in seconds

# File lib/redis_session.rb, line 77
def initialize(options={})
  raise ArgumentError, 'options must be Hash' unless options.kind_of? Hash

  options[:host]   ||= 'localhost' unless options[:path]
  options[:port]   ||= 6379
  options[:db]     ||= 0
  options[:prefix] ||= ''
  options[:expire] ||= 0

  @options = options
  @redis   = Redis.new(@options)
end

Public Instance Methods

delete(key)

Deleting a key from the session

key

The name of the key to be removed

returns

true if successful or false otherwise

Alias for: remove
expire(key, ttl) click to toggle source

Set an expire time in seconds to a key. If the key already has an expire time, it reset it to a new time.

key

The name of the key to set the expire time

ttl

The time in seconds to expire the key

returns

true if successful or false if not

# File lib/redis_session.rb, line 171
def expire(key, ttl)
  a_key = make_key(key)
  @redis.expire(a_key, ttl)
rescue Redis::BaseConnectionError
  raise
rescue Exception
  false
end
key?(key) click to toggle source

Check to see if a key exists

key

The name of the key to check

returns

true if it exists or false otherwise

# File lib/redis_session.rb, line 227
def key?(key)
  a_key = make_key(key)
  @redis.exists a_key

rescue Redis::BaseConnectionError
  raise
rescue Exception
  false
end
prefix() click to toggle source

Getting the prefix name

returns

The prefix string

# File lib/redis_session.rb, line 97
def prefix
  @options[:prefix]
end
prefix=(prefix) click to toggle source

Changing the prefix string _(will not effect existing keys)_

prefix

The new prefix to be set

# File lib/redis_session.rb, line 108
def prefix=(prefix)
  @options[:prefix] = prefix
end
remove(key) click to toggle source

Deleting a key from the session

key

The name of the key to be removed

returns

true if successful or false otherwise

# File lib/redis_session.rb, line 208
def remove(key)
  a_key = make_key(key)
  @redis.del(a_key)

rescue Redis::BaseConnectionError
  raise
rescue Exception
  false
end
Also aliased as: delete
restore(key, default={}) click to toggle source

Restoring a key's value or providing a default value instead

key

The name of the key to restore

default

The value to provide if no value was given. Default empty Hash

returns

The value of the key or default value

# File lib/redis_session.rb, line 151
def restore(key, default={})
  a_key = make_key(key)
  data  = @redis.get(a_key)
  data.nil? ? default : Marshal.load(data)
rescue Redis::BaseConnectionError
  raise
rescue Exception
  default
end
save(key, value, ttl = nil) click to toggle source

Saving a key with a value

key

the name of the key to be saved

value

the value to save. Can be any Ruby object

ttl

expiry time to the key. Default nil.

returns

true if successful or false otherwise

Note

If expire was set and ttl is nil, then the key will have expire by the :expire option

# File lib/redis_session.rb, line 125
def save(key, value, ttl = nil)
  a_key  = make_key(key)
  a_data = Marshal.dump(value)
  ttl ||= @options[:expire]
  if ttl > 0
    @redis.setex(a_key, ttl, a_data)
  else
    @redis.set(a_key, a_data)
  end
  true
rescue Redis::BaseConnectionError
  raise
rescue Exception
  false
end
scan_by() { |value| ... } click to toggle source

scan for partial value in redis Using a block you can create your own criteria for lookup:

Example: let's say there is a key named “foo”, and the value is a hash: {:click=>true, :reading=>0}

ret = session.scan_by do |x|

next unless x.kind_of? Hash
next unless x.key? :click

x[:click] == true

end

> {“foo”=>{:click=>true, :reading=>0}}

If found return a hash of key => value If not found, return nil

# File lib/redis_session.rb, line 287
def scan_by(&block)
  key   = ''
  value = ''
  @redis.keys('*').each do |x|
    next unless @redis.type(x) == 'string'
    
    value = Marshal.load(@redis.get(x)) rescue next # not really a ruby object
    if yield value
      key   = x
      break
    end
  end
  
  return nil if key.empty?
  
  { key => value }

rescue Redis::BaseConnectionError
  raise
rescue Exception #=> e
  # puts "exception: #{e}\n#{e.backtrace.join("\n")}"
  nil
end
ttl(key) click to toggle source

Examines how much time left to a key in seconds

key

The name of a key to check the ttl

returns

Returns the number of seconds left, or -1 if key does not exists or no ttl exists for it

# File lib/redis_session.rb, line 189
def ttl(key)
  a_key = make_key(key)
  @redis.ttl(a_key)

rescue Redis::BaseConnectionError
  raise
rescue Exception
  -1
end
value?(key) click to toggle source

Check if a key has a value

key

The name of the key to check

returns

true if exists or false otherwise

# File lib/redis_session.rb, line 246
def value?(key)
  a_key = make_key(key)
  @redis.get(a_key) != nil

rescue Redis::BaseConnectionError
  raise
rescue Exception
  false
end

Private Instance Methods

make_key(key) click to toggle source

Generate a key with a prefix string

key

The name of the key to generate

returns

the key with the prefix

# File lib/redis_session.rb, line 321
def make_key(key)
  "#{@options[:prefix]}#{key}"
end