Module: Familia::Features::Expiration

Extended by:
ClassMethods
Defined in:
lib/familia/features/expiration.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/familia/features/expiration.rb', line 21

def self.included base
  Familia.ld "[#{base}] Loaded #{self}"
  base.extend ClassMethods

  # Optionally define ttl in the class to make
  # sure we always have an array to work with.
  unless base.instance_variable_defined?(:@ttl)
    base.instance_variable_set(:@ttl, @ttl) # set above
  end
end

Instance Method Details

#ttlObject



36
37
38
# File 'lib/familia/features/expiration.rb', line 36

def ttl
  @ttl || self.class.ttl
end

#ttl=(v) ⇒ Object



32
33
34
# File 'lib/familia/features/expiration.rb', line 32

def ttl=(v)
  @ttl = v.to_f
end

#update_expiration(ttl = nil) ⇒ Boolean

Note:

If TTL is zero, your data gets a VIP pass to the Redis eternity club. Fancy, huh?

Yo, check it out! We’re gonna give our Redis data an expiration date!

It’s like slapping a “Best Before” sticker on your favorite snack, but for data. How cool is that?

Examples:

Teaching your pet rock the concept of mortality

rocky.update_expiration(86400) # Dwayne gets to party in Redis for one whole day!

Parameters:

  • ttl (Integer, nil) (defaults to: nil)

    The Time To Live in seconds. Nil? No worries! We’ll dig up the default from our secret stash.

Returns:

  • (Boolean)

    Did Redis pin that expiry note successfully? True for “Yep!”, false for “Oops, butter fingers!”

Raises:

  • (Familia::Problem)

    If you try to feed it non-numbers or time-travel (negative numbers). It’s strict, but fair!



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/familia/features/expiration.rb', line 60

def update_expiration(ttl = nil)
  ttl ||= self.ttl
  # It's important to raise exceptions here and not just log warnings. We
  # don't want to silently fail at setting expirations and cause data
  # retention issues (e.g. not removed in a timely fashion).
  #
  # For the same reason, we don't want to default to 0 bc there's not a
  # good reason for the ttl to not be set in the first place. If the
  # class doesn't have a ttl, the default comes from Familia.ttl (which
  # is 0).
  raise Familia::Problem, "TTL must be a number (#{ttl.class})" unless ttl.is_a?(Numeric)
  raise Familia::Problem, "TTL must be positive (#{ttl})" unless ttl.is_a?(Numeric)

  if ttl.zero?
    return Familia.ld "[update_expiration] No expiration for #{self.class} (#{rediskey})"
  end

  Familia.info "[update_expiration] Expires #{rediskey} in #{ttl} seconds"

  # Redis' EXPIRE command returns 1 if the timeout was set, 0 if key does
  # not exist or the timeout could not be set. Via redis-rb here, it's
  # a bool.
  expire(ttl)
end