DoneJS StealJS jQuery ++ FuncUnit DocumentJS
3.0.0
2.3.27

 

  • Github
  • Twitter
  • Chat
  • Forum
  • Guides
  • Core
    • can-component
    • can-compute
    • can-connect
      • behaviors
        • ./base/
        • ./cache-requests/
          • data interface
            • getListData
          • algebra
            • getDiff
            • getUnion
        • ./can/map/
        • ./can/ref/
        • ./constructor/callbacks-once/
        • ./constructor/
        • ./constructor/store/
        • ./data/callbacks/
        • ./data/callbacks-cache/
        • ./data/combine-requests/
        • ./data/localstorage-cache/
        • ./data/memory-cache/
        • ./data/parse/
        • ./data/url/
        • ./data/worker/
        • ./fall-through-cache/
        • ./real-time/
      • modules
        • ./can/base-map/
        • ./can/model/
        • ./can/super-map/
        • ./can/tag/
        • ./helpers/weak-reference-map
      • data types
        • DataInterface
        • Instance
        • InstanceInterface
        • List
        • ListData
    • can-define
    • can-define/list/list
    • can-define/map/map
    • can-route
    • can-route-pushstate
    • can-set
    • can-stache
    • can-stache/helpers/route
    • can-stache-bindings
  • Ecosystem
  • Infrastructure
  • Legacy
  • Bitovi
    • Bitovi.com
    • Blog
    • Consulting
    • Training
    • Open Source
  • Chat
  • Forum
  • Star
  • Follow @canjs
  • CanJS
  • /
  • Core
  • /
  • can-connect
  • /
  • ./cache-requests/
  • / On this page
    • can-connect/cache-requests/cache-requests

      module

      Caches reponse data and uses it to prevent future requests or make future requests smaller.

      • source

      cacheRequests( baseConnection )

      Overwrites getListData to use set logic to determine which data is already in cacheConnection or needs to be loaded from the base connection.

      It then gets data from the cache and/or the base connection, merges it, and returns it.

      Use

      Use cache-requests in combination with a cache like can-connect/data/memory-cache/memory-cache or can-connect/data/localstorage-cache/localstorage-cache. For example,

      var cacheConnection = connect([
        require("can-connect/data/memory-cache/memory-cache")
      ],{});
      
      var todoConnection = connect([
        require("can-connect/data/url/url"),
        require("can-connect/cache-requests/cache-requests")
      ],{
        cacheConnection: cacheConnection,
        url: "/todos"
      })
      

      This will make it so response data is cached in memory. For example, if today's todos are loaded:

      todoConnection.getListData({due: "today"})
      

      And later, a subset of those todos are loaded:

      todoConnection.getListData({due: "today", status: "critical"})
      

      The original request's data will be used.

      Using Algebra

      cache-requests can also "fill in" the data the cache is mising if you provide it the necessary set algebra.

      For example, if you requested paginated data like:

      todoConnection.getListData({start: 1, end: 10})
      

      And then later requested:

      todoConnection.getListData({start: 1, end: 20})
      

      ... with the appropriate configuration, cache-requests will only request {start: 11, end: 20}. That configuration looks like:

      var algebra = new set.Algebra( set.comparators.range("start","end") );
      
      var cacheConnection = connect([
        require("can-connect/data/memory-cache/memory-cache")
      ],{algebra: algebra});
      
      var todoConnection = connect([
        require("can-connect/data/url/url"),
        require("can-connect/cache-requests/cache-requests")
      ],{
        cacheConnection: cacheConnection,
        url: "/todos",
        algebra: algebra
      })
      

      Notice that cacheConnections often share many of the same options as the primary connection.

      CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 3.0.0.