module Spinel::Searcher
Public Instance Methods
cache_enable(key)
click to toggle source
# File lib/spinel/searcher.rb, line 31 def cache_enable key Spinel.redis.exists(key) end
search(term, options = {})
click to toggle source
# 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 = {})
click to toggle source
# File lib/spinel/searcher.rb, line 17 def search_option options = {} { limit: Spinel.search_limit, cache: true }.merge(options) end
search_word_split(word)
click to toggle source
# 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)
click to toggle source
# 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