Class: ActiveSupport::Cache::Entry
- 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 =
16.kilobytes
Instance Attribute Summary collapse
-
#version ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#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, options = {}) ⇒ Entry
constructor
Creates a new cache entry for the specified value.
- #mismatched?(version) ⇒ Boolean
-
#size ⇒ Object
Returns the size of the cached value.
- #value ⇒ Object
Constructor Details
#initialize(value, options = {}) ⇒ Entry
Creates a new cache entry for the specified value. Options supported are :compress
, :compress_threshold
, and :expires_in
.
665 666 667 668 669 670 671 672 673 674 675 676 677 |
# File 'activesupport/lib/active_support/cache.rb', line 665 def initialize(value, = {}) if should_compress?(value, ) @value = compress(value) @compressed = true else @value = value end @version = [:version] @created_at = Time.now.to_f @expires_in = [:expires_in] @expires_in = @expires_in.to_f if @expires_in end |
Instance Attribute Details
#version ⇒ Object (readonly)
:nodoc:
659 660 661 |
# File 'activesupport/lib/active_support/cache.rb', line 659 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.
724 725 726 727 728 729 730 731 732 |
# File 'activesupport/lib/active_support/cache.rb', line 724 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.
689 690 691 |
# File 'activesupport/lib/active_support/cache.rb', line 689 def expired? @expires_in && @created_at + @expires_in <= Time.now.to_f end |
#expires_at ⇒ Object
693 694 695 |
# File 'activesupport/lib/active_support/cache.rb', line 693 def expires_at @expires_in ? @created_at + @expires_in : nil end |
#expires_at=(value) ⇒ Object
697 698 699 700 701 702 703 |
# File 'activesupport/lib/active_support/cache.rb', line 697 def expires_at=(value) if value @expires_in = value.to_f - @created_at else @expires_in = nil end end |
#mismatched?(version) ⇒ Boolean
683 684 685 |
# File 'activesupport/lib/active_support/cache.rb', line 683 def mismatched?(version) @version && version && @version != version end |
#size ⇒ Object
Returns the size of the cached value. This could be less than value.size
if the data is compressed.
707 708 709 710 711 712 713 714 715 716 717 718 719 720 |
# File 'activesupport/lib/active_support/cache.rb', line 707 def size if defined?(@s) @s else case value when NilClass 0 when String @value.bytesize else @s = Marshal.dump(@value).bytesize end end end |
#value ⇒ Object
679 680 681 |
# File 'activesupport/lib/active_support/cache.rb', line 679 def value compressed? ? uncompress(@value) : @value end |