Class: ActiveSupport::Cache::Entry

Inherits:
Object
  • Object
show all
Defined in:
activesupport/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

Class Method Summary collapse

Instance Method Summary collapse

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.



1025
1026
1027
1028
1029
1030
1031
# File 'activesupport/lib/active_support/cache.rb', line 1025

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

#versionObject (readonly)

Returns the value of attribute version



1021
1022
1023
# File 'activesupport/lib/active_support/cache.rb', line 1021

def version
  @version
end

Class Method Details

.unpack(members) ⇒ Object



1016
1017
1018
# File 'activesupport/lib/active_support/cache.rb', line 1016

def unpack(members)
  new(members[0], expires_at: members[1], version: members[2])
end

Instance Method Details

#bytesizeObject

Returns the size of the cached value. This could be less than value.bytesize if the data is compressed.



1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
# File 'activesupport/lib/active_support/cache.rb', line 1061

def bytesize
  case value
  when NilClass
    0
  when String
    @value.bytesize
  else
    @s ||= Marshal.dump(@value).bytesize
  end
end

#compressed(compress_threshold) ⇒ Object



1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
# File 'activesupport/lib/active_support/cache.rb', line 1076

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:

Returns:

  • (Boolean)


1072
1073
1074
# File 'activesupport/lib/active_support/cache.rb', line 1072

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.



1106
1107
1108
1109
1110
1111
1112
1113
1114
# File 'activesupport/lib/active_support/cache.rb', line 1106

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.

Returns:

  • (Boolean)


1043
1044
1045
# File 'activesupport/lib/active_support/cache.rb', line 1043

def expired?
  @expires_in && @created_at + @expires_in <= Time.now.to_f
end

#expires_atObject



1047
1048
1049
# File 'activesupport/lib/active_support/cache.rb', line 1047

def expires_at
  @expires_in ? @created_at + @expires_in : nil
end

#expires_at=(value) ⇒ Object



1051
1052
1053
1054
1055
1056
1057
# File 'activesupport/lib/active_support/cache.rb', line 1051

def expires_at=(value)
  if value
    @expires_in = value.to_f - @created_at
  else
    @expires_in = nil
  end
end

#local?Boolean

Returns:

  • (Boolean)


1100
1101
1102
# File 'activesupport/lib/active_support/cache.rb', line 1100

def local?
  false
end

#mismatched?(version) ⇒ Boolean

Returns:

  • (Boolean)


1037
1038
1039
# File 'activesupport/lib/active_support/cache.rb', line 1037

def mismatched?(version)
  @version && version && @version != version
end

#packObject



1116
1117
1118
1119
1120
# File 'activesupport/lib/active_support/cache.rb', line 1116

def pack
  members = [value, expires_at, version]
  members.pop while !members.empty? && members.last.nil?
  members
end

#valueObject



1033
1034
1035
# File 'activesupport/lib/active_support/cache.rb', line 1033

def value
  compressed? ? uncompress(@value) : @value
end