Class: LookupBy::Cache
- Inherits:
-
Object
- Object
- LookupBy::Cache
- Defined in:
- lib/lookup_by/cache.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#field ⇒ Object
readonly
Returns the value of attribute field.
-
#reverse ⇒ Object
readonly
Returns the value of attribute reverse.
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
-
#testing ⇒ Object
Returns the value of attribute testing.
Instance Method Summary collapse
- #allow_blank? ⇒ Boolean
- #clear ⇒ Object
- #create(*args, &block) ⇒ Object
- #create!(*args, &block) ⇒ Object
- #disable! ⇒ Object
- #disabled? ⇒ Boolean
- #enable! ⇒ Object
- #enabled? ⇒ Boolean
- #fetch(value) ⇒ Object
- #has_cache? ⇒ Boolean
-
#initialize(klass, options = {}) ⇒ Cache
constructor
A new instance of Cache.
- #load ⇒ Object
- #read_through? ⇒ Boolean
- #reload ⇒ Object
- #seed(*values) ⇒ Object
- #while_disabled ⇒ Object
Constructor Details
#initialize(klass, options = {}) ⇒ Cache
Returns a new instance of Cache.
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, = {}) @klass = klass @primary_key = klass.primary_key @primary_key_type = klass.columns_hash[@primary_key].type @field = [:field].to_sym @cache = {} @reverse = {} @order = [:order] || @field @read = [:find_or_create] || [:find] @write = [:find_or_create] @allow_blank = [:allow_blank] || false @normalize = [:normalize] @raise_on_miss = [:raise] || false @testing = false @enabled = true @safe = .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 [: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 = [: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
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
3 4 5 |
# File 'lib/lookup_by/cache.rb', line 3 def cache @cache end |
#field ⇒ Object (readonly)
Returns the value of attribute field.
3 4 5 |
# File 'lib/lookup_by/cache.rb', line 3 def field @field end |
#reverse ⇒ Object (readonly)
Returns the value of attribute reverse.
3 4 5 |
# File 'lib/lookup_by/cache.rb', line 3 def reverse @reverse end |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
3 4 5 |
# File 'lib/lookup_by/cache.rb', line 3 def stats @stats end |
#testing ⇒ Object
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
120 121 122 |
# File 'lib/lookup_by/cache.rb', line 120 def allow_blank? @allow_blank end |
#clear ⇒ Object
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
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
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
112 113 114 |
# File 'lib/lookup_by/cache.rb', line 112 def has_cache? @type && @enabled end |
#load ⇒ Object
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
116 117 118 |
# File 'lib/lookup_by/cache.rb', line 116 def read_through? @read end |
#reload ⇒ Object
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_disabled ⇒ Object
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 |