Class: ActiveSupport::Cache::Entry
- Defined in:
- lib/active_support/cache.rb
Overview
Entry that is put into caches. It supports expiration time on entries and can compress values to save space in the cache.
Constant Summary collapse
- DEFAULT_COMPRESS_LIMIT =
16.kilobytes
Instance Attribute Summary collapse
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#expires_in ⇒ Object
readonly
Returns the value of attribute expires_in.
Class Method Summary collapse
-
.create(raw_value, created_at, options = {}) ⇒ Object
Create an entry with internal attributes set.
Instance Method Summary collapse
- #compressed? ⇒ Boolean
-
#expired? ⇒ Boolean
Check if the entry is expired.
-
#expires_at ⇒ Object
Seconds since the epoch when the entry will expire.
-
#expires_at=(time) ⇒ Object
Set a new time when the entry will expire.
-
#initialize(value, options = {}) ⇒ Entry
constructor
Create a new cache entry for the specified value.
-
#raw_value ⇒ Object
Get the raw value.
-
#size ⇒ Object
Returns the size of the cached value.
-
#value ⇒ Object
Get the value stored in the cache.
Constructor Details
#initialize(value, options = {}) ⇒ Entry
Create a new cache entry for the specified value. Options supported are :compress
, :compress_threshold
, and :expires_in
.
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 |
# File 'lib/active_support/cache.rb', line 553 def initialize(value, = {}) @compressed = false @expires_in = [:expires_in] @expires_in = @expires_in.to_f if @expires_in @created_at = Time.now.to_f unless value.nil? if should_compress?(value, ) @value = Zlib::Deflate.deflate(Marshal.dump(value)) @compressed = true else @value = value end else @value = nil end end |
Instance Attribute Details
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
533 534 535 |
# File 'lib/active_support/cache.rb', line 533 def created_at @created_at end |
#expires_in ⇒ Object (readonly)
Returns the value of attribute expires_in.
533 534 535 |
# File 'lib/active_support/cache.rb', line 533 def expires_in @expires_in end |
Class Method Details
.create(raw_value, created_at, options = {}) ⇒ Object
Create an entry with internal attributes set. This method is intended to be used by implementations that store cache entries in a native format instead of as serialized Ruby objects.
541 542 543 544 545 546 547 548 |
# File 'lib/active_support/cache.rb', line 541 def create (raw_value, created_at, = {}) entry = new(nil) entry.instance_variable_set(:@value, raw_value) entry.instance_variable_set(:@created_at, created_at.to_f) entry.instance_variable_set(:@compressed, !![:compressed]) entry.instance_variable_set(:@expires_in, [:expires_in]) entry end |
Instance Method Details
#compressed? ⇒ Boolean
586 587 588 |
# File 'lib/active_support/cache.rb', line 586 def compressed? @compressed end |
#expired? ⇒ Boolean
Check if the entry is expired. The expires_in
parameter can override the value set when the entry was created.
592 593 594 |
# File 'lib/active_support/cache.rb', line 592 def expired? @expires_in && @created_at + @expires_in <= Time.now.to_f end |
#expires_at ⇒ Object
Seconds since the epoch when the entry will expire.
606 607 608 |
# File 'lib/active_support/cache.rb', line 606 def expires_at @expires_in ? @created_at + @expires_in : nil end |
#expires_at=(time) ⇒ Object
Set a new time when the entry will expire.
597 598 599 600 601 602 603 |
# File 'lib/active_support/cache.rb', line 597 def expires_at=(time) if time @expires_in = time.to_f - @created_at else @expires_in = nil end end |
#raw_value ⇒ Object
Get the raw value. This value may be serialized and compressed.
571 572 573 |
# File 'lib/active_support/cache.rb', line 571 def raw_value @value end |
#size ⇒ Object
Returns the size of the cached value. This could be less than value.size if the data is compressed.
612 613 614 615 616 617 618 619 620 |
# File 'lib/active_support/cache.rb', line 612 def size if @value.nil? 0 elsif @value.respond_to?(:bytesize) @value.bytesize else Marshal.dump(@value).bytesize end end |
#value ⇒ Object
Get the value stored in the cache.
576 577 578 579 580 581 582 583 584 |
# File 'lib/active_support/cache.rb', line 576 def value unless @value.nil? val = compressed? ? Marshal.load(Zlib::Inflate.inflate(@value)) : @value unless val.frozen? val.freeze rescue nil end val end end |