2012-02-27 82 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