Skip to content

Commit

Permalink
add explicit clear cache to soulheart loader
Browse files Browse the repository at this point in the history
  • Loading branch information
sethherr committed Mar 23, 2016
1 parent 59c8000 commit 43bfe16
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 26 deletions.
17 changes: 7 additions & 10 deletions lib/soulheart/loader.rb
Expand Up @@ -55,22 +55,19 @@ def delete_data(id="#{Soulheart.base_id}:")
# everything will work itself out as soon as the cache expires again.
end

def remove_results_hash(cat_ids)
# delete the data store
# We don't do this every time we clear because because it breaks the caching feature.
# The option to clear this is only called in testing right now.
# There should be an option to clear it other times though.
cat_ids.map { |cat_id| redis.expire(no_query_id(cat_id), 0) }
def clear_cache
# Remove the remove_results_hash
# has to be called before the cat_ids are cleared
category_combos.map { |cat| redis.expire(no_query_id(category_id(cat)), 0) }
redis.expire results_hashes_id, 0
redis.del(results_hashes_id)
end

def clear(remove_results=false)
cat_ids = category_combos.map { |cat| category_id(cat) }
cat_ids.each {|cat_id| delete_data(cat_id) }
def clear(should_clear_cache = false)
clear_cache if should_clear_cache
category_combos.each {|cat| delete_data(category_id(cat)) }
delete_categories
delete_data
remove_results_hash(cat_ids) if remove_results
end

def load(items)
Expand Down
2 changes: 1 addition & 1 deletion lib/soulheart/version.rb
@@ -1,3 +1,3 @@
module Soulheart
VERSION = '0.2.4'
VERSION = '0.2.6'
end
87 changes: 72 additions & 15 deletions spec/soulheart/loader_spec.rb
Expand Up @@ -161,7 +161,6 @@
expect(prefixed.count).to eq(1)
end


it "doesn't add all if no_all" do
items = []
file = File.read('spec/fixtures/multiple_categories.json')
Expand Down Expand Up @@ -199,8 +198,71 @@
end
end

describe :clear do
it "deletes everything, but leaves the cache" do
describe :clear do
context 'remove_results false (default)' do
it "deletes everything, but leaves the cache" do
items = [
{'text' => 'Brompton Bicycle', 'category' => 'Gooble'},
{'text' => 'Surly Bicycle', 'category' => 'Bluster'},
{"text" => "Defaulted"}
]
search_opts = {'categories' => 'Bluster, Gooble', 'q' => 'brom'}

loader = Soulheart::Loader.new

redis = loader.redis
loader.load(items)
redis = loader.redis
expect(redis.hget(loader.results_hashes_id, 'brompton bicycle').length).to be > 0
expect((redis.zrange "#{loader.category_id('gooble')}brom", 0, -1)[0]).to eq("brompton bicycle")
expect((redis.zrange "#{loader.category_id('blustergooble')}brom", 0, -1)[0]).to eq("brompton bicycle")

matches1 = Soulheart::Matcher.new(search_opts).matches
expect(matches1[0]['text']).to eq("Brompton Bicycle")

loader.clear
expect(redis.hget(loader.results_hashes_id, 'brompton bicycle')).to_not be_nil
prefixed = redis.zrange "#{loader.category_id('gooble')}brom", 0, -1
expect(prefixed).to be_empty
expect(redis.zrange "#{loader.category_id('blustergooble')}brom", 0, -1).to be_empty
expect(redis.smembers(loader.categories_id).include?('gooble')).to be_false

matches2 = Soulheart::Matcher.new(search_opts).matches
expect(matches2[0]['text']).to eq("Brompton Bicycle")
expect(Soulheart::Matcher.new(search_opts.merge("cache" => false)).matches).to be_empty
end
end
context 'remove_results true' do
it 'removes everything including the results' do
items = [
{'text' => 'Brompton Bicycle', 'category' => 'Gooble'},
{'text' => 'Surly Bicycle', 'category' => 'Bluster'},
{"text" => "Defaulted"}
]
search_opts = {'categories' => 'Bluster, Gooble', 'q' => 'brom'}

loader = Soulheart::Loader.new

redis = loader.redis
loader.load(items)
redis = loader.redis
expect(redis.hget(loader.results_hashes_id, 'brompton bicycle').length).to be > 0
expect(redis.zrange "#{loader.no_query_id(loader.category_id('gooble'))}", 0, -1).to_not be_nil
# expect((redis.zrange "#{loader.no_query_id('gooble')}", 0, -1)[0]).to eq("brompton bicycle")
expect((redis.zrange "#{loader.category_id('gooble')}brom", 0, -1)[0]).to eq("brompton bicycle")
expect((redis.zrange "#{loader.category_id('blustergooble')}brom", 0, -1)[0]).to eq("brompton bicycle")

matches1 = Soulheart::Matcher.new(search_opts).matches
expect(matches1[0]['text']).to eq("Brompton Bicycle")

loader.clear(true)
expect(redis.zrange "#{loader.no_query_id(loader.category_id('gooble'))}", 0, -1).to eq([])
expect(redis.hget(loader.results_hashes_id, 'brompton bicycle')).to be_nil
end
end
end
describe :clear_cache do
it 'removes the cache' do
items = [
{'text' => 'Brompton Bicycle', 'category' => 'Gooble'},
{'text' => 'Surly Bicycle', 'category' => 'Bluster'},
Expand All @@ -214,23 +276,18 @@
loader.load(items)
redis = loader.redis
expect(redis.hget(loader.results_hashes_id, 'brompton bicycle').length).to be > 0
expect(redis.zrange "#{loader.no_query_id(loader.category_id('gooble'))}", 0, -1).to_not be_nil
# expect((redis.zrange "#{loader.no_query_id('gooble')}", 0, -1)[0]).to eq("brompton bicycle")
expect((redis.zrange "#{loader.category_id('gooble')}brom", 0, -1)[0]).to eq("brompton bicycle")
expect((redis.zrange "#{loader.category_id('blustergooble')}brom", 0, -1)[0]).to eq("brompton bicycle")

matches1 = Soulheart::Matcher.new(search_opts).matches
expect(matches1[0]['text']).to eq("Brompton Bicycle")

loader.clear
expect(redis.hget(loader.results_hashes_id, 'brompton bicycle')).to_not be_nil
prefixed = redis.zrange "#{loader.category_id('gooble')}brom", 0, -1
expect(prefixed).to be_empty
expect(redis.zrange "#{loader.category_id('blustergooble')}brom", 0, -1).to be_empty
expect(redis.smembers(loader.categories_id).include?('gooble')).to be_false

matches2 = Soulheart::Matcher.new(search_opts).matches
expect(matches2[0]['text']).to eq("Brompton Bicycle")
expect(Soulheart::Matcher.new(search_opts.merge("cache" => false)).matches).to be_empty
loader.clear_cache
expect(redis.zrange "#{loader.no_query_id(loader.category_id('gooble'))}", 0, -1).to eq([])
expect((redis.zrange "#{loader.category_id('gooble')}brom", 0, -1)[0]).to eq("brompton bicycle")
expect((redis.zrange "#{loader.category_id('blustergooble')}brom", 0, -1)[0]).to eq("brompton bicycle")
end
end

end
end

0 comments on commit 43bfe16

Please sign in to comment.