Created
March 1, 2016 14:12
-
-
Save leeky/1d29af460ff1ae276069 to your computer and use it in GitHub Desktop.
Model to Redis Example
This file contains hidden or 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
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