Class: Githu3::Cache::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/githu3/cache/redis.rb

Constant Summary collapse

DEFAULTS =
{ 
  :host       => 'localhost', 
  :port       => 6379, 
  :namespace  => :githu3,
  :expire     => 120
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Redis

Returns a new instance of Redis.



16
17
18
19
20
# File 'lib/githu3/cache/redis.rb', line 16

def initialize opts={}
  @config = DEFAULTS.merge(opts || {})
  redis = ::Redis.new :host => @config[:host], :port => @config[:port]
  @store = ::Redis::Namespace.new(@config[:namespace].to_sym, :redis => redis)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/githu3/cache/redis.rb', line 7

def config
  @config
end

#storeObject (readonly)

Returns the value of attribute store.



7
8
9
# File 'lib/githu3/cache/redis.rb', line 7

def store
  @store
end

Instance Method Details

#get(k) ⇒ Object



22
23
24
25
26
# File 'lib/githu3/cache/redis.rb', line 22

def get k
  val = @store[k.to_s]
  val = Marshal.load val unless val.nil?
  val
end

#set(k, val, opts = {}) ⇒ Object



28
29
30
31
# File 'lib/githu3/cache/redis.rb', line 28

def set k, val, opts={}
  @store[k.to_s] = Marshal.dump(val)
  @store.expire k.to_s, (opts[:expire] || @config[:expire]).to_i
end