Class: Bizside::Cache::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/bizside/cache/entry.rb

Constant Summary collapse

DEFAULT_COMPRESS_LIMIT =
16.kilobytes

Instance Method Summary collapse

Constructor Details

#initialize(value, options = {}) ⇒ Entry

Returns a new instance of Entry.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/bizside/cache/entry.rb', line 6

def initialize(value, options = {})
  if should_compress?(value, options)
    @value = compress(value)
    @compressed = true
  else
    @value = value
  end

  @created_at = Time.now.to_f
  @expires_in = options[:expires_in]
  @expires_in = @expires_in.to_f if @expires_in
end

Instance Method Details

#dup_value!Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bizside/cache/entry.rb', line 56

def dup_value!
  convert_version_4beta1_entry! if defined?(@v)

  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

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/bizside/cache/entry.rb', line 24

def expired?
  convert_version_4beta1_entry! if defined?(@v)
  @expires_in && @created_at + @expires_in <= Time.now.to_f
end

#expires_atObject



29
30
31
# File 'lib/bizside/cache/entry.rb', line 29

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

#expires_at=(value) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/bizside/cache/entry.rb', line 33

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

#sizeObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bizside/cache/entry.rb', line 41

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

#valueObject



19
20
21
22
# File 'lib/bizside/cache/entry.rb', line 19

def value
  convert_version_4beta1_entry! if defined?(@v)
  compressed? ? uncompress(@value) : @value
end