Module: Familia::Features::Expiration

Defined in:
lib/familia/features/expiration.rb

Overview

Famnilia::Features::Expiration

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#default_expirationObject



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

def default_expiration
  @default_expiration || self.class.default_expiration
end

#default_expiration=(num) ⇒ Object



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

def default_expiration=(num)
  @default_expiration = num.to_f
end

#update_expiration(default_expiration: nil) ⇒ Boolean

Note:

If Default expiration is set to zero, the expiration will be removed, making the data persist indefinitely.

Sets an expiration time for the Database data associated with this object.

This method allows setting a Time To Live (TTL) for the data in Redis, after which it will be automatically removed.

Examples:

Setting an expiration of one day

object.update_expiration(default_expiration: 86400)

Parameters:

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

    The Time To Live in seconds. If nil, the default TTL will be used.

Returns:

  • (Boolean)

    Returns true if the expiration was set successfully, false otherwise.

Raises:

  • (Familia::Problem)

    Raises an error if the default expiration is not a non-negative integer.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/familia/features/expiration.rb', line 60

def update_expiration(default_expiration: nil)
  default_expiration ||= self.default_expiration

  if self.class.has_relations?
    Familia.ld "[update_expiration] #{self.class} has relations: #{self.class.related_fields.keys}"
    self.class.related_fields.each do |name, definition|
      next if definition.opts[:default_expiration].nil?

      obj = send(name)
      Familia.ld "[update_expiration] Updating expiration for #{name} (#{obj.dbkey}) to #{default_expiration}"
      obj.update_expiration(default_expiration: default_expiration)
    end
  end

  # 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 default_expiration to not be set in the first place. If the
  # class doesn't have a default_expiration, the default comes from
  # Familia.default_expiration (which is 0, aka no-op/skip/do nothing).
  unless default_expiration.is_a?(Numeric)
    raise Familia::Problem, "Default expiration must be a number (#{default_expiration.class} in #{self.class})"
  end

  # If zero, simply skips setting an expiry for this key. If we were to set
  # 0 the database would drop the key immediately.
  return Familia.ld "[update_expiration] No expiration for #{self.class} (#{dbkey})" if default_expiration.zero?

  Familia.ld "[update_expiration] Expires #{dbkey} in #{default_expiration} 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(default_expiration)
end