Skip to content

Commit

Permalink
Append filter url to cache key #19
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcGiffing committed Dec 5, 2017
1 parent 39ac613 commit 123e092
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public FilterConfiguration buildFilterConfig(Bucket4JConfiguration config, Cache
}

if(!skipRateLimit) {
String key = getKeyFilter(rl, expressionParser, beanFactory).key(servletRequest);
String key = getKeyFilter(filterConfig.getUrl(), rl, expressionParser, beanFactory).key(servletRequest);
Bucket bucket = buckets.getProxy(key, () -> configBuilderToUse.buildConfiguration());

ConsumptionProbe probe = bucket.tryConsumeAndReturnRemaining(1);
Expand All @@ -114,12 +114,13 @@ public FilterConfiguration buildFilterConfig(Bucket4JConfiguration config, Cache
* Creates the key filter lambda which is responsible to decide how the rate limit will be performed. The key
* is the unique identifier like an IP address or a username.
*
* @param url is used to generated a unique cache key
* @param rateLimit the {@link RateLimit} configuration which holds the skip condition string
* @param expressionParser is used to evaluate the expression if the filter key type is EXPRESSION.
* @param beanFactory used to get full access to all java beans in the SpEl
* @return should not been null. If no filter key type is matching a plain 1 is returned so that all requests uses the same key.
*/
public KeyFilter getKeyFilter(RateLimit rateLimit, ExpressionParser expressionParser, BeanFactory beanFactory) {
public KeyFilter getKeyFilter(String url, RateLimit rateLimit, ExpressionParser expressionParser, BeanFactory beanFactory) {
switch(rateLimit.getFilterKeyType()) {
case IP:
return (request) -> request.getRemoteAddr();
Expand All @@ -134,7 +135,7 @@ public KeyFilter getKeyFilter(RateLimit rateLimit, ExpressionParser expressionPa
//TODO performance problem - how can the request object reused in the expression without setting it as a rootObject
Expression expr = expressionParser.parseExpression(rateLimit.getExpression());
final String value = expr.getValue(context, request, String.class);
return value;
return url + "-" + value;
};

}
Expand Down

0 comments on commit 123e092

Please sign in to comment.