Skip to content

Commit

Permalink
New option for Modis::Model - :enable_all_index
Browse files Browse the repository at this point in the history
Allows to disable creating of my:core:key:all kind of keys if they are not needed.
Getting rid of these keys is important feature, because those keys consume a huge amount of memory
(one single *:all key for 158m records consumes for about of 10GB of ram)
Assuming some people don't use Model.all in their code,
it's important to disable that feature on demand
  • Loading branch information
nattfodd committed Jun 12, 2017
1 parent 3bd413f commit 5ef9bea
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/modis/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ def sti_base_key_for(id)
"#{sti_base_absolute_namespace}:#{id}"
end

def enable_all_index(bool)
@use_all_index = bool
end

def all_index_enabled?
@use_all_index == true || @use_all_index.nil?
end

def create(attrs)
model = new(attrs)
model.save
Expand Down Expand Up @@ -128,8 +136,10 @@ def destroy
run_callbacks :destroy do
redis.pipelined do
remove_from_indexes(redis)
redis.srem(self.class.key_for(:all), id)
redis.srem(self.class.sti_base_key_for(:all), id) if self.class.sti_child?
if self.class.all_index_enabled?
redis.srem(self.class.key_for(:all), id)
redis.srem(self.class.sti_base_key_for(:all), id) if self.class.sti_child?
end
redis.del(key)
end
end
Expand Down Expand Up @@ -198,8 +208,10 @@ def persist
future = attrs.any? ? redis.hmset(key, attrs) : :unchanged

if new_record?
redis.sadd(self.class.key_for(:all), id)
redis.sadd(self.class.sti_base_key_for(:all), id) if self.class.sti_child?
if self.class.all_index_enabled?
redis.sadd(self.class.key_for(:all), id)
redis.sadd(self.class.sti_base_key_for(:all), id) if self.class.sti_child?
end
add_to_indexes(redis)
else
update_indexes(redis)
Expand Down
22 changes: 22 additions & 0 deletions spec/persistence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def test_before_save
called_callbacks << :test_before_save
end
end

class MockModelNoAllIndex < MockModel
enable_all_index false
end
end

describe Modis::Persistence do
Expand Down Expand Up @@ -296,4 +300,22 @@ def test_before_save
expect(model.update_attributes(name: nil)).to be false
end
end

describe 'key for all records' do
let(:all_key_name) { "#{PersistenceSpec::MockModel.absolute_namespace}:all" }

describe 'when :enable_all_index option is set to false' do
it 'does not save new record to the *:all key' do
model = PersistenceSpec::MockModel.create!(name: 'Sage')
expect(Redis.new.smembers(all_key_name).map(&:to_i)).to include(model.id)
end
end

describe 'when :enable_all_index option is set to false' do
it 'does not save new record to the *:all key' do
model = PersistenceSpec::MockModelNoAllIndex.create!(name: 'Alex')
expect(Redis.new.smembers(all_key_name).map(&:to_i)).to_not include(model.id)
end
end
end
end

0 comments on commit 5ef9bea

Please sign in to comment.