module Sequel::Plugins::StaticCacheCache::ClassMethods

Public Instance Methods

dump_static_cache_cache() click to toggle source

Dump the in-memory cached rows to the cache file.

   # File lib/sequel/plugins/static_cache_cache.rb
28 def dump_static_cache_cache
29   File.open(@static_cache_cache_file, 'wb'){|f| f.write(Marshal.dump(sort_static_cache_hash(@static_cache_cache)))}
30   nil
31 end

Private Instance Methods

_load_static_cache_rows(ds, key) click to toggle source

Check the cache first for the key, and return rows without a database query if present. Otherwise, get all records in the provided dataset, and update the cache with them.

   # File lib/sequel/plugins/static_cache_cache.rb
81 def _load_static_cache_rows(ds, key)
82   if rows = Sequel.synchronize{@static_cache_cache[key]}
83     rows.map{|row| call(row)}.freeze
84   else
85     rows = ds.all.freeze
86     raw_rows = rows.map(&:values)
87     Sequel.synchronize{@static_cache_cache[key] = raw_rows}
88     rows
89   end
90 end
load_static_cache_rows() click to toggle source

Load the rows for the model from the cache if available. If not available, load the rows from the database, and then update the cache with the raw rows.

   # File lib/sequel/plugins/static_cache_cache.rb
67 def load_static_cache_rows
68   _load_static_cache_rows(dataset, name)
69 end
load_subset_static_cache_rows(ds, meth) click to toggle source

Load the rows for the subset from the cache if available. If not available, load the rows from the database, and then update the cache with the raw rows.

   # File lib/sequel/plugins/static_cache_cache.rb
74 def load_subset_static_cache_rows(ds, meth)
75   _load_static_cache_rows(ds, [name, meth].freeze)
76 end
sort_static_cache_hash(cache) click to toggle source

Sort the given static cache hash in a deterministic way, so that the same static cache values will result in the same marshal file.

   # File lib/sequel/plugins/static_cache_cache.rb
39 def sort_static_cache_hash(cache)
40   cache = cache.sort do |a, b|
41     a, = a
42     b, = b
43     if a.is_a?(Array)
44       if b.is_a?(Array)
45         a_name, a_meth = a
46         b_name, b_meth = b
47         x = a_name <=> b_name
48         if x.zero?
49           x = a_meth <=> b_meth
50         end
51         x
52       else
53         1
54       end
55     elsif b.is_a?(Array)
56       -1
57     else
58       a <=> b
59     end
60   end
61   Hash[cache]
62 end