class ActiveSupport::Cache::Entry
:nodoc:
using short instance variable names that are lazily defined.
Since cache entries in most instances will be serialized, the internals of this class are highly optimized
on the cache.
expiration time. The expiration time is used to support the :race_condition_ttl option
This class is used to represent cache entries. Cache entries have a value and an optional
def compress(value)
def compress(value) Zlib::Deflate.deflate(Marshal.dump(value)) end
def compressed?
def compressed? defined?(@compressed) ? @compressed : false end
def dup_value!
Duplicates the value in a class. This is used by cache implementations that don't natively
def dup_value! if @value && !compressed? && !(@value.is_a?(Numeric) || @value == true || @value == false) if @value.is_a?(String) @value = @value.dup else @value = Marshal.load(Marshal.dump(@value)) end end end
def expired?
Checks if the entry is expired. The +expires_in+ parameter can override
def expired? @expires_in && @created_at + @expires_in <= Time.now.to_f end
def expires_at
def expires_at @expires_in ? @created_at + @expires_in : nil end
def expires_at=(value)
def expires_at=(value) if value @expires_in = value.to_f - @created_at else @expires_in = nil end end
def initialize(value, options = {})
Creates a new cache entry for the specified value. Options supported are
def initialize(value, options = {}) if should_compress?(value, options) @value = compress(value) @compressed = true else @value = value end @created_at = Time.now.to_f @expires_in = options[:expires_in] @expires_in = @expires_in.to_f if @expires_in end
def should_compress?(value, options)
def should_compress?(value, options) if value && options[:compress] compress_threshold = options[:compress_threshold] || DEFAULT_COMPRESS_LIMIT serialized_value_size = (value.is_a?(String) ? value : Marshal.dump(value)).bytesize return true if serialized_value_size >= compress_threshold end false end
def size
Returns the size of the cached value. This could be less than
def size if defined?(@s) @s else case value when NilClass 0 when String @value.bytesize else @s = Marshal.dump(@value).bytesize end end end
def uncompress(value)
def uncompress(value) Marshal.load(Zlib::Inflate.inflate(value)) end
def value
def value compressed? ? uncompress(@value) : @value end