Class: SlidingStats::Persist

Inherits:
Object
  • Object
show all
Defined in:
lib/sliding-stats/persist.rb

Overview

This class provides basic persistence for SlidingStats To use it, simply add add :persist => [number of requests between saves] to the SlidingStats::Window options, or pass a different persistence class.

Instance Method Summary collapse

Constructor Details

#initialize(every = 10, path = "/var/tmp/slidingstats") ⇒ Persist

Returns a new instance of Persist.



10
11
12
13
14
# File 'lib/sliding-stats/persist.rb', line 10

def initialize every = 10,path="/var/tmp/slidingstats"
  @every = every
  @num = 0
  @path = path
end

Instance Method Details

#loadObject



16
17
18
19
20
21
22
# File 'lib/sliding-stats/persist.rb', line 16

def load
  begin
    Marshal.load(File.read(@path))
  rescue
    []
  end
end

#save(requests) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/sliding-stats/persist.rb', line 24

def save requests
  @num += 1
  if (@num % @every) == 0
    File.open(@path,"w") do |f| 
      f.write(Marshal.dump(requests)) 
    end
  end
end