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) |
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 |
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 |
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 CountMinSketch | |
def initialize(width, length, hashes) | |
@width = width | |
@length = length | |
@hashes = hashes | |
@table = Array.new(length) { Array.new(width) { 0 } } | |
end | |