Sha256: 2ebcc2bebd1efce4169c362d82c9b2cb3b79e926a4941d00e1f47eec40bf9913

Contents?: true

Size: 691 Bytes

Versions: 6

Compression:

Stored size: 691 Bytes

Contents

# @list = LinkedUser
#   .order(Sequel.desc(:updated_at))
#   .where(user_id: user.id)
#   .limit(20)
#   .all
#   .precache(:job_id)
#   .precache(:org_id)

class Sequel::Model
  module ClassMethods
    # find will cache all finds in a scope
    def find id
      return nil if id.blank?
      Lux.current.cache("#{self}/#{id}") { where(id:id).first }
    end
  end
end

class Array
  # we have to call all on set and then precache
  def precache field, klass=nil
    list = self.map(&field).uniq.sort
    klass ||= field.to_s.sub(/_ids?$/, '').classify.constantize

    for el in klass.where(id: list).all
      Lux.current.cache("#{klass}/#{el.id}") { el.dup }
    end

    self
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
lux-fw-0.5.37 ./plugins/db/helpers/find_precache.rb
lux-fw-0.5.36 ./plugins/db/helpers/find_precache.rb
lux-fw-0.5.35 ./plugins/db/helpers/find_precache.rb
lux-fw-0.5.34 ./plugins/db/helpers/find_precache.rb
lux-fw-0.5.33 ./plugins/db/helpers/find_precache.rb
lux-fw-0.5.32 ./plugins/db/helpers/find_precache.rb