Skip to content

Commit

Permalink
Cache JSON generation for individual messages
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitpaulk committed Jan 5, 2018
1 parent fbf5d34 commit c65d7a4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
11 changes: 5 additions & 6 deletions lib/segment/analytics/message.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
require 'forwardable'

require 'segment/analytics/defaults'

module Segment
class Analytics
# Represents a message to be sent to the API
class Message
extend Forwardable

def initialize(hash)
@hash = hash
end
Expand All @@ -16,8 +12,11 @@ def too_big?
to_json.bytesize > Defaults::Message::MAX_BYTES
end

def_delegators :@hash, :to_json # TODO: Cache and reuse
def_delegators :@hash, :[]
# Since the hash is expected to not be modified (set at initialization),
# the JSON version can be cached after the first computation.
def to_json(*args)
@json ||= @hash.to_json(*args)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/segment/analytics/message_batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def <<(message)
end
end

def_delegators :@messages, :to_json # TODO: Cache and reuse
def_delegators :@messages, :to_json
def_delegators :@messages, :clear
def_delegators :@messages, :empty?
def_delegators :@messages, :length
Expand Down

0 comments on commit c65d7a4

Please # to comment.