Class: ActiveSupport::Cache::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/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.



927
928
929
930
931
932
933
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/cache.rb', line 927

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.



923
924
925
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/cache.rb', line 923

def version
  @version
end

Class Method Details

.unpack(members) ⇒ Object



918
919
920
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/cache.rb', line 918

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.



963
964
965
966
967
968
969
970
971
972
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/cache.rb', line 963

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

#compressed(compress_threshold) ⇒ Object



978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/cache.rb', line 978

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)


974
975
976
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/cache.rb', line 974

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.



1008
1009
1010
1011
1012
1013
1014
1015
1016
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/cache.rb', line 1008

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)


945
946
947
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/cache.rb', line 945

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

#expires_atObject



949
950
951
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/cache.rb', line 949

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

#expires_at=(value) ⇒ Object



953
954
955
956
957
958
959
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/cache.rb', line 953

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

#local?Boolean

Returns:

  • (Boolean)


1002
1003
1004
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/cache.rb', line 1002

def local?
  false
end

#mismatched?(version) ⇒ Boolean

Returns:

  • (Boolean)


939
940
941
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/cache.rb', line 939

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

#packObject



1018
1019
1020
1021
1022
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/cache.rb', line 1018

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

#valueObject



935
936
937
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/cache.rb', line 935

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