title: Cache for packaged NSDocument file format design
body: <p>I am designing a packaged-based NSDocument file format.  The document will contain lots of directories and files, so I want to save a cache, either as part of the document, or in the application's Caches directory.</p>
  
  <p>The cache would be a simple plist array.</p>
  
  <p>I want to design this so the document can be synced via Dropbox or iCloud.  So I'm up against one of the hard problems... cache invalidation.</p>
  
  <p>If I store the cache in the document package, if the document is edited on two machines at once, either one might overwrite the cache of the other, or they might need conflict resolution.</p>
  
  <p>If I store the cache in the external Caches directory, perhaps named based on the document name or file identifier, I need some way to detect that it's stale if the document was updated elsewhere (via sync or even just a newer version copied from an email).</p>
  
  <p>An external cache also has other issues, e.g. I thought about storing a UUID in the document and using that as the cache filename, but if the document is duplicated, the cache would be used by two documents, which is obviously wrong as soon as one is edited.</p>
  
  <p>It seems to me that the safest place for it is in the document package, but then it comes back to a possible conflict.</p>
  
  <p>I also wondered how to detect changes.  One overly-complex idea was to write a UUID-named file to a folder within the document every time it is saved, then if the number of files there changes, rebuild the cache.  (A simpler approach would be a change count in the document, but again that could result in a conflict if two machines increment the count and save at the same time, bearing in mind there could be a syncing delay.)</p>
  
  <p>It seems to me that safely syncing a document package isn't too an uncommon problem, so I'm hoping someone can offer some best practices recommendations?</p>