Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
# How to use ConfigCat with Redis | Sample application
# How to use ConfigCat with Redis

This is a companion repo the the blog article titled: **How to use ConfigCat with Redis**. The article walks you through understanding how Redis works and how you can use it alongside ConfigCat in a Node.js console application.
**Read the blog post here](https://configcat.com/blog/how-to-use-configcat-with-redis)**

A sample Node.js app demonstrating to integrate ConfigCat with a Redis cache. This is done by setting the `cache` property in the options passed to `getClient`.

## Build & Run

**1.** Clone the [GitHub repository](https://github.com/configcat-labs/configcat-with-redis-sample)

**2.** Run the following commands to install the required node packages:
**2.** Install the dependencies:

```sh
npm install
```

The `configcat-node` and `redis` npm packages will also be installed using this command. The `configcat-node` package will be used as a client to connect to and pull the feature flag status from your [ConfigCat dashboard](https://app.configcat.com). `redis` will be used similarly, and would connect to a local docker instance of Redis.

**3.** Using docker, pull the latest Redis docker image and run the container:

```sh
```bash
docker pull redis
```

```sh
```bash
docker run --name container-redis -p 6379:6379 -d redis
```

**4.** Start the node application with the following command:
**4** Add your [ConfigCat SDK key](https://app.configcat.com/sdkkey) to the `index.js` file.

**5.** Start the app:

```sh
npm start
```

You should see the following output logged to your terminal every 5 seconds:

```sh
```bash
> configcat-with-redis-sample@1.0.0 start
> node index.js

Connected to Redis
By default, ConfigCat’s auto-polling interval is 60 seconds. This means the cache is refreshed with the latest feature flag values from the ConfigCat Dashboard every 60 seconds. When you query a feature flag value between intervals, the result is served from the Redis cache instead of making a request to the Dashboard.
17:56:55 GMT-0300 (French Guiana Time) isMyAwesomeFeatureEnabled: false
17:57:00 GMT-0300 (French Guiana Time) isMyAwesomeFeatureEnabled: false
17:57:05 GMT-0300 (French Guiana Time) isMyAwesomeFeatureEnabled: false
Expand All @@ -52,7 +55,7 @@ Toggling on or off the feature flag within your [ConfigCat dashboard](https://ap
Useful links to technical resources.

- [Using a custom cache implementation](https://configcat.com/docs/sdk-reference/node/#using-custom-cache-implementation)
- [Official Redis website](https://redis.io/)
- [Redis documentation](https://redis.io/docs/latest/)

[**ConfigCat**](https://configcat.com) also supports many other frameworks and languages. Check out the full list of supported SDKs [here](https://configcat.com/docs/sdk-reference/overview/).

Expand Down
56 changes: 3 additions & 53 deletions configcat-redis-cache.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,5 @@
const util = require("util");
const redis = require("redis");
import { createClient } from "redis";

function configcatRedisCache(redisClientOptions) {

this.isRedisAvailable = false;
this.cacheClient = redis.createClient(redisClientOptions);

this.cacheClient.connect();

this.cacheClient.on("connect", () => {
console.log("Connected to Redis");
this.isRedisAvailable = true;
});

this.cacheClient.on("error", (error) => {
console.log(`Redis error: ${error}`);
this.isRedisAvailable = false;
});

this.lastCacheItems = {};
export class MyRedisCache {
constructor(redisOptions) {}
}

configcatRedisCache.prototype.get = async function (key) {
if (!this.isRedisAvailable) {
return this.lastCacheItems[key];
}

try {
const getAsync = util
.promisify(this.cacheClient.get)
.bind(this.cacheClient);

return JSON.parse(await getAsync(key));
} catch (error) {
console.error(`Cache read failed! \n ${error}`);
return this.lastCacheItems[key];
}
};

configcatRedisCache.prototype.set = async function (key, item) {
this.lastCacheItems[key] = item;

try {
const setAsync = util
.promisify(this.cacheClient.set)
.bind(this.cacheClient);

await setAsync(key, JSON.stringify(item));
} catch (error) {
console.error(`Cache write failed! \n ${error}`);
}
};

module.exports = configcatRedisCache;
20 changes: 7 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
const configcat = require("configcat-node");
const configcatRedisCache = require("./configcat-redis-cache");
import * as configcat from "@configcat/sdk";
import { MyRedisCache } from "./configcat-redis-cache.js";

const redisOptions = { host: "localhost", port: 6379 };
const redisOptions = { url: "redis://localhost:6379" };

const configCatClient = configcat.getClient("0yDbCLmNK0qIUakB2LFJDA/u0Z5j4oDjkuHKOVJkIo9Dw", configcat.PollingMode.AutoPoll,
{
cache: configcatRedisCache(redisOptions)
}
);
const myRedisCache = new MyRedisCache(redisOptions);

setInterval(() => {
configCatClient.getValueAsync("isMyAwesomeFeatureEnabled", false).then(value => {
console.log(`${new Date().toTimeString()} isMyAwesomeFeatureEnabled: ${value}`);
});
}, 5000);
async function main() {}

main();
Loading