Skip to content

Commit

Permalink
Update RateLimiter.cfc
Browse files Browse the repository at this point in the history
use cache.getOrSet, a little bit of formatting
  • Loading branch information
GunnarLieb authored Jun 26, 2024
1 parent 6fd7639 commit 447398c
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions modules/contentbox/models/security/RateLimiter.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
component extends="coldbox.system.Interceptor" {

// DI
property name="settingService" inject="id:settingService@contentbox";
property name="securityService" inject="id:securityService@contentbox";
property name = "settingService" inject = "id:settingService@contentbox";
property name = "securityService" inject = "id:securityService@contentbox";
property name = "cachebox" inject = "Cachebox";

/**
* Limiter
* onRequestCapture
* fires before any event caching or processing
*/
function onRequestCapture( event, data, buffer ){
var allSettings = variables.settingService.getAllSettings();
Expand Down Expand Up @@ -50,11 +51,15 @@ component extends="coldbox.system.Interceptor" {
var cache = cachebox.getDefaultCache();
var cacheKey = 'limiter'&realIP;

// If first time visit, create record.
var targetData = cache.get( cacheKey );
if( isNull( targetData ) ){
cache.set( cacheKey, { attempts = 1, lastAttempt = now() });
return this;
var targetData = cache.getOrSet( cacheKey, function(){
return { attempts = 0, lastAttempt = now() }
} );

// on first visit no further processing
if( targetData.attempts == 0 ){
targetData.attempts++;
cache.set( cacheKey, targetData );
return this;
}

log.debug( "Limit data", targetData );
Expand Down

0 comments on commit 447398c

Please sign in to comment.