RedisCacheAdapter with parse server

Hi,

Could anyone provide a real world example about how RedisCacheAdapter works with parse server?
I see the RedisCacheAdapter configuration here:

But. do I need other configurations before Redis can really work?

Thanks

Also, after I set up redis by:

var RedisCacheAdapter = require('parse-server').RedisCacheAdapter;
var redisOptions = {url: 'YOUR REDIS URL HERE'}
var redisCache = new RedisCacheAdapter(redisOptions);

var api = new ParseServer({
  databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
  appId: process.env.APP_ID || 'APPLICATION_ID',
  masterKey: process.env.MASTER_KEY || 'MASTER_KEY',
  ...
  cacheAdapter: redisCache,
  ...
});

Does parse server automatically start working with redis? Thanks

Currently, the cache adapter will be automatically used for schema cache and session tokens (maybe I’m forgetting something else). I can also be used for live query.

Thanks. But, do I need to config some settings? Right now, your package.json does not even have redis dependency. So, I think this will not work out-of-box.

Yes. It has: parse-server/package.json at master · parse-community/parse-server · GitHub

Your code looks to be in the right direction. Did you try to run it?

@davimacedo Thanks. I just tried using this sample code: GitHub - parse-community/parse-server-example: Example server using Express and the parse-server module.

I added several lines to index.js and then I run the code using
“parse-server --appId cn67FArPjbz46E6CprmZjkbCXvo2PDRDgcneRoyp3 --masterKey ZCnscyZlrcnfeAj9ot7fr0UN5jdkk2cG89yQGPcec --databaseURI postgres://call:call@localhost:5432/call”

Here are what I changed in index.js

// Example express application adding the parse-server module to expose Parse
// compatible API routes.

const express = require('express');
const ParseServer = require('parse-server').ParseServer;
const path = require('path');
const args = process.argv || [];
const test = args.some(arg => arg.includes('jasmine'));

var RedisCacheAdapter = require('parse-server').RedisCacheAdapter;
var redisOptions = {url: 'http://localhost:6379'}
var redisCache = new RedisCacheAdapter(redisOptions);


const databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI;

if (!databaseUri) {
  console.log('DATABASE_URI not specified, falling back to localhost.');
}
const config = {
  databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
  cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
  appId: process.env.APP_ID || 'myAppId',
  masterKey: process.env.MASTER_KEY || '', //Add your master key here. Keep it secret!
  serverURL: process.env.SERVER_URL || 'http://localhost:1337/parse', // Don't forget to change to https if needed
  liveQuery: {
    classNames: ['Posts', 'Comments'], // List of classes to support for query subscriptions
  },
  cacheAdapter: redisCache,
};

However, it did not work. When I checked the port in my Macbook, there is no application listening at port 6379.

Do you know how to solve this problem?

Thanks

parse-server-example is an example express.js app with parse server mounted on it. So you can’t use parse cli to start it. You should use npm start instead.

Thanks for pointing out this.
I modified index.js to hard-code the parameters because my “npm start” cannot read parameters from the config file. Here is my code:

const express = require('express');
const ParseServer = require('parse-server').ParseServer;
const path = require('path');
const args = process.argv || [];
const test = args.some(arg => arg.includes('jasmine'));

var RedisCacheAdapter = require('parse-server').RedisCacheAdapter;
var redisOptions = {url: 'redis://localhost:6379'}
var redisCache = new RedisCacheAdapter(redisOptions);


//const databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI;
const databaseUri = 'postgres://call:call@localhost:5432/call';

if (!databaseUri) {
  console.log('DATABASE_URI not specified, falling back to localhost.');
}
const config = {
  databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
  cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
  appId: 'cn67FArPjbz46E6CprmZsbCXvo2PDRDgcneRoyp3',
  //appId: process.env.APP_ID || 'myAppId',
  masterKey: 'ZCnscyZlrcnfeAj9ot7fr0UN5jdkk2cG6yQGPcec',
  //masterKey: process.env.MASTER_KEY || '', //Add your master key here. Keep it secret!
  //serverURL: 'postgres://call:call@localhost:5432/call',
  serverURL: process.env.SERVER_URL || 'http://localhost:1337/parse', // Don't forget to change to https if needed
  liveQuery: {
    classNames: ['Posts', 'Comments'], // List of classes to support for query subscriptions
  },
  cacheAdapter: redisCache,
};

Then, here is the error:

(1to1call) Tom:Parse-Server-example-latest mariolzx$ npm start

> [email protected] start /private/var/www/parse_server_example/Parse-Server-example-latest
> node index.js

parse-server-example running on port 1337.
info: Parse LiveQuery Server starts running
/private/var/www/parse_server_example/Parse-Server-example-latest/node_modules/parse-server/lib/ParseServer.js:237
          throw err;
          ^

Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1088:14)
npm ERR! code ELIFECYCLE
npm ERR! errno 7
npm ERR! [email protected] start: `node index.js`
npm ERR! Exit status 7
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/mariolzx/.npm/_logs/2021-05-08T03_04_53_667Z-debug.log

It seems that redis server is not running. Do you know why? Thanks

To be more accurate, I feel those lines have caused the problem.

var RedisCacheAdapter = require('parse-server').RedisCacheAdapter;
var redisOptions = {url: 'redis://localhost:6379'}
var redisCache = new RedisCacheAdapter(redisOptions);

Here is the error message:

parse-server-example running on port 1337.
info: Parse LiveQuery Server starts running
/private/var/www/parse_server_example/Parse-Server-example-latest/node_modules/parse-server/lib/ParseServer.js:237
          throw err;
          ^

Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1088:14)
npm ERR! code ELIFECYCLE
npm ERR! errno 7
npm ERR! [email protected] start: `node index.js`
npm ERR! Exit status 7
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/mariolzx/.npm/_logs/2021-05-08T18_08_25_051Z-debug.log

Parse Server does not spin up a redis server for you. You need to spin it up by yourself.

The redis server and the parse server example code are finally running. How do I know the parse server actually uses the redis server? Is there a way to see the effect of the redis server? Thanks

You can connect to redis and check if there are keys created over there.