Entry that is put into caches. It supports expiration time on entries and can compress values to save space in the cache.
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.
# File lib/active_support/cache.rb, line 540
540: def create (raw_value, created_at, options = {})
541: entry = new(nil)
542: entry.instance_variable_set(:@value, raw_value)
543: entry.instance_variable_set(:@created_at, created_at.to_f)
544: entry.instance_variable_set(:@compressed, !!options[:compressed])
545: entry.instance_variable_set(:@expires_in, options[:expires_in])
546: entry
547: end
Create a new cache entry for the specified value. Options supported are :compress, :compress_threshold, and :expires_in.
# File lib/active_support/cache.rb, line 552
552: def initialize(value, options = {})
553: @compressed = false
554: @expires_in = options[:expires_in]
555: @expires_in = @expires_in.to_f if @expires_in
556: @created_at = Time.now.to_f
557: if value
558: if should_compress?(value, options)
559: @value = Zlib::Deflate.deflate(Marshal.dump(value))
560: @compressed = true
561: else
562: @value = value
563: end
564: else
565: @value = nil
566: end
567: end
# File lib/active_support/cache.rb, line 585
585: def compressed?
586: @compressed
587: end
Check if the entry is expired. The expires_in parameter can override the value set when the entry was created.
# File lib/active_support/cache.rb, line 591
591: def expired?
592: if @expires_in && @created_at + @expires_in <= Time.now.to_f
593: true
594: else
595: false
596: end
597: end
Seconds since the epoch when the entry will expire.
# File lib/active_support/cache.rb, line 609
609: def expires_at
610: @expires_in ? @created_at + @expires_in : nil
611: end
Set a new time when the entry will expire.
# File lib/active_support/cache.rb, line 600
600: def expires_at=(time)
601: if time
602: @expires_in = time.to_f - @created_at
603: else
604: @expires_in = nil
605: end
606: end
Get the raw value. This value may be serialized and compressed.
# File lib/active_support/cache.rb, line 570
570: def raw_value
571: @value
572: end
Returns the size of the cached value. This could be less than value.size if the data is compressed.
# File lib/active_support/cache.rb, line 615
615: def size
616: if @value.nil?
617: 0
618: elsif @value.respond_to?(:bytesize)
619: @value.bytesize
620: else
621: Marshal.dump(@value).bytesize
622: end
623: end
Get the value stored in the cache.
# File lib/active_support/cache.rb, line 575
575: def value
576: if @value
577: val = compressed? ? Marshal.load(Zlib::Inflate.inflate(@value)) : @value
578: unless val.frozen?
579: val.freeze rescue nil
580: end
581: val
582: end
583: end
# File lib/active_support/cache.rb, line 626
626: def should_compress?(value, options)
627: if options[:compress] && value
628: unless value.is_a?(Numeric)
629: compress_threshold = options[:compress_threshold] || DEFAULT_COMPRESS_LIMIT
630: serialized_value = value.is_a?(String) ? value : Marshal.dump(value)
631: return true if serialized_value.size >= compress_threshold
632: end
633: end
634: false
635: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.