Commit c5a93bdc396bc0c806d0ddb3086973f19c5bd44e

Authored by Adhidarma Hadiwinoto
1 parent a2a0be1274
Exists in master

Optimasi redis history

Showing 1 changed file with 8 additions and 3 deletions Side-by-side Diff

1 1 'use strict';
2 2  
  3 +const MAX_HISTORY = 1000;
  4 +
3 5 const redis = require('redis');
4 6 const CircularBuffer = require("circular-buffer");
5 7  
... ... @@ -7,13 +9,13 @@ const config = require('komodo-sdk/config');
7 9 const logger = require('komodo-sdk/logger');
8 10  
9 11 const redisClient = redis.createClient(config.redis || { host: '127.0.0.1' });
10   -const history = new CircularBuffer(1000);
  12 +const history = new CircularBuffer(MAX_HISTORY);
11 13  
12 14 const REDIS_KEYWORD = `SHAKIR_SMS_HISTORY_${config.name||'SMS'}`;
13 15  
14 16 function fetchFromRedis() {
15 17 redisClient.LRANGE(REDIS_KEYWORD, 0, -1, (err, reply) => {
16   - reply.forEach((el) => {
  18 + reply.reverse().forEach((el) => {
17 19 try {
18 20 history.push(JSON.parse(el));
19 21 } catch (e) {
... ... @@ -26,7 +28,10 @@ function fetchFromRedis() {
26 28 fetchFromRedis();
27 29  
28 30 function push(item) {
29   - redisClient.LPUSH(REDIS_KEYWORD, JSON.stringify(item));
  31 + redisClient.LPUSH(REDIS_KEYWORD, JSON.stringify(item), () => {
  32 + redisClient.LTRIM(REDIS_KEYWORD, 0, MAX_HISTORY);
  33 + });
  34 +
30 35 history.push(item);
31 36 }
32 37