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
41
42
43
44
45
46
47
48
49
50
51
52
# 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            = {}
  @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[:safe] || concurrent?
  @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

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


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

def allow_blank?
  @allow_blank
end

#clearObject



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

def clear
  @cache.clear
end

#create(*args, &block) ⇒ Object



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

def create(*args, &block)
  created = @klass.create(*args, &block)

  cache_write(created) if cache?

  created
end

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



78
79
80
81
82
83
84
# File 'lib/lookup_by/cache.rb', line 78

def create!(*args, &block)
  created = @klass.create!(*args, &block)

  cache_write(created) if cache?

  created
end

#disable!Object



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

def disable!
  return if disabled?

  @enabled = false
  clear
end

#disabled?Boolean

Returns:

  • (Boolean)


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

def disabled?
  !@enabled
end

#enable!Object



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

def enable!
  return if enabled?

  @enabled = true
  reload
end

#enabled?Boolean

Returns:

  • (Boolean)


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

def enabled?
  @enabled
end

#fetch(value) ⇒ Object



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

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)


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

def has_cache?
  @type && @enabled
end

#read_through?Boolean

Returns:

  • (Boolean)


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

def read_through?
  @read
end

#reloadObject



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

def reload
  return unless @type == :all

  clear

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

#seed(*values) ⇒ Object



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

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)


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

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

  disable!

  yield

  enable!
end