Class: ActiveSupport::Cache::Entry
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Inherits: | Object |
Defined in: | activesupport/lib/active_support/cache/entry.rb |
Overview
This class is used to represent cache entries. ::ActiveSupport::Cache
entries have a value, an optional expiration time, and an optional version. The expiration time is used to support the :race_condition_ttl
option on the cache. The version is used to support the :version
option on the cache for rejecting mismatches.
Since cache entries in most instances will be serialized, the internals of this class are highly optimized using short instance variable names that are lazily defined.
Class Method Summary
-
.new(value, compressed: false, version: nil, expires_in: nil, expires_at: nil) ⇒ Entry
constructor
Creates a new cache entry for the specified value.
- .unpack(members)
Instance Attribute Summary
- #compressed? ⇒ Boolean readonly
-
#expired? ⇒ Boolean
readonly
Checks if the entry is expired.
- #expires_at rw
- #expires_at=(value) rw
- #local? ⇒ Boolean readonly
- #version readonly
Instance Method Summary
-
#bytesize
Returns the size of the cached value.
- #compressed(compress_threshold) readonly
-
#dup_value!
Duplicates the value in a class.
- #mismatched?(version) ⇒ Boolean
- #pack
- #value
- #marshal_load(payload) private
- #uncompress(value) private
Constructor Details
.new(value, compressed: false, version: nil, expires_in: nil, expires_at: nil) ⇒ Entry
Creates a new cache entry for the specified value. Options supported are :compressed
, :version
, :expires_at
and :expires_in
.
# File 'activesupport/lib/active_support/cache/entry.rb', line 25
def initialize(value, compressed: false, version: nil, expires_in: nil, expires_at: nil, **) @value = value @version = version @created_at = 0.0 @expires_in = expires_at&.to_f || expires_in && (expires_in.to_f + Time.now.to_f) @compressed = true if compressed end
Class Method Details
.unpack(members)
[ GitHub ]# File 'activesupport/lib/active_support/cache/entry.rb', line 16
def unpack(members) new(members[0], expires_at: members[1], version: members[2]) end
Instance Attribute Details
#compressed? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'activesupport/lib/active_support/cache/entry.rb', line 72
def compressed? # :nodoc: defined?(@compressed) end
#expired? ⇒ Boolean
(readonly)
Checks if the entry is expired. The expires_in
parameter can override the value set when the entry was created.
# File 'activesupport/lib/active_support/cache/entry.rb', line 43
def expired? @expires_in && @created_at + @expires_in <= Time.now.to_f end
#expires_at (rw)
[ GitHub ]# File 'activesupport/lib/active_support/cache/entry.rb', line 47
def expires_at @expires_in ? @created_at + @expires_in : nil end
#expires_at=(value) (rw)
[ GitHub ]
#local? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'activesupport/lib/active_support/cache/entry.rb', line 100
def local? false end
#version (readonly)
[ GitHub ]# File 'activesupport/lib/active_support/cache/entry.rb', line 21
attr_reader :version
Instance Method Details
#bytesize
Returns the size of the cached value. This could be less than value.bytesize
if the data is compressed.
#compressed(compress_threshold) (readonly)
[ GitHub ]# File 'activesupport/lib/active_support/cache/entry.rb', line 76
def compressed(compress_threshold) return self if compressed? case @value when nil, true, false, Numeric uncompressed_size = 0 when String uncompressed_size = @value.bytesize else serialized = Marshal.dump(@value) uncompressed_size = serialized.bytesize end if uncompressed_size >= compress_threshold serialized ||= Marshal.dump(@value) compressed = Zlib::Deflate.deflate(serialized) if compressed.bytesize < uncompressed_size return Entry.new(compressed, compressed: true, expires_at: expires_at, version: version) end end self end
#dup_value!
Duplicates the value in a class. This is used by cache implementations that don’t natively serialize entries to protect against accidental cache modifications.
# File 'activesupport/lib/active_support/cache/entry.rb', line 106
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
#marshal_load(payload) (private)
[ GitHub ]# File 'activesupport/lib/active_support/cache/entry.rb', line 127
def marshal_load(payload) Marshal.load(payload) rescue ArgumentError => error raise Cache::DeserializationError, error. end
#mismatched?(version) ⇒ Boolean
#pack
[ GitHub ]# File 'activesupport/lib/active_support/cache/entry.rb', line 116
def pack members = [value, expires_at, version] members.pop while !members.empty? && members.last.nil? members end
#uncompress(value) (private)
[ GitHub ]# File 'activesupport/lib/active_support/cache/entry.rb', line 123
def uncompress(value) marshal_load(Zlib::Inflate.inflate(value)) end
#value
[ GitHub ]# File 'activesupport/lib/active_support/cache/entry.rb', line 33
def value compressed? ? uncompress(@value) : @value end