Class: Result

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result) ⇒ Result

Returns a new instance of Result.



24
25
26
27
28
29
# File 'lib/sidekiq_smart_cache/result.rb', line 24

def initialize(result)
  @value = result[:value]
  @created_at = result[:created_at]
  @valid_until = result[:valid_until]
  @cache_prefix = result[:cache_prefix]
end

Instance Attribute Details

#cache_prefixObject

Returns the value of attribute cache_prefix.



5
6
7
# File 'lib/sidekiq_smart_cache/result.rb', line 5

def cache_prefix
  @cache_prefix
end

#created_atObject

Returns the value of attribute created_at.



5
6
7
# File 'lib/sidekiq_smart_cache/result.rb', line 5

def created_at
  @created_at
end

#valid_untilObject

Returns the value of attribute valid_until.



5
6
7
# File 'lib/sidekiq_smart_cache/result.rb', line 5

def valid_until
  @valid_until
end

#valueObject

Returns the value of attribute value.



5
6
7
# File 'lib/sidekiq_smart_cache/result.rb', line 5

def value
  @value
end

Class Method Details

.load_from(cache_tag) ⇒ Object



19
20
21
22
# File 'lib/sidekiq_smart_cache/result.rb', line 19

def self.load_from(cache_tag)
  raw = redis.get(cache_tag)
  new(YAML.safe_load(raw, allowed_classes)) if raw
end

.persist(cache_tag, value, expires_in) ⇒ Object



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

def self.persist(cache_tag, value, expires_in)
  structure = {
    value: value,
    created_at: Time.now,
    valid_until: Time.now + expires_in,
    cache_prefix: SidekiqSmartCache.cache_prefix
  }
  result_lifetime = 1.month.to_i # ??? maybe a function of expires_in ???
  redis.set(cache_tag, structure.to_yaml)
  redis.expire(cache_tag, result_lifetime)
end

Instance Method Details

#fresh?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/sidekiq_smart_cache/result.rb', line 31

def fresh?
  !stale?
end

#stale?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/sidekiq_smart_cache/result.rb', line 35

def stale?
  (Time.now > valid_until) || (cache_prefix != SidekiqSmartCache.cache_prefix)
end