Http Cache - Creating custom config extension

« back to HTTP Cache Table of Contents

Extending cache config

@Component(configurationPolicy = ConfigurationPolicy.REQUIRE, service = HttpCacheConfigExtension.class)
@Designate(ocd = WeRetailHttpCacheConfigExtension.Config, factory = true)
public class WeRetailHttpCacheConfigExtension implements HttpCacheConfigExtension {

    //our own service
    private WeRetailUserService userService;

    //config name
    private String configName;

    private boolean allowAnonymous;

    @ObjectClassDefinition(name = "ACS AEM Commons - HTTP Cache - WeRetailHttpCacheConfigExtension.")
    public @interface Config {

        @AttributeDefinition(name = "Config Name")
        String configName() default EMPTY;

        boolean cacheAnonymous() default false;

    public boolean accepts(SlingHttpServletRequest request, HttpCacheConfig cacheConfig){

            //in this case we always will cache the request. Different variants will be handled by our keyfactory.
            return true;

        return userService.doesUserExist(request);

    public void activate(WeRetailHttpCacheConfigExtension.Config config){
        this.configName = config.configName();
        this.allowAnonymous = config.cacheAnonymous();

Now, we can use our custom WeRetailHttpCacheConfigExtension by creating a sling:OsgiConfig named com.weretail.cache.WeRetailHttpCacheConfigExtension-example.xml:

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="" xmlns:cq=""
    xmlns:jcr="" xmlns:nt=""
Please report any issues with the Feature or documentation on the ACS AEM Commons GitHub Issue tracker.