You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: caching_writer.rb
    path of file: ./repos/beats/lib/wavefile
    
    the code of the file until where you have to start completion: module WaveFile
  # Implementation of Writer that caches the raw wave data for each buffer that it has written.
 
    
    def write(buffer)
      packed_buffer_data = {}

      key = buffer.hash
      if @buffer_cache.member?(key)
        packed_buffer_data = @buffer_cache[key]
      else
        samples = buffer.convert(@format).samples

        if @format.channels > 1
          data = samples.flatten.pack(@pack_code)
        else
          # Flattening an already flat array is a waste of time.
          data = samples.pack(@pack_code)
        end