Module: Familia::Features::Expiration

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#default_expirationObject



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

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

#default_expiration=(v) ⇒ Object



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

def default_expiration=(v)
  @default_expiration = v.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.



59
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
# File 'lib/familia/features/expiration.rb', line 59

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).
  unless default_expiration.is_a?(Numeric)
    raise Familia::Problem, "Default expiration must be a number (#{default_expiration.class} in #{self.class})"
  end

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

  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