class Dalli::Protocol::TtlSanitizer
Utility class for sanitizing TTL arguments based on Memcached rules. TTLs are either expirations times in seconds (with a maximum value of 30 days) or expiration timestamps. This class sanitizes TTLs to ensure they meet those restrictions.
Constants
- MAX_ACCEPTABLE_EXPIRATION_INTERVAL
github.com/memcached/memcached/blob/master/doc/protocol.txt#L79 > An expiration time, in seconds. Can be up to 30 days. After 30 days, is
treated as a unix timestamp of an exact date.
Public Class Methods
as_timestamp(ttl_as_i)
click to toggle source
# File lib/dalli/protocol/ttl_sanitizer.rb, line 28 def self.as_timestamp(ttl_as_i) now = current_timestamp return ttl_as_i if ttl_as_i > now # Already a timestamp Dalli.logger.debug "Expiration interval (#{ttl_as_i}) too long for Memcached " \ 'and too short to be a future timestamp,' \ 'converting to an expiration timestamp' now + ttl_as_i end
current_timestamp()
click to toggle source
Pulled out into a method so it's easy to stub time
# File lib/dalli/protocol/ttl_sanitizer.rb, line 39 def self.current_timestamp Time.now.to_i end
less_than_max_expiration_interval?(ttl_as_i)
click to toggle source
# File lib/dalli/protocol/ttl_sanitizer.rb, line 24 def self.less_than_max_expiration_interval?(ttl_as_i) ttl_as_i <= MAX_ACCEPTABLE_EXPIRATION_INTERVAL end
sanitize(ttl)
click to toggle source
Ensures the TTL passed to Memcached is a valid TTL in the expected format.
# File lib/dalli/protocol/ttl_sanitizer.rb, line 17 def self.sanitize(ttl) ttl_as_i = ttl.to_i return ttl_as_i if less_than_max_expiration_interval?(ttl_as_i) as_timestamp(ttl_as_i) end