Class: LookupBy::Cache

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, options = {}) ⇒ Cache

Returns a new instance of Cache.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lookup_by/cache.rb', line 6

def initialize(klass, options = {})
  @klass            = klass
  @primary_key      = klass.primary_key
  @primary_key_type = klass.columns_hash[@primary_key].type
  @field            = options[:field].to_sym
  @cache            = {}
  @order            = options[:order] || @field
  @read             = options[:find_or_create] || options[:find]
  @write            = options[:find_or_create]
  @allow_blank      = options[:allow_blank] || false
  @normalize        = options[:normalize]
  @testing          = false
  @enabled          = true

  @stats            = { db: Hash.new(0), cache: Hash.new(0) }

  raise ArgumentError, %Q(unknown attribute "#{@field}" for <#{klass}>) unless klass.column_names.include?(@field.to_s)

  case options[:cache]
  when true
    @type    = :all
    @read  ||= false
  when ::Integer
    raise ArgumentError, "`#{@klass}.lookup_by :#{@field}` options[:find] must be true when caching N" if @read == false

    @type    = :lru
    @limit   = options[:cache]
    @cache   = Rails.configuration.allow_concurrency ? Caching::SafeLRU.new(@limit) : Caching::LRU.new(@limit)
    @read    = true
    @write ||= false
    @testing = true if Rails.env.test? && @write
  else
    @read    = true
  end
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



3
4
5
# File 'lib/lookup_by/cache.rb', line 3

def cache
  @cache
end

#fieldObject (readonly)

Returns the value of attribute field.



3
4
5
# File 'lib/lookup_by/cache.rb', line 3

def field
  @field
end

#statsObject (readonly)

Returns the value of attribute stats.



3
4
5
# File 'lib/lookup_by/cache.rb', line 3

def stats
  @stats
end

#testingObject

Returns the value of attribute testing.



4
5
6
# File 'lib/lookup_by/cache.rb', line 4

def testing
  @testing
end

Instance Method Details

#allow_blank?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/lookup_by/cache.rb', line 88

def allow_blank?
  @allow_blank
end

#clearObject



54
55
56
# File 'lib/lookup_by/cache.rb', line 54

def clear
  @cache.clear
end

#create!(*args, &block) ⇒ Object



58
59
60
61
62
# File 'lib/lookup_by/cache.rb', line 58

def create!(*args, &block)
  created = @klass.create!(*args, &block)
  @cache[created.id] = created if cache?
  created
end

#disable!Object



105
106
107
108
# File 'lib/lookup_by/cache.rb', line 105

def disable!
  @enabled = false
  clear
end

#disabled?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/lookup_by/cache.rb', line 96

def disabled?
  !@enabled
end

#enable!Object



100
101
102
103
# File 'lib/lookup_by/cache.rb', line 100

def enable!
  @enabled = true
  reload
end

#enabled?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/lookup_by/cache.rb', line 92

def enabled?
  @enabled
end

#fetch(value) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/lookup_by/cache.rb', line 64

def fetch(value)
  increment :cache, :get

  value = normalize(value)  if @normalize && !primary_key?(value)

  found = cache_read(value) if cache?
  found ||= db_read(value)  if @read
  found ||= db_read(value)  if not @enabled

  @cache[found.id] = found  if found && cache?

  found ||= db_write(value) if @write

  found
end

#has_cache?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/lookup_by/cache.rb', line 80

def has_cache?
  @type && @enabled
end

#read_through?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/lookup_by/cache.rb', line 84

def read_through?
  @read
end

#reloadObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/lookup_by/cache.rb', line 42

def reload
  return unless @type == :all

  clear

  ::ActiveRecord::Base.connection.send :log, "", "#{@klass.name} Load Cache All" do
    @klass.order(@order).each do |i|
      @cache[i.id] = i
    end
  end
end