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
.
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 |
# File 'lib/active_support/cache.rb', line 552 def initialize(value, = {}) @compressed = false @expires_in = [:expires_in] @expires_in = @expires_in.to_f if @expires_in @created_at = Time.now.to_f if value 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.
532 533 534 |
# File 'lib/active_support/cache.rb', line 532 def created_at @created_at end |
#expires_in ⇒ Object (readonly)
Returns the value of attribute expires_in.
532 533 534 |
# File 'lib/active_support/cache.rb', line 532 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.
540 541 542 543 544 545 546 547 |
# File 'lib/active_support/cache.rb', line 540 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
585 586 587 |
# File 'lib/active_support/cache.rb', line 585 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.
591 592 593 594 595 596 597 |
# File 'lib/active_support/cache.rb', line 591 def expired? if @expires_in && @created_at + @expires_in <= Time.now.to_f true else false end end |
#expires_at ⇒ Object
Seconds since the epoch when the entry will expire.
609 610 611 |
# File 'lib/active_support/cache.rb', line 609 def expires_at @expires_in ? @created_at + @expires_in : nil end |
#expires_at=(time) ⇒ Object
Set a new time when the entry will expire.
600 601 602 603 604 605 606 |
# File 'lib/active_support/cache.rb', line 600 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.
570 571 572 |
# File 'lib/active_support/cache.rb', line 570 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.
615 616 617 618 619 620 621 622 623 |
# File 'lib/active_support/cache.rb', line 615 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.
575 576 577 578 579 580 581 582 583 |
# File 'lib/active_support/cache.rb', line 575 def value if @value val = compressed? ? Marshal.load(Zlib::Inflate.inflate(@value)) : @value unless val.frozen? val.freeze rescue nil end val end end |