# redis

***

**1. Caching**

```js
const redis = require('redis');
const client = redis.createClient();

client.setex('my-key', 300, 'my-value'); // Cache for 5 minutes
client.get('my-key', (err, reply) => {
  if (err) { /* handle error */ }
  console.log(reply); // 'my-value'
});
client.del('my-key'); // Delete the cache
```

**2. Rate Limiting**

```js
const redis = require('redis');
const client = redis.createClient();

client.incr('rate-limit:user-a', (err, reply) => {
  if (err) { /* handle error */ }
  if (reply > 10) { // Limit exceeded
    console.log('Rate limit exceeded.');
  }
});
```

**3. Queuing**

```js
const redis = require('redis');
const client = redis.createClient();

client.rpush('queue', 'message1', 'message2'); // Push messages to the queue
client.lpop('queue', (err, reply) => {
  if (err) { /* handle error */ }
  console.log(reply); // 'message1'
});
```

**4. Pub/Sub**

```js
const redis = require('redis');
const client = redis.createClient();

client.publish('my-channel', 'hello world'); // Publish a message
client.subscribe('my-channel', (err, channel) => {
  if (err) { /* handle error */ }
  client.on('message', (ch, message) => {
    console.log(message); // 'hello world'
  });
});
```

**5. Session Management**

```js
const redis = require('redis');
const client = redis.createClient();
const express = require('express');
const app = express();

app.use(express.session({
  store: new RedisStore({ client: client }),
  secret: 'my-secret'
}));
```

**6. Geospatial Queries**

```js
const redis = require('redis');
const client = redis.createClient();

client.geoadd('places', [12.3, 45.6, 'Rome'], [34.5, 78.9, 'Paris']);
client.georadius('places', 12.3, 45.6, 100, 'km', (err, reply) => {
  if (err) { /* handle error */ }
  console.log(reply); // [ 'Rome', 'Paris' ]
});
```

**7. HyperLogLog**

```js
const redis = require('redis');
const client = redis.createClient();

client.pfadd('unique-users', 'user1', 'user2', 'user3');
client.pfcount('unique-users', (err, reply) => {
  if (err) { /* handle error */ }
  console.log(reply); // 3
});
```

**8. Time Series**

```js
const redis = require('redis');
const client = redis.createClient();

client.ts.add('temperature', new Date(), 25.3); // Add a data point for temperature
client.ts.get('temperature', new Date(), '1 hour ago', (err, reply) => {
  if (err) { /* handle error */ }
  console.log(reply); // [ { timestamp: '2022-08-24T09:30:00Z', value: '25.3' } ]
});
```

**9. Graph**

```js
const redis = require('redis');
const client = redis.createClient();

client.graph.query('my-graph', `
  MATCH (a)-[r]-(b)
  RETURN a, r, b
`, (err, reply) => {
  if (err) { /* handle error */ }
  console.log(reply); // [ { a: 'node1', r: { id: 'rel1', label: 'knows' }, b: 'node2' } ]
});
```

**10. Search**

```js
const redis = require('redis');
const client = redis.createClient();

client.ft.create('my-index', {
  name: {
    type: 'text'
  },
  description: {
    type: 'text'
  }
}, (err, reply) => {
  if (err) { /* handle error */ }
  console.log(reply); // OK
});
```

**11. Aggregate Queries**

```js
const redis = require('redis');
const client = redis.createClient();

client.xread('STREAMS', 'my-stream', '>', (err, reply) => {
  if (err) { /* handle error */ }
  console.log(reply); // [ { id: '1', body: '{"name":"John","age":30}' } ]
});
```

**12. Multi-Database**

```js
const redis = require('redis');
const client = redis.createClient({
  database: 1
});

client.select(0, (err, reply) => {
  if (err) { /* handle error */ }
  console.log(reply); // OK
});
```

**13. Cluster**

```js
const redis = require('redis');
const client = redis.createClient({
  cluster: {
    nodes: [
      { host: '127.0.0.1', port: 6379 },
      { host: '127.0.0.1', port: 6380 }
    ]
  }
});
```

**14. Sentinel**

```js
const redis = require('redis');
const client = redis.createClient({
  sentinel: {
    sentinels: [
      { host: '127.0.0.1', port: 26379 }
    ],
    name: 'my-master'
  }
});
```

**15. Pub/Sub with Pattern Matching**

```js
const redis = require('redis');
const client = redis.createClient();

client.psubscribe('my-pattern-*', (err, reply) => {
  if (err) { /* handle error */ }
  console.log(reply); // 'subscribe'
});
```

**16. Lua Scripting**

```js
const redis = require('redis');
const client = redis.createClient();

client.eval('return redis.call("get", KEYS[1])', 1, 'my-key', (err, reply) => {
  if (err) { /* handle error */ }
  console.log(reply); // 'my-value'
});
```

**17. Slowlog**

```js
const redis = require('redis');
const client = redis.createClient();

client.slowlog('get', (err, reply) => {
  if (err) { /* handle error */ }
  console.log(reply); // [ { id: 1, time: 23, command: 'GET my-key' } ]
});
```

**18. Config**

```js
const redis = require('redis');
const client = redis.createClient();

client.config('get', 'maxmemory', (err, reply) => {
  if (err) { /* handle error */ }
  console.log(reply); // '256mb'
});
```

**19. Monitor**

```js
const redis = require('redis');
const client = redis.createClient();

client.monitor((err, reply) => {
  if (err) { /* handle error */ }
  console.log(reply); // 'OK'
});
```

**20. Pipeline**

```js
const redis = require('redis');
const client = redis.createClient();

client.pipeline()
  .get('my-key')
  .set('my-other-key', 'my-value')
  .expire('my-other-key', 300)
  .exec((err, replies) => {
    if (err) { /* handle error */ }
    console.log(replies); // [ 'my-value', 'OK' ]
  });
```

**21. Multi**

```js
const redis = require('redis');
const client = redis.createClient();

client.multi()
  .get('my-key')
  .set('my-other-key', 'my-value')
  .expire('my-other-key', 300)
  .exec((err, replies) => {
    if (err) { /* handle error */ }
    console.log(replies); // [ 'my-value', 'OK' ]
  });
```

**22. Watch**

```js
const redis = require('redis');
const client = redis.createClient();

client.watch('my-key', (err, reply) => {
  if (err) { /* handle error */ }
  console.log(reply); // 'OK'
});
```

**23. Sort**

```js
const redis = require('redis');
const client = redis.createClient();

client.sort('my-list', 'DESC', 'ALPHA', (err, reply) => {
  if (err) { /* handle error */ }
  console.log(reply); // [ 'item3', 'item2', 'item1' ]
});
```

**24. Scan**

```js
const redis = require('redis');
const client = redis.createClient();

client.scan(0, 'MATCH', 'my-pattern*', 'COUNT', 10, (err, replies) => {
  if (err) { /* handle error */ }
  console.log(replies); // [ 'cursor', [ 'key1', 'key2', ... ] ]
});
```

**25. TTL**

```js
const redis = require('redis');
const client = redis.createClient();

client.ttl('my-key', (err, reply) => {
  if (err) { /* handle error */ }
  console.log(reply); // -1 (no expiration) or positive number (in seconds)
});
```
