Class: Queris::QueryStore

Inherits:
Model
  • Object
show all
Defined in:
lib/queris/query_store.rb

Defined Under Namespace

Classes: Metaquery

Constant Summary

Constants included from Queris

VERSION

Class Attribute Summary collapse

Attributes inherited from Model

#id, #query_score

Class Method Summary collapse

Methods inherited from Model

#as_json, attr_val_block, attribute, #attribute_diff, attributes, attrs, #changed, #changed?, #delete, #deleted?, expire, find_all, get, #hash_key, #import, #increment, #initialize, keyf, #load, #load_missing, #loaded?, #noload, #noload?, prefix, #redis, #redis=, restore, #save, #set_id, #to_json, #while_loading

Methods included from QuerisModelMixin

included

Methods included from ObjectMixin

#get_cached_attribute, included

Methods included from Queris

add_redis, all_redises, build_missing_indices!, clear_cache!, debug?, digest, disconnect, duplicate_redis_client, from_redis_float, import_lua_script, included, info, load_lua_script, #log_stats_per_request!, #log_stats_per_request?, model, rebuild!, reconnect, redis_prefix, redis_role, redises, register_model, run_script, script, script_hash, #stats, to_redis_float, #track_stats!, #track_stats?

Constructor Details

This class inherits a constructor from Queris::Model

Class Attribute Details

.metaquery_ttlObject

Returns the value of attribute metaquery_ttl.



18
19
20
# File 'lib/queris/query_store.rb', line 18

def metaquery_ttl
  @metaquery_ttl
end

Class Method Details

.add(query) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/queris/query_store.rb', line 37

def add(query)
  redis.pipelined do
    redis_indices.each {|i| i.add query}
    update query
  end
  #puts "added #{query} to QueryStore"
end

.all_metaqueriesObject

NOT EFFICIENT!



58
59
60
61
62
# File 'lib/queris/query_store.rb', line 58

def all_metaqueries
  q=query(self, :ttl => 20).static!
  redis_indices(live: true).each { |i| q.union(i) }
  q.results
end

.clear!Object Also known as: clear_queries!

wipe all metaquery info



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/queris/query_store.rb', line 78

def clear!
  querykeys = redis_master.keys query(self).results_key(nil, "*")
  expirekeys = redis_master.keys "Queris:Metaquery:expire:*"
  indexkeys = redis_master.keys redis_index(:index).key("*", nil, true)
  total = querykeys.count + expirekeys.count + indexkeys.count
  print "Clearing everything (#{total} keys) from QueryStore..."
  [querykeys, expirekeys, indexkeys].each do |keys|
    redis_master.multi do |r|
      keys.each { |k| r.del k }
    end
  end
  puts "ok"
  total
end

.index_to_val(index) ⇒ Object



33
34
35
# File 'lib/queris/query_store.rb', line 33

def index_to_val(index)
  Index === index ? "#{index.model.name}:#{index.class.name.split('::').last}:#{index.name}" : index
end

.load(marshaled) ⇒ Object Also known as: find



72
73
74
# File 'lib/queris/query_store.rb', line 72

def load(marshaled)
  Marshal.load(marshaled)
end

.metaquery(arg = {}) ⇒ Object



68
69
70
# File 'lib/queris/query_store.rb', line 68

def metaquery(arg={})
  query self, arg
end

.query(model = nil, arg = {}) ⇒ Object



64
65
66
67
# File 'lib/queris/query_store.rb', line 64

def query(model=nil, arg={})
  model ||= self
  Metaquery.new(self, arg.merge(:target_model => model, :realtime => true))
end

.redis(another_model = nil) ⇒ Object

Raises:



19
20
21
22
23
24
25
26
27
28
# File 'lib/queris/query_store.rb', line 19

def redis(another_model = nil)
  another_model = another_model.model if Query === another_model
  if another_model == self
    r = Queris.redis "metaquery:metaquery"
  else
    r= Queris.redis :'metaquery:slave', :metaquery
  end
  raise Error, "No appropriate redis connection found for QueryStore. Add a queris connection with the metaquery role (Queris.add_redis(r, :metaquery), or add live_queries to desired models." unless r
  r
end

.redis_masterObject



29
30
31
# File 'lib/queris/query_store.rb', line 29

def redis_master
  Queris.redis :metaquery, :master
end

.remove(query) ⇒ Object

puts “added #query to QueryStore”



44
45
46
47
48
49
# File 'lib/queris/query_store.rb', line 44

def remove(query)
  redis.pipelined do
    redis_indices.each { |i| i.remove query }
  end
  #puts "removed #{query} from QueryStore"
end

.update(query) ⇒ Object

puts “removed #query from QueryStore”



50
51
52
53
54
55
# File 'lib/queris/query_store.rb', line 50

def update(query)
  redis.pipelined do
    redis.setex "Queris:Metaquery:expire:#{query.marshaled}", query.ttl, ""
  end
  #puts "updated #{query} for QueryStore"
end