簡體   English   中英

Rails 3緩存站點設置,無需重新啟動服務器和第三方工具即可重新加載緩存

[英]rails 3 caching site settings with ability to reload cache without server restart and third-party tools

我需要一些解決方案才能在RoR 3網站中實現以下功能:

網站需要一個用戶評分系統,在該系統中,用戶可以獲得執行某些操作的得分(例如,獲得有關堆棧溢出問題的解答的得分)。

問題是:

1)我需要為某些操作重新分配點數的能力(這種情況並不常見,但是每次需要重新分配時我都無法重新啟動Mongrel,因此代碼常量和YAML不適合)

2)我不能使用簡單的Active Record,因為在5000個用戶上,我將對每個用戶操作執行太多查詢,因此我需要緩存,並且需要在重新分配時重置緩存

3)我想使其不帶有memcached或類似的東西,因為我的服務器硬件足夠舊。

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

那這樣的事情呢?

@@points_loaded = false
@@points

def load_action_points 
    if (File.ctime("setting.yml")  < Time.now) || !@@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

您還可以將點緩存在用戶模型中,如下所示:.pseudocode ...

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 

我找到了一些簡單的解決方案,但是僅當您擁有一個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即可解決多個服務器實例的解決方案,那么我將非常高興看到它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM