package org.wisdom.cache.ehcache;

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Requires;
import org.slf4j.LoggerFactory;
import org.wisdom.api.cache.Cache;
import org.wisdom.api.cache.Cached;
import org.wisdom.api.http.Result;
import org.wisdom.api.interceptor.InterceptionContext;
import org.wisdom.api.interceptor.Interceptor;

/**
 * An action interceptor caching the result of an action and returning the cached result if it was cached already.
 */
@Component
@Provides(specifications = Interceptor.class)
@Instantiate
public class CachedActionInterceptor extends Interceptor<Cached> {

    @Requires
    private Cache cache;

    @Override
    public Result call(Cached configuration, InterceptionContext context) throws Throwable {
        Result result = (Result) cache.get(configuration.key());

        if (result == null) {
            result = context.proceed();
            cache.set(configuration.key(), result, configuration.duration());
            LoggerFactory.getLogger(this.getClass()).info("Caching result of " + context.request().uri() + " for " +
                    configuration.duration() + " seconds");
        }

        return result;
    }

    @Override
    public Class<Cached> annotation() {
        return Cached.class;
    }
}

@@ -236,24 +236,55 @@
 s.Requires;%0A
+import org.joda.time.Duration;%0A
 import org.s
@@ -1072,71 +1072,343 @@
     
-cache.set(configuration.key(), result, configuration.duration()
+Duration duration;%0A            if (configuration.duration() == 0) %7B%0A                // Eternity == 1 year.%0A                duration = Duration.standardDays(365);%0A            %7D else %7B%0A                duration = Duration.standardSeconds(configuration.duration());%0A            %7D%0A            cache.set(configuration.key(), result, duration
 );%0A 

Support 'eternity'