Module: Sequel::Postgres::StaticCacheUpdater

Defined in:
lib/sequel/extensions/pg_static_cache_updater.rb

Instance Method Summary collapse

Instance Method Details

#create_static_cache_update_function(opts = OPTS) ⇒ Object

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.



75
76
77
78
79
80
81
82
# File 'lib/sequel/extensions/pg_static_cache_updater.rb', line 75

def create_static_cache_update_function(opts=OPTS)
  create_function(opts[:function_name]||default_static_cache_update_name, <<SQL, :returns=>:trigger, :language=>:plpgsql)
BEGIN
  PERFORM pg_notify(#{literal((opts[:channel_name]||default_static_cache_update_name).to_s)}, TG_RELID::text);
  RETURN NULL;
END
SQL
end

#create_static_cache_update_trigger(table, opts = OPTS) ⇒ Object

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.



90
91
92
# File 'lib/sequel/extensions/pg_static_cache_updater.rb', line 90

def create_static_cache_update_trigger(table, opts=OPTS)
  create_trigger(table, opts[:trigger_name]||default_static_cache_update_name, opts[:function_name]||default_static_cache_update_name, :after=>true)
end

#default_static_cache_update_nameObject

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



96
97
98
# File 'lib/sequel/extensions/pg_static_cache_updater.rb', line 96

def default_static_cache_update_name
  :sequel_static_cache_update
end

#listen_for_static_cache_updates(models, opts = OPTS) ⇒ Object

Listen on the notification channel for changes to any of tables for the models given. 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 model models that use the same underlying table.

Options:

:channel_name

Override the channel name to use.

Raises:



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/sequel/extensions/pg_static_cache_updater.rb', line 110

def listen_for_static_cache_updates(models, opts=OPTS)
  raise Error, "this database object does not respond to listen, use the postgres adapter with the pg driver" unless respond_to?(:listen)
  models = [models] unless models.is_a?(Array)
  raise Error, "array of models to listen for changes cannot be empty" if models.empty?

  oid_map = {}
  models.each do |model|
    raise Error, "#{model.inspect} does not use the static_cache plugin" unless model.respond_to?(:load_cache, true)
    oid_map[get(regclass_oid(model.dataset.first_source_table))] = model
  end

  Thread.new do
    listen(opts[:channel_name]||default_static_cache_update_name, {:loop=>true}.merge(opts)) do |_, _, oid|
      if model = oid_map[oid.to_i]
        model.send(:load_cache)
      end
    end
  end
end