Class: ActiveSupport::Cache::Entry
- Defined in:
- lib/active_support/cache.rb
Overview
This class is used to represent cache entries. 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.
Instance Attribute Summary collapse
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
Instance Method Summary collapse
-
#bytesize ⇒ Object
Returns the size of the cached value.
- #compressed(compress_threshold) ⇒ Object
-
#compressed? ⇒ Boolean
:nodoc:.
-
#dup_value! ⇒ Object
Duplicates the value in a class.
-
#expired? ⇒ Boolean
Checks if the entry is expired.
- #expires_at ⇒ Object
- #expires_at=(value) ⇒ Object
-
#initialize(value, compressed: false, version: nil, expires_in: nil, expires_at: nil) ⇒ Entry
constructor
Creates a new cache entry for the specified value.
- #local? ⇒ Boolean
- #mismatched?(version) ⇒ Boolean
- #pack ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(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
.
919 920 921 922 923 924 925 |
# File 'lib/active_support/cache.rb', line 919 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 |
Instance Attribute Details
#version ⇒ Object (readonly)
Returns the value of attribute version.
915 916 917 |
# File 'lib/active_support/cache.rb', line 915 def version @version end |
Class Method Details
.unpack(members) ⇒ Object
910 911 912 |
# File 'lib/active_support/cache.rb', line 910 def unpack(members) new(members[0], expires_at: members[1], version: members[2]) end |
Instance Method Details
#bytesize ⇒ Object
Returns the size of the cached value. This could be less than value.bytesize
if the data is compressed.
955 956 957 958 959 960 961 962 963 964 |
# File 'lib/active_support/cache.rb', line 955 def bytesize case value when NilClass 0 when String @value.bytesize else @s ||= Marshal.dump(@value).bytesize end end |
#compressed(compress_threshold) ⇒ Object
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 |
# File 'lib/active_support/cache.rb', line 970 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 |
#compressed? ⇒ Boolean
:nodoc:
966 967 968 |
# File 'lib/active_support/cache.rb', line 966 def compressed? # :nodoc: defined?(@compressed) end |
#dup_value! ⇒ Object
Duplicates the value in a class. This is used by cache implementations that don’t natively serialize entries to protect against accidental cache modifications.
1000 1001 1002 1003 1004 1005 1006 1007 1008 |
# File 'lib/active_support/cache.rb', line 1000 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 |
#expired? ⇒ Boolean
Checks if the entry is expired. The expires_in
parameter can override the value set when the entry was created.
937 938 939 |
# File 'lib/active_support/cache.rb', line 937 def expired? @expires_in && @created_at + @expires_in <= Time.now.to_f end |
#expires_at ⇒ Object
941 942 943 |
# File 'lib/active_support/cache.rb', line 941 def expires_at @expires_in ? @created_at + @expires_in : nil end |
#expires_at=(value) ⇒ Object
945 946 947 948 949 950 951 |
# File 'lib/active_support/cache.rb', line 945 def expires_at=(value) if value @expires_in = value.to_f - @created_at else @expires_in = nil end end |
#local? ⇒ Boolean
994 995 996 |
# File 'lib/active_support/cache.rb', line 994 def local? false end |
#mismatched?(version) ⇒ Boolean
931 932 933 |
# File 'lib/active_support/cache.rb', line 931 def mismatched?(version) @version && version && @version != version end |
#pack ⇒ Object
1010 1011 1012 1013 1014 |
# File 'lib/active_support/cache.rb', line 1010 def pack members = [value, expires_at, version] members.pop while !members.empty? && members.last.nil? members end |
#value ⇒ Object
927 928 929 |
# File 'lib/active_support/cache.rb', line 927 def value compressed? ? uncompress(@value) : @value end |