Class: FreshRedis

Inherits:
Object
  • Object
show all
Defined in:
lib/fresh_redis.rb

Class Method Summary collapse

Class Method Details

.freshen(name, expire_age = nil, format = :ruby, &blk) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fresh_redis.rb', line 7

def self.freshen(name, expire_age = nil, format = :ruby, &blk)
  self.unset(name) if self.skip_cache?

  unless contents = $redis.get(name)
    contents = self.set(name, blk.call())
    $redis.expire(name, expire_age.seconds) if expire_age
  end

  contents = JSON.parse(contents) unless (format == :json)

  return contents
end

.get(name) ⇒ Object



2
3
4
5
# File 'lib/fresh_redis.rb', line 2

def self.get(name)
  content = $redis.get(name)
  return content.present? ? JSON.parse(content) : content
end

.set(name, contents) ⇒ Object



20
21
22
23
24
25
# File 'lib/fresh_redis.rb', line 20

def self.set(name, contents)
  contents = JSON.generate(contents)
  $redis.set(name, contents)

  return contents
end

.unset(name) ⇒ Object



27
28
29
# File 'lib/fresh_redis.rb', line 27

def self.unset(name)
  return $redis.del(name) == 1
end