FvctxCachingPolicy.java 3.62 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.scene7.is.provider.CacheSpec
 *  com.scene7.is.provider.catalog.Catalog
 *  com.scene7.is.provider.catalog.ObjectRecord
 *  com.scene7.is.sleng.CacheAction
 *  com.scene7.is.sleng.CacheEnum
 *  org.jetbrains.annotations.NotNull
 */
package com.scene7.is.ps.provider.fvctx;

import com.scene7.is.provider.CacheSpec;
import com.scene7.is.provider.catalog.Catalog;
import com.scene7.is.provider.catalog.ObjectRecord;
import com.scene7.is.ps.provider.ModifierSet;
import com.scene7.is.ps.provider.Request;
import com.scene7.is.ps.provider.Util;
import com.scene7.is.ps.provider.defs.ModifierEnum;
import com.scene7.is.ps.provider.util.CachingPolicy;
import com.scene7.is.ps.provider.util.NoCacheCachingPolicy$;
import com.scene7.is.sleng.CacheAction;
import com.scene7.is.sleng.CacheEnum;
import org.jetbrains.annotations.NotNull;

public class FvctxCachingPolicy
implements CachingPolicy {
    @NotNull
    private final CacheEnum cacheUse;
    private final long catalogTimeStamp;

    public static CachingPolicy createFvctxCachingPolicy(boolean useCache, boolean useMainRecordValidation, @NotNull Request request) {
        if (useCache) {
            Catalog catalog = request.getRecord().getCatalog();
            ModifierSet attributes = request.getGlobalAttributes();
            boolean useCatalogValidation = catalog.getCatalogBasedValidation();
            CacheEnum cacheUse = attributes.get(ModifierEnum.CACHE, CacheSpec.ON).getServer();
            if (useMainRecordValidation && useCatalogValidation) {
                return new FvctxCachingPolicy(cacheUse, Util.computeCatalogTimeStamp(request));
            }
            return new FvctxCachingPolicy(cacheUse, catalog.getCompileTimeStamp());
        }
        return NoCacheCachingPolicy$.MODULE$;
    }

    @Override
    public boolean cacheEnabled() {
        return this.cacheUse != CacheEnum.OFF;
    }

    @Override
    public long getValidationTime() {
        return this.catalogTimeStamp;
    }

    @Override
    public CacheAction getCacheHitAction(long cacheTimeStamp) {
        switch (this.cacheUse) {
            case VALIDATE: 
            case UPDATE: {
                return CacheAction.UPDATE;
            }
            case DELETE: {
                return CacheAction.DELETE;
            }
            case ON: {
                if (this.catalogTimeStamp != FvctxCachingPolicy.truncToSec(cacheTimeStamp)) {
                    return CacheAction.UPDATE;
                }
                return CacheAction.REUSE;
            }
            case OFF: {
                throw new IllegalStateException("Attempt to access cache entry when caching is turned \"off\"");
            }
        }
        throw new AssertionError((Object)("Unsupported cacheUse value: " + (Object)this.cacheUse));
    }

    @Override
    public CacheAction getCacheMissAction() {
        switch (this.cacheUse) {
            case DELETE: 
            case OFF: {
                return CacheAction.IGNORE;
            }
            case VALIDATE: 
            case UPDATE: 
            case ON: {
                return CacheAction.CREATE;
            }
        }
        throw new AssertionError((Object)("Unsupported cacheUse value: " + (Object)this.cacheUse));
    }

    @Override
    public CacheEnum getCacheSetting() {
        return this.cacheUse;
    }

    private FvctxCachingPolicy(@NotNull CacheEnum useCache, long timeStamp) {
        this.cacheUse = useCache;
        this.catalogTimeStamp = FvctxCachingPolicy.truncToSec(timeStamp);
    }

    private static long truncToSec(long value) {
        return value / 1000 * 1000;
    }

}