Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a cache key prefix option #328

Open
phombal opened this issue Aug 27, 2024 · 1 comment
Open

Adding a cache key prefix option #328

phombal opened this issue Aug 27, 2024 · 1 comment

Comments

@phombal
Copy link

phombal commented Aug 27, 2024

Hi! I am working with a cache where it is required for a prefix to be added to the cache key before keying into the cache. However, when I begin the cache key with this prefix, it seems like there is a url that is being prepended to the entirety of the cache key, which prevents the entry from keying into the cache. This feature seems to be implemented here: https://github.com/MarcGiffing/bucket4j-spring-boot-starter/issues/19. I was wondering whether there is any way that we can add functionality for a prefix to prepend the entirety of the key (url + specified key) or restructure the key as a whole to be able to add a prefix. Thank you!

@MarcGiffing
Copy link
Owner

I haven't tried it but maybe you can try something like this:

@Service
@Primary
public class CustomRateLimitService extends RateLimitService {

    private final ExpressionService expressionService;
    
    public CustomRateLimitService(ExpressionService expressionService) {
        super(expressionService);
        this.expressionService = expressionService;
    }

    @Override
    public <R> KeyFilter<R> getKeyFilter(String url, RateLimit rateLimit) {
        return expressionParams -> {
            return expressionService.parseString(rateLimit.getCacheKey(), expressionParams);
        };
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants