Skip to content

Instantly share code, notes, and snippets.

@leeky
Created March 1, 2016 14:12
Show Gist options
  • Save leeky/1d29af460ff1ae276069 to your computer and use it in GitHub Desktop.
Save leeky/1d29af460ff1ae276069 to your computer and use it in GitHub Desktop.
Model to Redis Example
products = Product.includes(:coffee).all
coffees = Coffee.all
r = Redis.current
r.keys('product*').each { |k| r.del(k) }
r.keys('coffee*').each { |k| r.del(k) }
products.each do |p|
sku = p.sku
r.sadd 'products', sku
r.sadd "products:available:#{p.available}", sku
r.sadd "products:product_type:#{p.product_type}", sku
r.set "product:#{sku}", p.to_json
r.set "product:#{sku}:coffee_id", p.coffee.id if p.coffee.present?
end
coffees.each do |c|
id = c.id
r.sadd 'coffees', id
r.sadd "coffees:decaf:#{c.decaf}", id
r.sadd "coffees:limited:#{c.limited}", id
r.set "coffee:#{id}", c.to_json
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment