module Sequel::Postgres::StaticCacheUpdater

  1. lib/sequel/extensions/pg_static_cache_updater.rb

Public Instance methods

create_static_cache_update_function(opts=OPTS)

Add the static cache update function to the PostgreSQL database. This must be added before any triggers using this function are added.

Options:

:channel_name

Override the channel name to use.

:function_name

Override the function name to use.

[show source]
   # File lib/sequel/extensions/pg_static_cache_updater.rb
80       def create_static_cache_update_function(opts=OPTS)
81         create_function(opts[:function_name]||default_static_cache_update_name, <<SQL, :returns=>:trigger, :language=>:plpgsql)
82 BEGIN
83   PERFORM pg_notify(#{literal((opts[:channel_name]||default_static_cache_update_name).to_s)}, TG_RELID::text);
84   RETURN NULL;
85 END
86 SQL
87       end
create_static_cache_update_trigger(table, opts=OPTS)

Add a trigger to the given table that calls the function which will notify about table changes.

Options:

:function_name

Override the function name to use.

:trigger_name

Override the trigger name to use.

[show source]
   # File lib/sequel/extensions/pg_static_cache_updater.rb
95 def create_static_cache_update_trigger(table, opts=OPTS)
96   create_trigger(table, opts[:trigger_name]||default_static_cache_update_name, opts[:function_name]||default_static_cache_update_name, :after=>true)
97 end
default_static_cache_update_name()

The default name for the function, trigger, and notification channel for this extension.

[show source]
    # File lib/sequel/extensions/pg_static_cache_updater.rb
101 def default_static_cache_update_name
102   :sequel_static_cache_update
103 end
listen_for_static_cache_updates(models, opts=OPTS)

Listen on the notification channel for changes to any of tables for the models given in a new thread. If notified about a change to one of the tables, reload the cache for the related model. Options given are also passed to Database#listen.

Note that this implementation does not currently support multiple models that use the same underlying table.

Options:

:channel_name

Override the channel name to use.

:before_thread_exit

An object that responds to call that is called before the the created thread exits.

[show source]
    # File lib/sequel/extensions/pg_static_cache_updater.rb
117 def listen_for_static_cache_updates(models, opts=OPTS)
118   raise Error, "this database object does not respond to listen, use the postgres adapter with the pg driver" unless respond_to?(:listen)
119   models = [models] unless models.is_a?(Array)
120   raise Error, "array of models to listen for changes cannot be empty" if models.empty?
121 
122   oid_map = {}
123   models.each do |model|
124     raise Error, "#{model.inspect} does not use the static_cache plugin" unless model.respond_to?(:load_cache)
125     oid_map[get(regclass_oid(model.dataset.first_source_table))] = model
126   end
127 
128   Thread.new do
129     begin
130       listen(opts[:channel_name]||default_static_cache_update_name, {:loop=>true}.merge!(opts)) do |_, _, oid|
131         if model = oid_map[oid.to_i]
132           model.load_cache
133         end
134       end
135     ensure
136       opts[:before_thread_exit].call if opts[:before_thread_exit]
137     end
138   end
139 end