Skip to content

Commit

Permalink
Lazily load shards and shard databases (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
djmb authored Aug 10, 2023
1 parent bdf12c1 commit eab54dc
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/solid_cache/cluster/connection_handling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@
module SolidCache
class Cluster
module ConnectionHandling
attr_reader :async_writes, :shards, :database_shards
attr_reader :async_writes

def initialize(options = {})
super(options)
@shards = options.delete(:shards) || SolidCache.all_shard_keys || [nil]
@database_shards = @shards.to_h { |shard| [ SolidCache.shard_databases[shard], shard ] }
@shard_options = options.delete(:shards)
@async_writes = options.delete(:async_writes)
end

def shards
# Load lazily as the cache maybe created before the SolidCache connections are initialized
@shards ||= @shard_options || SolidCache.all_shard_keys || [nil]
end

def database_shards
@database_shards ||= shards.compact.to_h { |shard| [ SolidCache.shard_databases[shard], shard ] }
end

def writing_all_shards
return enum_for(:writing_all_shards) unless block_given?

Expand Down

0 comments on commit eab54dc

Please sign in to comment.