Skip to content

Instantly share code, notes, and snippets.

class Indexer
attr :dictionary
LETTER_TO_NUMBER = {
2 => ['a','b','c'],
3 => ['d','f','e'],
4 => ['g','h','i'],
5 => ['j','k','l'],
6 => ['m','n','o'],
7 => ['p','q','r','s'],
did not find key in cache, executing block ...
executing : select count(*) from users
::1 - - [31/Mar/2017:23:34:34 -0400] "GET / HTTP/1.1" 200 15 0.0102
{"number_of_users"=>{:value=>{:count=>33}, :expiration_time=>1491017684}}
fetch from cache and will expire in 8
::1 - - [31/Mar/2017:23:34:36 -0400] "GET / HTTP/1.1" 200 15 0.0008
{"number_of_users"=>{:value=>{:count=>33}, :expiration_time=>1491017684}}
fetch from cache and will expire in 6
class Cache
@redis = {}
def self.fetch key, expires_in = 30, &block
puts ""
puts @redis
if @redis.key?(key) && (@redis[key][:expiration_time] > Time.now.to_i)
# fetch and return result
puts "fetch from cache and will expire in #{@redis[key][:expiration_time] - Time.now.to_i}"
@redis[key][:value]
else
$ rackup
[2017-03-31 23:24:49] INFO WEBrick 1.3.1
[2017-03-31 23:24:49] INFO ruby 2.3.0 (2015-12-25) [x86_64-darwin15]
[2017-03-31 23:24:49] INFO WEBrick::HTTPServer#start: pid=51672 port=9292
did not find key in cache, executing block ...
executing : select count(*) from users
::1 - - [31/Mar/2017:23:25:01 -0400] "GET / HTTP/1.1" 200 15 0.0385
fetch from cache
::1 - - [31/Mar/2017:23:25:04 -0400] "GET / HTTP/1.1" 200 15 0.0012
fetch from cache
class Cache
@redis = {}
def self.fetch key, &block
if @redis.key?(key)
# fetch and return result
puts "fetch from cache"
@redis[key]
else
if block_given?
# make the DB query and create a new entry for the request result
class Main < Sinatra::Base
get '/' do
# make call to the database
result = number_of_users
"Hello World! #{result[:count]}"
end
def number_of_users
Cache.fetch "number_of_users" do
FakeDatabase.connection "select count(*) from users"
require 'sinatra'
class Main < Sinatra::Base
get '/' do
# make call to the database
result = FakeDatabase.connection "select count(*) from users"
"Hello World! #{result[:count]}"
end
end
class FakeDatabase
def self.connection query
puts "executing : #{query}"
return {count: 33}
end
end
require 'pp'
require 'pry-byebug'
class Grid
attr_accessor :grid
def initialize
@grid = Array.new
read_input 'input.txt'
0.upto(99) do
tick
require 'pry-byebug'
@wires = {}
def is_integer? n
return n.to_i.to_s == n
end
def first_pass
File.readlines('input.txt').each do |line|
line = line.split("->")