Permalink
Browse files

Abstracting redid cache into a separate class, added caching to all r…

…ecommender methods.
  • Loading branch information...
1 parent 870ba26 commit 7fbe250d64e01f3ee240bdc7a209950b95467bfd @vasinov committed Jan 16, 2013
Showing with 110 additions and 34 deletions.
  1. +1 −0 lib/jruby_mahout.rb
  2. +82 −34 lib/jruby_mahout/recommender.rb
  3. +27 −0 lib/jruby_mahout/redis_cache.rb
View
@@ -8,6 +8,7 @@ module JrubyMahout
require 'jruby_mahout/recommender'
require 'jruby_mahout/recommender_builder'
require 'jruby_mahout/data_model'
+ require 'jruby_mahout/redis_cache'
require 'jruby_mahout/evaluator'
require 'jruby_mahout/postgres_manager'
require 'jruby_mahout/mysql_manager'
@@ -1,6 +1,6 @@
module JrubyMahout
class Recommender
- attr_accessor :is_weighted, :neighborhood_size, :similarity_name, :recommender_name, :data_model, :recommender, :cache
+ attr_accessor :is_weighted, :neighborhood_size, :similarity_name, :recommender_name, :data_model, :recommender, :redis_cache
def initialize(similarity_name, neighborhood_size, recommender_name, is_weighted)
@is_weighted = is_weighted
@@ -13,23 +13,18 @@ def initialize(similarity_name, neighborhood_size, recommender_name, is_weighted
@is_weighted)
@data_model = nil
@recommender = nil
- @cache = false
+ @redis_cache = RedisCache.new(nil, false, nil)
end
def data_model=(data_model)
@data_model = data_model
@recommender = @recommender_builder.build_recommender(@data_model)
end
- def cache=(val)
- @cache = val unless !(@redis and [true, false].include? val)
- end
-
def recommend(user_id, number_of_items, rescorer)
- if @recommender.nil?
- nil
- else
- cached_recommendations = (@cache) ? $redis.get("generic_prefix-user_id:#{user_id}") : nil
+ begin
+ cached_recommendations = (@redis_cache.on?) ? @redis_cache.redis.get("#{@redis_cache.prefix}-recommendations-user_id:#{user_id}-number_of_items:#{number_of_items}")
+ : nil
if cached_recommendations
JSON.parse(cached_recommendations)
@@ -41,54 +36,107 @@ def recommend(user_id, number_of_items, rescorer)
recommendations_array << [recommendation.getItemID, recommendation.getValue.round(5)]
end
- $redis.set("generic_prefix-user_id:#{user_id}", recommendations_array.to_json) unless !@cache
+ @redis_cache.redis.set("#{@redis_cache.prefix}-recommendations-user_id:#{user_id}-number_of_items:#{number_of_items}", recommendations_array.to_json) unless @redis_cache.off?
recommendations_array
end
+ rescue Exception => e
+ e
end
end
def evaluate(training_percentage, evaluation_percentage)
- evaluator = Evaluator.new(@data_model, @recommender_builder)
- evaluator.evaluate(training_percentage, evaluation_percentage)
+ begin
+ evaluator = Evaluator.new(@data_model, @recommender_builder)
+ evaluator.evaluate(training_percentage, evaluation_percentage)
+ rescue Exception => e
+ e
+ end
end
def similar_items(item_id, number_of_items, rescorer)
- if @recommender.nil? or @recommender_name == "GenericUserBasedRecommender"
- nil
- else
- similarities = @recommender.mostSimilarItems(item_id, number_of_items, rescorer)
- similarities_array = []
-
- similarities.each do |similarity|
- similarities_array << similarity.getItemID
+ begin
+ cached_similar_items = (@redis_cache.on?) ? @redis_cache.redis.get("#{@redis_cache.prefix}-similar_items-item_id:#{item_id}-number_of_items:#{number_of_items}")
+ : nil
+
+ if cached_similar_items
+ JSON.parse(cached_similar_items)
+ else
+ similarities = @recommender.mostSimilarItems(item_id, number_of_items, rescorer)
+ similarities_array = []
+
+ similarities.each do |similarity|
+ similarities_array << similarity.getItemID
+ end
+
+ @redis_cache.redis.set("#{@redis_cache.prefix}-similar_items-item_id:#{item_id}-number_of_items:#{number_of_items}", similarities_array.to_json) unless @redis_cache.off?
+
+ similarities_array
end
- similarities_array
+ rescue Exception => e
+ e
end
end
def similar_users(user_id, number_of_users, rescorer)
- if @recommender.nil? or @recommender_name == "GenericItemBasedRecommender"
- nil
- else
- to_array(@recommender.mostSimilarUserIDs(user_id, number_of_users, rescorer))
+ begin
+ cached_similar_users = (@redis_cache.on?) ? @redis_cache.redis.get("#{@redis_cache.prefix}-similar_users-user_id:#{user_id}-number_of_items:#{number_of_items}")
+ : nil
+
+ if cached_similar_users
+ JSON.parse(cached_similar_users)
+ else
+ similar_users = to_array(@recommender.mostSimilarUserIDs(user_id, number_of_users, rescorer))
+
+ @redis_cache.redis.set("#{@redis_cache.prefix}-similar_users-user_id:#{user_id}-number_of_items:#{number_of_items}", similar_users.to_json) unless @redis_cache.off?
+
+ similar_users
+ end
+
+ rescue Exception => e
+ e
end
end
def estimate_preference(user_id, item_id)
- if @recommender.nil?
- nil
- else
- @recommender.estimatePreference(user_id, item_id)
+ begin
+ cached_estimate_preference = (@redis_cache.on?) ? @redis_cache.redis.get("#{@redis_cache.prefix}-estimate_preference-user_id:#{user_id}-item_id:#{item_id}-number_of_items:#{number_of_items}")
+ : nil
+
+ if cached_estimate_preference
+ JSON.parse(cached_estimate_preference)
+ else
+ estimate_preference = @recommender.estimatePreference(user_id, item_id)
+
+ @redis_cache.redis.set("#{@redis_cache.prefix}-estimate_preference-user_id:#{user_id}-item_id:#{item_id}-number_of_items:#{number_of_items}", estimate_preference.to_json) unless @redis_cache.off?
+
+ estimate_preference
+ end
+
+ rescue Exception => e
+ e
end
end
def recommended_because(user_id, item_id, number_of_items)
- if @recommender.nil? or @recommender_name == "GenericUserBasedRecommender"
- nil
- else
- to_array(@recommender.recommendedBecause(user_id, item_id, number_of_items))
+ begin
+ cached_recommended_because = (@redis_cache.on?) ? @redis_cache.redis.get("#{@redis_cache.prefix}-recommended_because-user_id:#{user_id}-item_id:#{item_id}-number_of_items:#{number_of_items}")
+ : nil
+
+ if cached_recommended_because
+ JSON.parse(cached_recommended_because)
+ else
+ recommended_because = to_array(@recommender.recommendedBecause(user_id, item_id, number_of_items))
+
+ @redis_cache.redis.set("#{@redis_cache.prefix}-recommended_because-user_id:#{user_id}-item_id:#{item_id}-number_of_items:#{number_of_items}", recommended_because.to_json) unless @redis_cache.off?
+
+ recommended_because
+ end
+
+
+ rescue Exception => e
+ e
end
end
@@ -0,0 +1,27 @@
+module JrubyMahout
+ class RedisCache
+ attr_accessor :on, :redis, :prefix
+
+ def initialize(redis, on, prefix)
+ @redis = redis
+ @on = on
+ @prefix = prefix
+ end
+
+ def on?
+ @on
+ end
+
+ def off?
+ !@on
+ end
+
+ def empty!(value, params)
+ params_string = ""
+ params.each do |key, val|
+ params_string += "-#{key}:#{val}"
+ end
+ @redis.del("#{@prefix}-#{value}#{params_string}")
+ end
+ end
+end

0 comments on commit 7fbe250

Please sign in to comment.