# module Sequel::Plugins::LuxCachedFindBy # module ClassMethods # @@cached_find_by ||= {} # @@cache_all_for ||= {} # def find id # return nil if id.blank? # key = "#{self}/#{id}" # Lux.cache.thread(key) { where(id:id).first } # end # def find_by what # where(what).first # end # ### # # in top of the class, defines cache to be run in memory # # cache_all_for 10.minutes # def cache_all_for duration # @@cache_all_for[self.to_s] = duration # end # # City.cached_find_by(120, code: nav.root) # def cached_find_by duration, opts # key = opts.keys.first # value = opts.values.first # time = Time.now.to_i # @@cached_find_by[self.to_s] = nil if @@cached_find_by[self.to_s] && (@@cached_find_by[self.to_s][0] + duration) < time # @@cached_find_by[self.to_s] ||= [time, all] # for el in @@cached_find_by[self.to_s][1] # return el.dup if el[key] == value # end # nil # end # end # end # Sequel::Model.plugin :lux_cached_find_by