parse-cache
Parse SDK caching for cloud code or browser that actually works.
About
A Parse caching module that works exactly how you would expect it to, with the latest version of Parse SDK. It can be used on cloud code (memory or redis storages) or client Browser code (memory storage only).
Important:
This module was tested using parse-server 2.8.4 and Parse SDK 2.1. There is no warranty using it on versions before these ones.
Install:
You can use npm to install it.
npm install --save parse-cache
You can use it on cloud code, so you need to put it in your package.json
and deploy it on your server.
"dependencies": "parse-cache": "*"
You can also use it with Parse Javascript SDK on a single page app.
Usage
const Parse = ; // or require('parse') if you are on a Browserconst parseCache = ; // choose your storage... // memory storage usage:; // {engine: 'memory', count: 1000} are default values and are optional // redis storage usage:; const RecordObject = ParseObject;const query = RecordObject; // or const query = new Parse.Query('Record'); query // The number of seconds to cache the query. Defaults to 60 seconds. ; // you can use find, first, count, countDocuments, estimatedDocumentCount, aggregate, each, get or distinct // on a cloud code with 10s ttlParseCloud; // calling your cloud codecurl -X POST -H "X-Parse-Application-Id: your_app_id" -H "X-Parse-REST-API-Key: your_rest_api_key" -H "Content-Type: application/json" -d "{}" https://parseapi.back4app.com/functions/test // the result should be"result":"fromCache":false // then run the curl again and..."result":"fromCache":true // then run again after 10s and..."result":"fromCache":false
You can also pass a custom key into the .cache()
method, which you can then use later to clear the cached content.
query // The number of seconds to cache the query. Defaults to 60 seconds. ;
To use it on frontend with Parse Javascript SDK just call it using parseCache
:
Insert .cache()
into the queries before find, first, count, countDocuments, estimatedDocumentCount, aggregate, each, get or distinct
if you want to cache, and they will be cached. Works with select
, ascending
, descending
, and anything else that will modify the results of a query.
Clearing the cache
If you want to clear the cache for a specific query, you must specify the cache key yourself:
query ; parseCache; // or using async/await { await parseCache;}
If you call parseCache.clearCache(null, cb) or await parseCache.clearCache()
without passing a cache key as the first parameter, the entire cache will be cleared for all queries.
Test
npm test