2012-02-27 53 views
1

我有一個Rails 3.1應用程序運行一個MySQL服務器來存儲數據。 該應用程序中90%的數據非常適合關係數據庫。與MySQL的軌道和一個小的Redis商店

其他10%是一個非常大的散列,我需要將其拉出,更改並放回相當快。在mysql中將所有這些數據片段放在一起,是一個相當大的查詢,但是一旦我有了它,我想我會將它保存爲散列,並且用戶可以與散列進行交互並進行更改。這些變化永遠不會被保存回mysql,因爲mysql不需要它們。

因此,我決定將redis添加到我的rails應用程序,並且redis-objects gem被朋友推薦。

我創建了我active_hash模型和控制器等等

 
class ActiveHash < ActiveRecord::Base 
    include Redis::Objects 
end 

class ActiveHashesController < ApplicationController 

    def show 
    #this is a big query with a bunch of merges, but simplified here as it isn't important 
    active = Game.find(params[:id]) 

    active_hash_in_redis = ActiveHash.new() 
     if active_hash_in_redis.save 
      render :json => active_hash 
     else 
      render :text => "didn't save" 
     end 
    end 

end 

,當我瀏覽到active_hashes/ID,我得到一個錯誤,沒有MySQL表active_hashes,這是正確的,因爲這是應該成爲我的redis db,如模型中所定義的那樣。

任何人都可以向我解釋如何在我的應用程序中使用這兩個dbs,和/或指向我做一個教程?我一直無法找到任何東西。正在使用Redis-對象錯誤的方式去與此?還有其他建議嗎?

回答

1

事實證明,這對我來說有點混亂,但希望這可以幫助其他人。

我沒有最終使用redis-objects,gem,我安裝了redis-rb和gem redis。 然後我建立配置文件作爲

 
require 'redis' 
$redis = Redis.new() 

我的模型是目前實際還是空白,在我的控制,我用

 
class ActiveHashesController < ApplicationController 

    def show 
    #this is a big query with a bunch of merges, but simplified here as it isn't important 
    active = Game.find(params[:id]) 

    $redis.set params[:id], active.to_json 
    get_game = $redis.get params[:id] 
    render :json => get_game 

     end 
    end 

end