Class: Riddl::Utils::OAuth2::Helper::Tokens::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/riddl/utils/oauth2-helper.rb

Overview

{{{

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Redis

Returns a new instance of Redis.



15
16
17
# File 'lib/ruby/riddl/utils/oauth2-helper.rb', line 15

def initialize(url)
  @redis = ::Redis.new(:url => url)
end

Instance Method Details

#[](key) ⇒ Object



19
20
21
# File 'lib/ruby/riddl/utils/oauth2-helper.rb', line 19

def [](key)
  get(key)
end

#delete(key) ⇒ Object



50
51
52
53
54
# File 'lib/ruby/riddl/utils/oauth2-helper.rb', line 50

def delete(key)
  value = @redis.get key
  @redis.del key
  value
end

#eachObject



31
32
33
34
35
36
37
38
39
# File 'lib/ruby/riddl/utils/oauth2-helper.rb', line 31

def each
  if block_given?
    @redis.keys.each do |e| 
      yield e, get(e)
    end
  else
    @redis.keys.lazy.map{|e| [e,get(e)]}
  end
end

#get(key) ⇒ Object



23
24
25
# File 'lib/ruby/riddl/utils/oauth2-helper.rb', line 23

def get(key)
  @redis.get key
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/ruby/riddl/utils/oauth2-helper.rb', line 27

def key?(key)
  @redis.exists(key)
end

#set(key, value, dur = nil) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/ruby/riddl/utils/oauth2-helper.rb', line 41

def set(key,value,dur=nil)
  value = value.is_a?(String) ? value.to_s : (JSON::generate(value) rescue '')
  @redis.multi do
    @redis.set key, value
    @redis.expire key, dur unless dur.nil?
  end
  nil
end