Module: Spinel::Searcher

Included in:
Client
Defined in:
lib/spinel/searcher.rb

Instance Method Summary collapse

Instance Method Details

#cache_enable(key) ⇒ Object



31
32
33
# File 'lib/spinel/searcher.rb', line 31

def cache_enable key
  Spinel.redis.exists(key)
end

#search(term, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/spinel/searcher.rb', line 4

def search term, options = {}
  options = search_option options
  words = search_word_split term
  return [] if words.empty?

  tmp_cachekey = cachekey(words)

  set_cache(tmp_cachekey, words) unless options[:cache] && cache_enable(tmp_cachekey)

  ids = Spinel.redis.zrevrange(tmp_cachekey, 0, options[:limit] - 1)
  ids.empty? ? [] : Spinel.redis.hmget(database, *ids).compact.map{|json| MultiJson.decode(json)}
end

#search_option(options = {}) ⇒ Object



17
18
19
# File 'lib/spinel/searcher.rb', line 17

def search_option options = {}
  { limit: Spinel.search_limit, cache: true }.merge(options)
end

#search_word_split(word) ⇒ Object



21
22
23
# File 'lib/spinel/searcher.rb', line 21

def search_word_split word
  squish(word).split.reject{|w| w.size < Spinel.minimal_word}.sort
end

#set_cache(key, words) ⇒ Object



25
26
27
28
29
# File 'lib/spinel/searcher.rb', line 25

def set_cache key, words
  interkeys = words.map{ |w| index w }
  Spinel.redis.zinterstore(key, interkeys)
  Spinel.redis.expire(key, Spinel.cache_expire)
end