Sha256: ce04ba6c373d4e15dc85bae592252c004b2d244e0c518e6b8b57fb4ebd11b9e5

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

# 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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lux-fw-0.1.35 ./lib/plugins/db_helpers/cached_find_by.rb
lux-fw-0.1.17 ./lib/plugins/db_helpers/cached_find_by.rb