Class: ActiveSupport::Cache::Entry

Inherits:
Object
  • Object
show all
Defined in:
activesupport/lib/active_support/cache/entry.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.

Direct Known Subclasses

Coder::LazyEntry

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.



25
26
27
28
29
30
31
# 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

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version.



21
22
23
# File 'activesupport/lib/active_support/cache/entry.rb', line 21

def version
  @version
end

Class Method Details

.unpack(members) ⇒ Object



16
17
18
# 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 Method Details

#bytesizeObject

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



61
62
63
64
65
66
67
68
69
70
# File 'activesupport/lib/active_support/cache/entry.rb', line 61

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

#compressed(compress_threshold) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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

#compressed?Boolean

:nodoc:

Returns:

  • (Boolean)


72
73
74
# File 'activesupport/lib/active_support/cache/entry.rb', line 72

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.



106
107
108
109
110
111
112
113
114
# 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

#expired?Boolean

Checks if the entry is expired. The expires_in parameter can override the value set when the entry was created.

Returns:

  • (Boolean)


43
44
45
# File 'activesupport/lib/active_support/cache/entry.rb', line 43

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

#expires_atObject



47
48
49
# File 'activesupport/lib/active_support/cache/entry.rb', line 47

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

#expires_at=(value) ⇒ Object



51
52
53
54
55
56
57
# File 'activesupport/lib/active_support/cache/entry.rb', line 51

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

#local?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'activesupport/lib/active_support/cache/entry.rb', line 100

def local?
  false
end

#mismatched?(version) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'activesupport/lib/active_support/cache/entry.rb', line 37

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

#packObject



116
117
118
119
120
# 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

#valueObject



33
34
35
# File 'activesupport/lib/active_support/cache/entry.rb', line 33

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