Working with big caches


In Database caching I showed how to use a cacheHandler to speed things up and avoid using up service quotas. In some cases you may hit the maximum cache size, which is 100k. The cache handler can take care of that automatically. There is no special action needed on your part. 


Here's how it works. 


If a put cache operation detects the data will break the cacheservice quota, it splits the data into chunks and writes a number of cache entries. The main cache item's key will end up pointing to a list of chunks that need to be put back together again. 
      

split it up...

     
// it will automatically split cache into chunks, if it's going to be too big
      if (t.length > MAXCACHECHUNK) {
        
        // the main cache entry will just contain pointers to the pieces
        var chunkKeys = [],chunk;
        for (var x = 0 ; x < t.length  ; x+= chunk.length) {
          chunk = t.slice (x,x+MAXCACHECHUNK);
          
          try {
            // want to make sure the pieces last longer than the master, so make them slightly longer expiry
            var k = s+x.toString(36);
            chunkKeys.push (k);
            cache.put ( k,chunk,expiry * 1.2);
           
          }
          catch (err) {
            // just silently fail - there'll be no caching this one
            return '';
          }
        }
        // now we need to store pointers to chunks
        t = JSON.stringify ({cacheHandlerChunks:chunkKeys});
      }
      
      // write to cache, either the pointers to the pieces, or the whole thing
      try {
        cache.put ( s,t,expiry);
        return s;
      }
      catch(err) {
        // for now, just doesn't write to cache.
        return '';
      }
    }

put it back together again...


        if (ob.hasOwnProperty('cacheHandlerChunks')) {
          
          // need to unwind the chunks - the cache entry keys for the pieces will be in the main entry
          var chunks = [];
          
          // get each chunk , and piece it back together again
          // if any failure, then we just return null as if no cache entries were found
          for (var i=0; i < ob.cacheHandlerChunks.length ;i++) {
            try {
              var t = cache.get(ob.cacheHandlerChunks[i]); 
              if (!t) return null;
              chunks.push(t);
            }
            catch(err) {
              return null;
            }
          }
          // if we get this far then we've managed to put cache together again
          var ob = JSON.parse(chunks.join(''));
        }

 

Here's the full code of the cache Handler









Comments