title: How to implement a Persistent Cache in Siesta with a structured model layer
body: <p>I'm using (and loving) Siesta to communicate with a REST web service in my Swift App. I have implemented a series of ResponseTransformers to map the API call responses to model classes so that the Siesta Resources are automatically parsed into object instances. This all works great.</p>
  
  <p>I now want to implement a Siesta PersistantCache object to support an offline mode by having Siesta cache these objects to disk (rather than in memory) by storing them in Realm. I am not sure how to do this because the documentation says (about the EntityCache.writeEntity function):</p>
  
  <blockquote>
    <p>This method can — and should — examine the entity’s content and/or headers and ignore it if it is not encodable. While they can apply <em>type-based</em> rules, however, cache implementations should not apply <em>resource-based</em> or <em>url-based</em> rules; use <code>Resource.configure(...)</code> to select which resources are cached and by whom.</p>
  </blockquote>
  
  <p>In an attempt to conform to this guideline, I have created a specific PersistentCache object for each Resource type based on URL Pattern matching during Service Configuration:</p>
  
  <pre><code>class _GFSFAPI: Service {
      private init() {
          configure("/Challenge/*") { $0.config.persistentCache = SiestaRealmChallengeCache() }
      }
  </code></pre>
  
  <p>However, since the EntityCache protocol methods only include a reference to the Entity (which exposes raw content but not the typed objects), I don't see how I can call the realm write methods during the call to EntityCache.writeEntity or how to pull the objects out of Realm during EntityCache.readEntity. </p>
  
  <p>Any suggestions about how to approach this would be greatly appreciated. </p>