2011-02-13 76 views
0

我需要一些解決方案,以按照我的回報率3網站的功能能力3個緩存站點設置:導軌和重新加載緩存,而不重新啓動服務器和第三方工具

網站需要用戶的評價系統,用戶獲得積分用於執行一些操作(例如在stackoverflow上獲取回答問題的點)。

的問題是:

1)我需要重新分配點的數量的一些行動(不是經常的能力,但我不能重新啓動雜種每次我需要重新分配,所以在代碼常量和YAML不適合)

2)我不能使用簡單的活動記錄,因爲對於5000個用戶,我會爲每個用戶操作做太多的查詢,所以我需要緩存以及在重新分配時重置緩存

3)我想使它沒有memcached或類似的東西,導致我的服務器硬件足夠老。

有誰知道這樣的解決方案?

回答

0

我發現了一些簡單的e系統解決方案,但它只能如果你有一個RoR的服務器實例:

class Point < ActiveRecord::Base 
    @@cache = {} 

    validates :name, :presence => true 
    validates :amount, :numericality => true, :presence => true 

    after_save :load_to_cache, :revaluate_users 

    def self.get_for(str) 
    @@cache = Hash[Point.all.map { |point| [point.name.to_sym, point] }] if @@cache == {} #for cache initializing 
    @@cache[str.to_sym].amount 
    end 

    private 

    def load_to_cache 
    @@cache[self.name.to_sym] = self 
    end 

    def revaluate_users 
    #schedule some background method, that will update all users' scores 
    end 
end 

如果你知道不安裝memcached的多個服務器實例的解決方案,我會很高興地看到它。

1

這樣的事情呢?

@@points_loaded = false 
@@points 

def load_action_points 
    if (File.ctime("setting.yml") < Time.now) || [email protected]@points_loaded 
     @@points = YAML::load(File.open('setting.yml')) 
     @@points_loaded = true 
    else 
     @@points 
end 

或使用A :: B和緩存數據庫查詢

class ActionPoints < ActiveRecord::Base 
    extend ActiveSupport::Memoizable 


    def points 
    self.all 
    end 

    memoize :points 
end 

您還緩存點在用戶模式,這樣的事情.. ...僞

class User < A::B 

    serialize :points 


def after_save 
points = PointCalculator(self).points 
end 

end 

和....

class PointCalculator 

     def initialize(user) 
     @@total_points = 0 
      user.actions.each do |action| 
      p = action.times * ActionPoints.find_by_action("did_something_cool").points 
@@total_points = @@total_points + p 
      end 

     end 

    def points 
    @@total_points 
    end 

     end 
+0

我需要一個接口(視圖和控制器至少)編輯這些值。而且我不確定每次用戶做什麼都檢查文件的更新時間是個好主意。 – sandrew 2011-02-13 09:31:52

相關問題