Class: FileSyncedQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/file_synced_queue.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, max_limit = 1000) ⇒ FileSyncedQueue

Returns a new instance of FileSyncedQueue.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/file_synced_queue.rb', line 7

def initialize(path,max_limit=1000)
  @save_path = path
  unless File.exists? path and File.file? path then
    raise "ファイルじゃない。"  if File.exists? path and not File.file? path
  end
  @m = Mutex.new
  at_exit{
    self.save
  }
  @q =self.load                  if     File.exists? path
  @q = SizedQueue.new(max_limit) unless File.exists? path
  self.save 
end

Instance Method Details

#loadObject



20
21
22
# File 'lib/file_synced_queue.rb', line 20

def load
  @q = Marshal.load open(@save_path).read
end

#popObject Also known as: shift, deq



35
36
37
38
39
40
41
# File 'lib/file_synced_queue.rb', line 35

def pop()
  val = @q.pop
  @m.synchronize{
    self.save    
  }
  val
end

#push(val) ⇒ Object Also known as: enq, <<



29
30
31
32
33
34
# File 'lib/file_synced_queue.rb', line 29

def push(val)
  @q.push val
  @m.synchronize{
    self.save    
  }
end

#saveObject



23
24
25
26
27
28
# File 'lib/file_synced_queue.rb', line 23

def save
  f = open(@save_path,"w")
  Marshal.dump(@q,f)
  f.flush
  f.close
end