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.

Constant Summary collapse

DEFAULT_COMPRESS_LIMIT =
1.kilobyte

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, compress: true, compress_threshold: DEFAULT_COMPRESS_LIMIT, version: nil, expires_in: nil) ⇒ Entry

Creates a new cache entry for the specified value. Options supported are :compress, :compress_threshold, :version and :expires_in.


763
764
765
766
767
768
769
770
# File 'activesupport/lib/active_support/cache.rb', line 763

def initialize(value, compress: true, compress_threshold: DEFAULT_COMPRESS_LIMIT, version: nil, expires_in: nil, **)
  @value      = value
  @version    = version
  @created_at = Time.now.to_f
  @expires_in = expires_in && expires_in.to_f

  compress!(compress_threshold) if compress
end

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version


757
758
759
# File 'activesupport/lib/active_support/cache.rb', line 757

def version
  @version
end

Instance Method Details

#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.


813
814
815
816
817
818
819
820
821
# File 'activesupport/lib/active_support/cache.rb', line 813

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)

782
783
784
# File 'activesupport/lib/active_support/cache.rb', line 782

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

#expires_atObject


786
787
788
# File 'activesupport/lib/active_support/cache.rb', line 786

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

#expires_at=(value) ⇒ Object


790
791
792
793
794
795
796
# File 'activesupport/lib/active_support/cache.rb', line 790

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

#mismatched?(version) ⇒ Boolean

Returns:

  • (Boolean)

776
777
778
# File 'activesupport/lib/active_support/cache.rb', line 776

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

#sizeObject

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


800
801
802
803
804
805
806
807
808
809
# File 'activesupport/lib/active_support/cache.rb', line 800

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

#valueObject


772
773
774
# File 'activesupport/lib/active_support/cache.rb', line 772

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