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
- #ttl ⇒ Object
- #ttl=(v) ⇒ Object
-
#update_expiration(ttl = nil) ⇒ Boolean
Yo, check it out! We’re gonna give our Redis data an expiration date!.
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
#ttl ⇒ Object
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?
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 |
# 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). unless ttl.is_a?(Numeric) raise Familia::Problem, "TTL must be a number (#{ttl.class} in #{self.class})" end if ttl.zero? return Familia.ld "[update_expiration] No expiration for #{self.class} (#{rediskey})" end Familia.ld "[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 |