This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const RedisConnectionHelper = require('./redisHelper') | |
const checkApiRate = (scriptSHA) => { | |
return async(req, res, next) => { | |
let redisInstance = await RedisConnectionHelper.getInstance(); | |
try { | |
let result = await redisInstance.EVALSHA(scriptSHA,{keys:[req.body.userId]}) | |
if(result == 'True') { | |
//Allow this request | |
next(); | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const RedisConnectionHelper = require('./redisHelper') | |
const checkApiRate = require('./CheckAPIRate') | |
const fs = require('fs') | |
const app = express(); | |
const port = 3000; | |
async function initializeApp() { | |
let redisInstance = await RedisConnectionHelper.getInstance(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local logchannel = "tokenbucketdebug" | |
-- This function will be used to log debug messages to redis pub/sub channel | |
local function logit(msg) | |
redis.pcall("PUBLISH", logchannel, msg) | |
end | |
local key = KEYS[1] | |
local logPrependText = "User: "..key | |
local availableToken = redis.call('GET', 'tokens_'..key) | |
if availableToken == false then |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
SUM <key> <startIndex> <endIndex> | |
*/ | |
int sumCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { | |
int replyIndex, replyLength; | |
long long value; | |
long long sum = 0; | |
RedisModuleCallReply *tempReply; | |
RedisModuleString *tempString; | |
RedisModule_Log(ctx,"debug","Inside Sum Function %d", argc); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { | |
// init function | |
if(RedisModule_Init(ctx, "stats", 1, REDISMODULE_APIVER_1) == REDISMODULE_ERR) { | |
printf("Error while module initialization"); | |
return REDISMODULE_ERR; | |
} | |
// Create commands | |
if(RedisModule_CreateCommand(ctx, "stats.total", sumCommand, "readonly", 0, 0, 0) == REDISMODULE_ERR) { | |
printf("Error while command initialization"); | |
return REDISMODULE_ERR; |