class TieredCaching::ReplicatingStore

Public Class Methods

new(internal_stores, replication_factor = nil) click to toggle source
# File lib/tiered_caching/replicating_store.rb, line 3
def initialize(internal_stores, replication_factor = nil)
  @internal_stores = internal_stores
  @replication_factor = replication_factor || internal_stores.count
end

Public Instance Methods

clear() click to toggle source
# File lib/tiered_caching/replicating_store.rb, line 31
def clear
  @internal_stores.map(&:clear)
end
delete(key) click to toggle source
# File lib/tiered_caching/replicating_store.rb, line 25
def delete(key)
  replication_range(key).map do |index|
    @internal_stores[store_index(index)].delete(key)
  end
end
get(key) click to toggle source
# File lib/tiered_caching/replicating_store.rb, line 15
def get(key)
  index = store_index(hash_for_key(key))
  end_index = store_index(hash_for_key(key) + @replication_factor)
  recursive_get(key, end_index, index)
end
getset(key) { || ... } click to toggle source
# File lib/tiered_caching/replicating_store.rb, line 21
def getset(key)
  get(key) || set(key, yield)
end
set(key, value) click to toggle source
# File lib/tiered_caching/replicating_store.rb, line 8
def set(key, value)
  replication_range(key).map do |index|
    @internal_stores[store_index(index)].set(key, value)
  end
  value
end

Private Instance Methods

hash_for_key(key) click to toggle source
# File lib/tiered_caching/replicating_store.rb, line 62
def hash_for_key(key)
  Digest::MD5.hexdigest(key.to_s).unpack('L').first
end
internal_get(key, end_index, index) click to toggle source
# File lib/tiered_caching/replicating_store.rb, line 37
def internal_get(key, end_index, index)
  return nil if end_index == index

  recursive_get(key, end_index, index)
end
recursive_get(key, end_index, index) click to toggle source
# File lib/tiered_caching/replicating_store.rb, line 43
def recursive_get(key, end_index, index)
  store = @internal_stores[index]
  store.get(key) || begin
    Logging.logger.warn("ReplicatingStore: Cache miss at level #{index}")
    result = internal_get(key, end_index, store_index(index+1))
    result && store.set(key, result)
  end
end
replication_range(key) click to toggle source
# File lib/tiered_caching/replicating_store.rb, line 52
def replication_range(key)
  start_index = hash_for_key(key)
  end_index = start_index + @replication_factor
  (start_index...end_index)
end
store_index(index) click to toggle source
# File lib/tiered_caching/replicating_store.rb, line 58
def store_index(index)
  index % @internal_stores.count
end