Skip to content

Instantly share code, notes, and snippets.

@thoughtspeed7
Created September 5, 2021 14:18
Show Gist options
  • Save thoughtspeed7/e65a5393824da92de9878caf0980ee76 to your computer and use it in GitHub Desktop.
Save thoughtspeed7/e65a5393824da92de9878caf0980ee76 to your computer and use it in GitHub Desktop.
Connect To Redis Memorystore From Cloud Functions, Cloud Run and App Engine Using Serverless VPC Access | Google Cloud
// om namah shivaya
// require scripts
const { promisify } = require('util');
const redis = require('redis');
const express = require('express');
startService();
async function startService() {
// replace 127.0.0.1 with your redis instance ip
const redisHost = '127.0.0.1';
const client = redis.createClient({
host: redisHost,
});
const incrAsync = promisify(client.incr).bind(client);
const app = express();
const port = process.env.PORT || 4000;
app.get('/', async (req, res) => {
res.send(`counter: ${await incrAsync('counter')}`);
});
app.listen(port, () => {
console.log(`cloud-run listening on port ${port}`);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment