# Parse-cache-memory - A cache library customized for parse-server queries

**URL:** <https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367>\
**Category:** Parse Server 📡\
**Created:** [April 2, 2023, 11:16pm UTC](https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367 "2023-04-02T23:16:03Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rgunindi](https://community.parseplatform.org/user_avatar/community.parseplatform.org/rgunindi/32/1553_2.png) [@rgunindi](https://community.parseplatform.org/u/rgunindi)\
**Post date:** [April 2, 2023, 11:16pm UTC](https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367/1 "2023-04-02T23:16:03Z")

</div>

- 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.

```js
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:

```js
const query = new Parse.Query('GameScore');
query.equalTo('playerName', 'Dan Stemkoski');
const data = await query.findCache();

```

```auto
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:

```js
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:

![Before](https://parse-community-forum-uploads.s3.dualstack.us-east-1.amazonaws.com/original/2X/c/c00f2b7b3d19f2bb02acbd551ec06a8518b725c4.png)

---

<div class="post-metadata">

**Author:** ![andreisu](https://community.parseplatform.org/user_avatar/community.parseplatform.org/andreisu/32/1414_2.png) [@andreisu](https://community.parseplatform.org/u/andreisu)\
**Post date:** [April 3, 2023, 2:52am UTC](https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367/2 "2023-04-03T02:52:59Z")

</div>

Thank you!!! Just in time

---

<div class="post-metadata">

**Author:** ![dblythy](https://community.parseplatform.org/user_avatar/community.parseplatform.org/dblythy/32/231_2.png) [@dblythy](https://community.parseplatform.org/u/dblythy)\
**Post date:** [April 4, 2023, 1:26am UTC](https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367/3 "2023-04-04T01:26:57Z")

</div>

Is this for client side caching?

Also just a side note that if your queries are taking 4s to resolve, I would guess that there are more optimisations that could be made (such as indexing)

---

<div class="post-metadata">

**Author:** ![andreisu](https://community.parseplatform.org/user_avatar/community.parseplatform.org/andreisu/32/1414_2.png) [@andreisu](https://community.parseplatform.org/u/andreisu)\
**Post date:** [April 4, 2023, 8:53am UTC](https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367/4 "2023-04-04T08:53:30Z")

</div>

I use it server-side in my cloud code and it’s awesome. The execution time drops from 200ms to 1ms. One improvement that could be made is to allow tweaking the caching settings per function. For example:

```auto
const resultA = await queryA.firstCache({useMasterKey: true, ttl: 1000 * 60 * 5});

```

```auto
const resultB = await queryB.firstCache({useMasterKey: true, ttl: 2000 * 60 * 5});

```

As far as I know currently you can only set the caching parameters globally when you initiate the module.

---

<div class="post-metadata">

**Author:** ![rgunindi](https://community.parseplatform.org/user_avatar/community.parseplatform.org/rgunindi/32/1553_2.png) [@rgunindi](https://community.parseplatform.org/u/rgunindi)\
**Post date:** [April 4, 2023, 9:16am UTC](https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367/5 "2023-04-04T09:16:24Z")

</div>

To increase the query difficulty, a lot of data was used in the tests with complex aggregate. Therefore the first query time is long.

---

<div class="post-metadata">

**Author:** ![rgunindi](https://community.parseplatform.org/user_avatar/community.parseplatform.org/rgunindi/32/1553_2.png) [@rgunindi](https://community.parseplatform.org/u/rgunindi)\
**Post date:** [April 4, 2023, 9:19am UTC](https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367/6 "2023-04-04T09:19:34Z")

</div>

Thank you for testing the package and expressing your satisfaction.

I will consider your feedback, as you said it is defined globally

---

<div class="post-metadata">

**Author:** ![dblythy](https://community.parseplatform.org/user_avatar/community.parseplatform.org/dblythy/32/231_2.png) [@dblythy](https://community.parseplatform.org/u/dblythy)\
**Post date:** [April 4, 2023, 3:00pm UTC](https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367/7 "2023-04-04T15:00:19Z")

</div>

Does it respect ACLs/sessionToken when used server side? Sounds like an interesting package

---

<div class="post-metadata">

**Author:** ![rgunindi](https://community.parseplatform.org/user_avatar/community.parseplatform.org/rgunindi/32/1553_2.png) [@rgunindi](https://community.parseplatform.org/u/rgunindi)\
**Post date:** [April 4, 2023, 6:06pm UTC](https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367/8 "2023-04-04T18:06:48Z")

</div>

Yes it respect ACLs/sessionToken when used server side. You should try. I am waiting more usage for parse server followers.

---

<div class="post-metadata">

**Author:** ![sfraise](https://community.parseplatform.org/user_avatar/community.parseplatform.org/sfraise/32/741_2.png) [@sfraise](https://community.parseplatform.org/u/sfraise)\
**Post date:** [April 7, 2023, 8:29pm UTC](https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367/9 "2023-04-07T20:29:24Z")

</div>

Very interesting!

Thanks for releasing this.

---

<div class="post-metadata">

**Author:** ![rgunindi](https://community.parseplatform.org/user_avatar/community.parseplatform.org/rgunindi/32/1553_2.png) [@rgunindi](https://community.parseplatform.org/u/rgunindi)\
**Post date:** [April 8, 2023, 12:02am UTC](https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367/10 "2023-04-08T00:02:31Z")

</div>

Thank you, good use.

---

<div class="post-metadata">

**Author:** ![ATrcked](https://community.parseplatform.org/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@ATrcked](https://community.parseplatform.org/u/ATrcked)\
**Post date:** [April 13, 2023, 8:50pm UTC](https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367/11 "2023-04-13T20:50:01Z")

</div>

This is great. Thank you for contributing! Would be nice to be able to pass a flag so that it will “fall back” to the DB if the cache returns zero records.

---

<div class="post-metadata">

**Author:** ![rgunindi](https://community.parseplatform.org/user_avatar/community.parseplatform.org/rgunindi/32/1553_2.png) [@rgunindi](https://community.parseplatform.org/u/rgunindi)\
**Post date:** [April 13, 2023, 9:04pm UTC](https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367/12 "2023-04-13T21:04:14Z")

</div>

If there is no data in the cache, the Db is already queryed and cached again. 🙌

---

<div class="post-metadata">

**Author:** ![andreafalzetti](https://community.parseplatform.org/user_avatar/community.parseplatform.org/andreafalzetti/32/999_2.png) [@andreafalzetti](https://community.parseplatform.org/u/andreafalzetti)\
**Post date:** [April 13, 2023, 9:16pm UTC](https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367/13 "2023-04-13T21:16:54Z")

</div>

thanks for sharing your tool @rgunindi - I have many clients making requests so it’s hard to introduce it there. Is there a way to instrument parse-server running in Docker, to use this module?

---

<div class="post-metadata">

**Author:** ![rgunindi](https://community.parseplatform.org/user_avatar/community.parseplatform.org/rgunindi/32/1553_2.png) [@rgunindi](https://community.parseplatform.org/u/rgunindi)\
**Post date:** [April 13, 2023, 9:25pm UTC](https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367/14 "2023-04-13T21:25:17Z")

</div>

You can install it into your parse-server in docker.

---

<div class="post-metadata">

**Author:** ![andreafalzetti](https://community.parseplatform.org/user_avatar/community.parseplatform.org/andreafalzetti/32/999_2.png) [@andreafalzetti](https://community.parseplatform.org/u/andreafalzetti)\
**Post date:** [April 13, 2023, 9:39pm UTC](https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367/15 "2023-04-13T21:39:11Z")

</div>

Is it enough to require it on the backend to make it work? Or would I need to customize anything?

---

<div class="post-metadata">

**Author:** ![rgunindi](https://community.parseplatform.org/user_avatar/community.parseplatform.org/rgunindi/32/1553_2.png) [@rgunindi](https://community.parseplatform.org/u/rgunindi)\
**Post date:** [April 13, 2023, 9:41pm UTC](https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367/16 "2023-04-13T21:41:23Z")

</div>

It is sufficient to use the specified functions to apply the cache. You can use it with default settings.

---

<div class="post-metadata">

**Author:** ![andreafalzetti](https://community.parseplatform.org/user_avatar/community.parseplatform.org/andreafalzetti/32/999_2.png) [@andreafalzetti](https://community.parseplatform.org/u/andreafalzetti)\
**Post date:** [April 13, 2023, 9:45pm UTC](https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367/17 "2023-04-13T21:45:22Z")

</div>

Ok I was hoping for a drop in plugin, but how you’re suggesting to use those functions which doesn’t make sense for my use case. I run a vanilla parse server using docker and clients call it. How and where would I call the cache functions in the server?

---

<div class="post-metadata">

**Author:** ![rgunindi](https://community.parseplatform.org/user_avatar/community.parseplatform.org/rgunindi/32/1553_2.png) [@rgunindi](https://community.parseplatform.org/u/rgunindi)\
**Post date:** [April 13, 2023, 9:54pm UTC](https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367/18 "2023-04-13T21:54:48Z")

</div>

```
I have some questions for your setup, In your Parse Server, which directory are your cloud functions defined in?
When starting the Parse Server on Docker, do you specify the disk address for the cloud functions?
Can you share the full path to the file where you have defined your cloud functions?
```

---

<div class="post-metadata">

**Author:** ![andreafalzetti](https://community.parseplatform.org/user_avatar/community.parseplatform.org/andreafalzetti/32/999_2.png) [@andreafalzetti](https://community.parseplatform.org/u/andreafalzetti)\
**Post date:** [April 13, 2023, 10:10pm UTC](https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367/19 "2023-04-13T22:10:05Z")

</div>

Sure, I am using docker image: `parseplatform/parse-server:5.4.0-beta.1`

then I am copying my cloud functions:

`COPY ./provisioning/cloud-functions /parse-server/cloud/`

Then installing dependencies:

`RUN cd /parse-server && npm i @parse/s3-files-adapter@1.6.2 aws-sdk@2.1262.0 deep-object-diff@1.1.9`

Hope it helps, I’d love to use your module!

---

<div class="post-metadata">

**Author:** ![rgunindi](https://community.parseplatform.org/user_avatar/community.parseplatform.org/rgunindi/32/1553_2.png) [@rgunindi](https://community.parseplatform.org/u/rgunindi)\
**Post date:** [April 13, 2023, 10:33pm UTC](https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367/20 "2023-04-13T22:33:41Z")

</div>

I think you can do it this way.  
Before you should change the cloud function  
example: …query.find() - \> to …query.findCache() and do it other functions like this.  
Dont, forget! adding this line.  
into cloud function file or main.js file

```auto
new (require('parse-cache-memory'))({ ttl: 30 * 60 * 1000 }) // ex 30min

```

→ add cache package.  
RUN cd /parse-server && npm i @parse/s3-files-adapter@1.6.2 aws-sdk@2.1262.0 deep-object-diff@1.1.9 parse-cache-memory

[Next page](https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367.md?page=2)
