lib/alephant/sequencer/sequencer.rb in alephant-sequencer-2.0.1 vs lib/alephant/sequencer/sequencer.rb in alephant-sequencer-3.0.0
- old
+ new
@@ -1,36 +1,39 @@
-require 'jsonpath'
-require 'alephant/logger'
+require "jsonpath"
+require "alephant/logger"
module Alephant
module Sequencer
class Sequencer
include Logger
- attr_reader :ident, :jsonpath, :keep_all
+ attr_reader :ident, :jsonpath, :keep_all, :cache
- def initialize(sequence_table, id, sequence_path, keep_all = true)
+ def initialize(sequence_table, opts = {})
@sequence_table = sequence_table
- @keep_all = keep_all
+ @cache = opts[:cache]
+ @keep_all = opts[:keep_all]
+ @ident = opts[:id]
@exists = exists?
- @jsonpath = sequence_path
- @ident = id
+ @jsonpath = opts[:jsonpath]
logger.info(
"event" => "SequencerInitialized",
"sequenceTable" => sequence_table,
- "jsonPath" => sequence_path,
- "id" => id,
+ "jsonPath" => @jsonpath,
+ "id" => @ident,
"method" => "#{self.class}#initialize"
)
end
def sequential?(msg)
(get_last_seen || 0) < Sequencer.sequence_id_from(msg, jsonpath)
end
def exists?
- @exists || @sequence_table.sequence_exists(ident)
+ @exists || cache.get(ident) do
+ @sequence_table.sequence_exists(ident)
+ end
end
def validate(msg, &block)
last_seen_id = get_last_seen
sequential = ((last_seen_id || 0) < Sequencer.sequence_id_from(msg, jsonpath))
@@ -73,10 +76,12 @@
(exists? ? last_seen_check : nil)
)
end
def get_last_seen(key = ident)
- @sequence_table.sequence_for(key)
+ cache.get(key) do
+ @sequence_table.sequence_for(key)
+ end
end
def self.sequence_id_from(msg, path)
JsonPath.on(msg.body, path).first.to_i
end