Rails中的永久变量 [英] Permanent variable in Rails

查看:124
本文介绍了Rails中的永久变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说,在我的Rails应用程序之上,有一个显示文本的栏 - 最新的热门交易,预定的停机时间通知等等。这是一种单一的信息,基本上每个请求都需要访问,并且可能会不时更新。什么是最好的方式来实现这一点?



我想要做的是某种永久性全局变量(可从控制器访问)。




  • 它很少会被更新,所以如果在更新之后的一段时间内工作者之间会有不一致的情况,这没有任何问题。

  • 另一方面,它应该在服务器故障的情况下保持不变(定期备份就足够了)。
  • 它会经常被访问,所以它应该尽可能快 - 最好留在内存中。
  • 另外,它只是其中一种,所以我真的不想用专用的数据库模型来扩充应用程序。



类似的东西在Node.js中非常简单,但我找不到在Rails中实现这一点的单一方法。我应该怎么做?

编辑

感谢迄今为止的答案,但是当他们鼓舞人心的时候,我认为我应该强调他们都缺少的一个关键功能。 该变量应该可在应用程序内编辑并保持持久性。尽管可以编辑变量,但在服务器重新启动的情况下,我将恢复为默认值 - 这很糟糕。

解决方案

好的,这就是我所做的。我没有将这个值赋给一个初始化器,而是创建了一个处理它的简单类。变量本身存储在一个预定义的文件中。除了在初始化时读取文件之外,该类在更改值时更新文件,并且还定期重新读取该文件以保持工作人员之间的一致性。我还提供了一些基本的JSON处理和备份功能,以使生活更轻松。



对于任何感兴趣的人,这里是重要的代码:

 类石榴

def初始化
@delay = 30.minutes
@path =db / pomegranate。 json
@valid = Time.now - 1
validate
end

def get(* p)
validate
p.inject (@data){| object,key |对象[键]如果对象}
结束

def set(* p,q,v)
hash = p.inject(@data){| object,key | object [key] || = {}}
hash [q] = v
end

def save
@valid = Time.now + @delay
File.open(@path,w){| f | f.write(@ data.to_json)}
end

private

def validate
if @valid< Time.now
@data = ActiveSupport :: JSON.decode(File.read(@path))rescue {}
@valid = Time.now + @delay
@valid = Time。现在 - 1如果@ data.empty?
end
end

end

$ pom = Pomegranate.new


Lets say that on top of my Rails app there is a bar with piece of text displayed - latest hot deal, scheduled downtime notfication, something like that. It's a single, on of a kind information that needs to be accessed on basically every request, and may be updated from time to time. What is the best way to achieve this?

What I'd like to do is some kind of permanent global variable (accessible from controllers).

  • It will be updated very rarely, so there's no problem if for some time after update there will be an inconsistency between workers.
  • On the other hand, it should be persistent in case of server fault (periodic backup is enough).
  • It will be accessed really often, so it should be as fast as possible - preferably stay in memory.
  • Also, it's only one of a kind, so I'd really prefer not to bloat the app with a dedicated database model.

Something like that is damn easy in Node.js for example, but I couldn't find a single way to achieve this in Rails. What shall I do?

EDIT

Thanks for the answers so far, but while they're inspiring, I think that I should stress out one key functionality that they're all missing. The variable should be editable inside the app and persistent. While it's possible to edit your variables, in case of server restart I'm back to the default - which is bad.

解决方案

Ok, so here's what I did. Instead of just putting the value to an initializer, I've made there a simple class that handles it. The variable itself is stored in a predefined file. Besides of reading the file upon the initialization, the class updates file when the value is changed, and also re-read the file periodically to maintain consistency across workers. I've also put there some basic JSON handling and backup functionality to make life easier.

For anyone interested, here's the important code:

class Pomegranate

  def initialize
    @delay = 30.minutes
    @path = "db/pomegranate.json"
    @valid = Time.now - 1
    validate
  end

  def get(*p)
    validate
    p.inject(@data) {|object,key| object[key] if object}
  end

  def set(*p, q, v)
    hash = p.inject(@data) {|object,key| object[key]||={}}
    hash[q] = v
  end

  def save
    @valid = Time.now + @delay
    File.open(@path,"w") {|f| f.write(@data.to_json)}
  end

  private

  def validate
    if @valid < Time.now
      @data = ActiveSupport::JSON.decode(File.read(@path)) rescue {}
      @valid = Time.now + @delay
      @valid = Time.now - 1 if @data.empty?
    end
  end

end

$pom = Pomegranate.new

这篇关于Rails中的永久变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆