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.
-
#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
- #disable! ⇒ Object
- #disabled? ⇒ Boolean
- #enable! ⇒ Object
- #enabled? ⇒ Boolean
- #fetch(value) ⇒ Object
- #has_cache? ⇒ Boolean
-
#initialize(klass, options = {}) ⇒ Cache
constructor
A new instance of Cache.
- #read_through? ⇒ Boolean
- #reload ⇒ Object
Constructor Details
#initialize(klass, options = {}) ⇒ Cache
Returns a new instance of Cache.
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, = {}) @klass = klass @primary_key = klass.primary_key @primary_key_type = klass.columns_hash[@primary_key].type @field = [:field].to_sym @cache = {} @order = [:order] || @field @read = [:find_or_create] || [:find] @write = [:find_or_create] @allow_blank = [:allow_blank] || false @normalize = [: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 [: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 = [: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
#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 |
#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.
4 5 6 |
# File 'lib/lookup_by/cache.rb', line 4 def testing @testing end |
Instance Method Details
#allow_blank? ⇒ Boolean
88 89 90 |
# File 'lib/lookup_by/cache.rb', line 88 def allow_blank? @allow_blank end |
#clear ⇒ Object
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
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
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
80 81 82 |
# File 'lib/lookup_by/cache.rb', line 80 def has_cache? @type && @enabled end |
#read_through? ⇒ Boolean
84 85 86 |
# File 'lib/lookup_by/cache.rb', line 84 def read_through? @read end |
#reload ⇒ Object
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 |