- Part 1:
Hello, today I want to tell you about the parse-cache-memory package. This package is a tool that you can use to increase performance by caching the results of Parse Server queries in memory. With this package, you can run frequently repeated and unchanged queries faster and put less load on Parse Server. To learn how this package works and how to use it, keep reading.
- Part 2:
To use the parse-cache-memory package, you first need to install it with npm. You can do this with the npm install parse-cache-memory
command. Then you need to require this package along with Parse SDK.
const Parse = require('parse/node');
require('parse-cache-memory');
//or
new (require('parse-cache-memory'))({
max: 5000, // maximum number of items that can be stored for each class (className)
maxSize: 50000, // total maximum number of items that can be stored across all classes
ttl: 1000 * 60 * 5, // time-to-live for items in the cache, here set to 10 minutes
allowStale: false, // determines whether items can be used after their ttl has expired
updateAgeOnGet: false, // determines whether an item's age is updated when it's retrieved using "get"
updateAgeOnHas: false, // determines whether an item's age is updated when it's checked using "has"
});
-
This way you can use this package with Parse SDK.
-
Part 3:
The parse-cache-memory package adds new methods to Parse queries. With these methods, the results of queries are automatically cached and retrieved from the cache when you run the same query again. These methods are: findCache
, firstCache
, countCache
, distinctCache
, aggregateCache
, eachCache
, eachBatchCache
, mapCache
, reduceCache
, filterCache
and subscribeCache
. You can use these methods instead of normal Parse queries. For example:
const query = new Parse.Query('GameScore');
query.equalTo('playerName', 'Dan Stemkoski');
const data = await query.findCache();
This way you can get the result of the query from the cache.😊
- Part 4:
The parse-cache-memory package also hooks the save
, saveAll
, destroy
and destroyAll
methods of Parse objects. With these methods, when you save or delete objects, the cache is cleared. This prevents old or corrupted data from remaining in the cache. You can use these methods instead of normal Parse objects. For example:
const gameScore = new Parse.Object('GameScore');
gameScore.set('score', 1337);
gameScore.set('playerName', 'Sean Plott');
gameScore.set('cheatMode', false);
await gameScore.save();
This way you can clear the cache after saving the object.
Some test results: