Skip to content

Commit

Permalink
Support for no l2 cache mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon committed Oct 27, 2014
1 parent 7ff558d commit 71c5274
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 174 deletions.
3 changes: 2 additions & 1 deletion features/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@

Looksist.configure do |looksist|
looksist.lookup_store = Redis.new(url: 'redis://localhost:6379', driver: :hiredis)
looksist.l2_cache = :no_cache
looksist.driver = Looksist::Serializers::Her
end
end
4 changes: 2 additions & 2 deletions lib/looksist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ module Looksist
include Hashed

class << self
attr_accessor :lookup_store, :driver, :cache_buffer_size, :redis_service
attr_accessor :lookup_store, :driver, :cache_buffer_size, :redis_service, :l2_cache

def configure
yield self
self.redis_service = Looksist::RedisService.instance do |lookup|
lookup.client = self.lookup_store
lookup.buffer_size = self.cache_buffer_size || 50000
lookup.buffer_size = (self.l2_cache == :no_cache) ? 0 : (self.cache_buffer_size || 50000)
end
end

Expand Down
22 changes: 17 additions & 5 deletions lib/looksist/redis_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,22 @@ def flush_cache!
private

def find_all(entity, ids)
raise 'Buffer overflow! Increase buffer size' if ids.length > @buffer_size
raise 'Buffer overflow! Increase buffer size' if ids.length > @buffer_size && @buffer_size != 0
keys = ids.collect { |id| redis_key(entity, id) }
missed_keys = (keys - @cache.keys).uniq
missed_keys = cache_op(proc_from { keys.uniq }) { (keys - @cache.keys).uniq }
unless missed_keys.empty?
values = @client.mget *missed_keys
@cache.merge!(Hash[*missed_keys.zip(values).flatten])
cache_op { @cache.merge!(Hash[*missed_keys.zip(values).flatten]) }
end
(cache_op(proc_from { values }) { @cache.mslice(keys) })
end

def cache_op(computed = nil)
if @buffer_size == 0
return computed.call if computed
else
yield
end
@cache.mslice(keys)
end

def find(entity, id)
Expand All @@ -50,11 +58,15 @@ def find(entity, id)
end

def fetch(key, &block)
@cache[key] ||= block.call
(cache_op(proc_from { block.call }) { @cache[key] ||= block.call })
end

def redis_key(entity, id)
"#{entity.pluralize}/#{id}"
end

def proc_from
Proc.new
end
end
end
2 changes: 1 addition & 1 deletion lib/looksist/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Lookist
VERSION = '0.2.3'
VERSION = '0.2.4'
end
1 change: 0 additions & 1 deletion spec/looksist/hashed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
end

context 'inject ' do

it 'should be capable to deep lookup and inject' do
class Menu
include Looksist
Expand Down
Loading

0 comments on commit 71c5274

Please sign in to comment.