Class: Sequel::Unicache::Write

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel/unicache/write.rb

Class Method Summary collapse

Class Method Details

.check_completeness?(model, configs = unicache_configurations(model)) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
# File 'lib/sequel/unicache/write.rb', line 44

def check_completeness? model, configs = unicache_configurations(model)
  all_unicache_keys = configs.keys.flatten.uniq
  model_keys = model.keys
  all_unicache_keys.all? {|key| model_keys.include? key }
end

.expire(model, force: false) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sequel/unicache/write.rb', line 30

def expire model, force: false
  configs = unicache_configurations model
  reload model unless check_completeness? model, configs
  restore_previous model do # restore to previous values temporarily
    # Unicache must be enabled then do expiration
    configs.each_value { |config| expire_for model, config if force || enabled?(model, config) }
  end
rescue => error
  Unicache::Logger.fatal model, "[Unicache] Exception happen when expire cache for a model. Reason: #{error.message}. Model: #{model.inspect}"
  error.backtrace.each do |trace|
    Unicache::Logger.fatal model, "[Unicache] #{trace}"
  end
end

.write(model) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sequel/unicache/write.rb', line 7

def write model
  # If model is not completed, don't cache it
  if (model.columns - model.keys).empty?
    cache = {}
    unicache_configurations(model).each_value do |config|
      continue unless enabled? model, config # if unicached is disabled, do nothing
      # write cache requires if-condition returns true
      # otherwise will fallback to expire
      if permitted? model, config
        write_for model, config, cache unless suspended? # must be allowed to write cache
      else
        expire_for model, config
      end
    end
  end
rescue => error
  Unicache::Logger.fatal model, "[Unicache] Exception happen when write cache for a model, fallback to expire. Reason: #{error.message}. Model: #{model.inspect}"
  error.backtrace.each do |trace|
    Unicache::Logger.fatal model, "[Unicache] #{trace}"
  end
  expire model
end