View quorum.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Node is an abstraction of the db | |
class Node | |
attr_reader :id | |
def initialize(id) | |
@id = id | |
@data = {} | |
end | |
def read(key) | |
@data[key] | |
end |
View validate_associated_n_queries.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Blog post: https://veerpalbrar.github.io/blog/2021/11/26/Fix-N+1-queries-when-using-validates_associated-with-has_many | |
### Setup | |
require "bundler/inline" | |
gemfile(true) do | |
source "https://rubygems.org" | |
gem "rails" | |
gem "sqlite3" | |
end |
View consistent_hashing.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'digest' | |
class ConsistentHashing | |
def initialize(nodes) | |
nodes.map { |node| add_node(node) } | |
end | |
def find_cache(key) | |
puts | |
hash = hash_value(key) |