Skip to content

Commit

Permalink
Adds experimental operator cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipe Zimmerle committed Nov 6, 2015
1 parent c204f1f commit 3266969
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/rule.cc
Expand Up @@ -257,9 +257,13 @@ bool Rule::evaluate(Assay *assay) {
return evaluateActions(assay);
}

#ifndef NO_LOGS
std::string eparam = MacroExpansion::expand(this->op->param, assay);
std::string cache_key = eparam + this->op->op + Variable::to_s(variables) + std::to_string(rule_id);
if (RuleInstantCache::getInstance().count(cache_key) > 0) {
return 0;
}

#ifndef NO_LOGS
if (this->op->param != eparam) {
eparam = "\"" + eparam + "\" Was: \"" + this->op->param + "\"";
} else {
Expand Down Expand Up @@ -490,6 +494,11 @@ bool Rule::evaluate(Assay *assay) {
}
//delete e;
}

if (ret == 0) {
RuleInstantCache::getInstance().cache(cache_key);
}

return ret;
}

Expand Down
18 changes: 18 additions & 0 deletions src/rule.h
Expand Up @@ -29,6 +29,24 @@

namespace ModSecurity {

class RuleInstantCache : public std::unordered_map<std::string, int> {
public:
static RuleInstantCache& getInstance() {
static RuleInstantCache instance;
return instance;
}

void cache(const std::string& value) {
emplace(value, 1);
if (size() > 1500) {
erase(begin());
}
}
private:
RuleInstantCache() {};
};


class Rule {
public:
Rule(operators::Operator *_op,
Expand Down

0 comments on commit 3266969

Please sign in to comment.