我在初始化程序中将Redis实例化为:
uri = URI.parse(ENV["REDISTOGO_URL"] || "redis://localhost:6379/")
KEYS = %w(
first_attempt_success
first_attempt_failure
first_retry_success
first_retry_failure
)
unless $stats
$stats = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
KEYS.map { |k| $stats.set(k, 0) }
end
然后我继续在我的模型中使用它,如下所示:
class StatsCounter
KEYS = %w(
first_attempt_success
first_attempt_failure
first_retry_success
first_retry_failure
)
def self.stats
uri = URI.parse(ENV["REDISTOGO_URL"] || "redis://localhost:6379/")
unless $stats
$stats = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
KEYS.map { |k| $stats.set(k, 0) }
end
$stats
end
def self.get_stats
Hash[KEYS.collect { |k| [k, stats.get(k)] }]
end
end
但我遇到的问题是,每次重新启动服务器时 - (重新启动应用程序),Redis实例中保存的所有数据都会重置为零。 我知道Redis是一个内存数据库。 我该怎么做,以便即使应用程序重新启动,我的数据仍然存在? 感谢
答案 0 :(得分:0)