Class: HeuristicCache::TTL

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

Class Method Summary collapse

Class Method Details

.defaultObject



58
59
60
# File 'lib/heuristic_cache/heuristic_cache.rb', line 58

def self.default
  @ttls["default"]
end

.for(object, action) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/heuristic_cache/heuristic_cache.rb', line 62

def self.for(object, action)
  return TTL.default unless object.respond_to?(:updated_at) && !object.updated_at.nil?

  difference    = Time.now.to_i - object.updated_at.to_i
  coefficient   = Coefficient.for(object.class.to_s.downcase, action)
  heuristic_ttl = (difference * coefficient).to_i
  
  return TTL.minimum if TTL.lower_than_minimum?(heuristic_ttl)
  return TTL.maximum if TTL.greater_than_maximum?(heuristic_ttl)

  heuristic_ttl
end

.greater_than_maximum?(value) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/heuristic_cache/heuristic_cache.rb', line 46

def self.greater_than_maximum?(value)
  value > self.maximum
end

.init(yml) ⇒ Object



38
39
40
# File 'lib/heuristic_cache/heuristic_cache.rb', line 38

def self.init(yml)
  @ttls = yml
end

.lower_than_minimum?(value) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/heuristic_cache/heuristic_cache.rb', line 54

def self.lower_than_minimum?(value)
  value < self.minimum
end

.maximumObject



42
43
44
# File 'lib/heuristic_cache/heuristic_cache.rb', line 42

def self.maximum
  @ttls["maximum"]
end

.minimumObject



50
51
52
# File 'lib/heuristic_cache/heuristic_cache.rb', line 50

def self.minimum
  @ttls["minimum"]
end