Module: QueryDam
- Defined in:
- lib/query_dam.rb,
lib/query_dam/version.rb,
lib/query_dam/concerns/trackable.rb
Defined Under Namespace
Modules: Trackable
Constant Summary collapse
- DEFAULT_EXPIRATION =
5.minutes
- VERSION =
'0.2.0'
Class Method Summary collapse
- .expiration ⇒ Object
- .expiration=(value) ⇒ Object
- .get_updates(tracking_key) ⇒ Object
- .query_sets(tracking_key) ⇒ Object
- .watch_relation(relation) ⇒ Object
Class Method Details
.expiration ⇒ Object
54 55 56 |
# File 'lib/query_dam.rb', line 54 def expiration @expiration || DEFAULT_EXPIRATION end |
.expiration=(value) ⇒ Object
50 51 52 |
# File 'lib/query_dam.rb', line 50 def expiration=(value) @expiration = value end |
.get_updates(tracking_key) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/query_dam.rb', line 23 def get_updates(tracking_key) query_hash = Redis::HashKey.new(tracking_key) return nil unless query_hash.exists? query_hash.expire(expiration) model = query_hash[:model].constantize additions_set, updates_set, removals_set = query_sets(tracking_key) updated_ids = (additions_set.members + updates_set.members).uniq removed_ids = removals_set.members result = { model: model, updates: model.where(id: updated_ids), exclusions: removed_ids.map(&:to_i) } [additions_set, updates_set, removals_set].each(&:clear) result end |
.query_sets(tracking_key) ⇒ Object
44 45 46 47 48 |
# File 'lib/query_dam.rb', line 44 def query_sets(tracking_key) %w[additions updates removals].map do |set_type| Redis::Set.new("#{tracking_key} #{set_type}") end end |
.watch_relation(relation) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/query_dam.rb', line 13 def watch_relation(relation) tracking_key = SecureRandom.uuid query = relation.select("`#{relation.model.table_name}`.`id`").to_sql query_hash = Redis::HashKey.new(tracking_key) query_hash.bulk_set(model: relation.model.name, query: query) query_hash.expire(expiration) Redis::Set.new(relation.model.name) << tracking_key tracking_key end |