move the logic of checking stream key duplication into the function of format_timeseries

I found a bug where if we found a existing key and then flush the buffer, the newly found duplicate stream key is not added into the stream key cache tree, but it's in the send buffer, which will results in duplication error next time we send the request.

key1: ts 1 -> cache{key1}, buffer {key1}
key2: ts 1 -> cache{key1,key2} buffer {key1, key2}
key1: ts 2 
  -> found key1 in cache and flush, -> cache{}, buffer{}
 -> add key1 into buffer using format_timeseries function -> buffer{key1}

key2: ts2 cache{key2}, buffer{key1, key2}
key1: ts3 cache{key1, key2} buffer{key1, key2, key1} //duplicate keys