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)


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
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lookup_by/cache.rb', line 7

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            = {}
  @reverse          = {}
  @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]
  @raise_on_miss    = options[:raise] || false
  @testing          = false
  @enabled          = true
  @safe             = options.fetch(:safe, true)
  @mutex            = Mutex.new if @safe

  @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)

  # Order matters here, some instance variables depend on prior assignments.
  case options[:cache]
  when true
    @type    = :all
    @read  ||= false

    raise ArgumentError, "`#{@klass}.lookup_by :#{@field}` Should be `cache: true` or `cache: N, find_or_create: true`" if @write
  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   = @safe ? Caching::SafeLRU.new(@limit) : Caching::LRU.new(@limit)
    @reverse = @safe ? Caching::SafeLRU.new(@limit) : Caching::LRU.new(@limit)
    @read    = true
    @write ||= false
    @testing = true if Rails.env.test? && @write
  else
    @read    = true
  end

  if @write && @raise_on_miss
    raise ArgumentError, "`#{@klass}.lookup_by :#{@field}` can not use `raise: true` and `find_or_create: true` together."
  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

#reverseObject (readonly)

Returns the value of attribute reverse.



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

def reverse
  @reverse
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.



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

def testing
  @testing
end

Instance Method Details

#allow_blank?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/lookup_by/cache.rb', line 120

def allow_blank?
  @allow_blank
end

#clearObject



70
71
72
73
# File 'lib/lookup_by/cache.rb', line 70

def clear
  @cache.clear
  @reverse.clear
end

#create(*args, &block) ⇒ Object



75
76
77
78
79
# File 'lib/lookup_by/cache.rb', line 75

def create(*args, &block)
  @klass.create(*args, &block).tap do |created|
    cache_write(created) if cache?
  end
end

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



81
82
83
84
85
# File 'lib/lookup_by/cache.rb', line 81

def create!(*args, &block)
  @klass.create!(*args, &block).tap do |created|
    cache_write(created) if cache?
  end
end

#disable!Object



139
140
141
142
143
144
# File 'lib/lookup_by/cache.rb', line 139

def disable!
  return if disabled?

  @enabled = false
  clear
end

#disabled?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/lookup_by/cache.rb', line 128

def disabled?
  !@enabled
end

#enable!Object



132
133
134
135
136
137
# File 'lib/lookup_by/cache.rb', line 132

def enable!
  return if enabled?

  @enabled = true
  reload
end

#enabled?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/lookup_by/cache.rb', line 124

def enabled?
  @enabled
end

#fetch(value) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/lookup_by/cache.rb', line 93

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 || !@enabled

  cache_write(found) if cache?

  found ||= db_write(value) if @write

  if @raise_on_miss && found.nil?
    raise LookupBy::RecordNotFound, "No #{@klass.name} lookup record found for value: #{value.inspect}"
  end

  found
end

#has_cache?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/lookup_by/cache.rb', line 112

def has_cache?
  @type && @enabled
end

#loadObject



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

def load
  return unless @type == :all

  ::ActiveRecord::Base.connection.send :log, "", "#{@klass.name} Load Cache All" do
    @klass.order(@order).readonly.each do |object|
      cache_write(object)
    end
  end
end

#read_through?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/lookup_by/cache.rb', line 116

def read_through?
  @read
end

#reloadObject



65
66
67
68
# File 'lib/lookup_by/cache.rb', line 65

def reload
  clear
  load
end

#seed(*values) ⇒ Object



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

def seed(*values)
  @klass.transaction(requires_new: true) do
    values.map { |value| @klass.where(@field => value).first_or_create! }
  end
end

#while_disabledObject

Raises:

  • (ArgumentError)


146
147
148
149
150
151
152
153
154
# File 'lib/lookup_by/cache.rb', line 146

def while_disabled
  raise ArgumentError, "no block given" unless block_given?

  disable!

  yield

  enable!
end