CatalogCachingPolicy.java 2.94 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.scene7.is.sleng.CacheAction
 *  com.scene7.is.sleng.CacheEnum
 */
package com.scene7.is.ps.provider.util;

import com.scene7.is.ps.provider.util.CachingPolicy;
import com.scene7.is.sleng.CacheAction;
import com.scene7.is.sleng.CacheEnum;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CatalogCachingPolicy
implements CachingPolicy,
Serializable {
    private static final Logger LOGGER = Logger.getLogger(CatalogCachingPolicy.class.getName());
    private final CacheEnum cacheUse;
    private final long catalogTimeStamp;

    public CatalogCachingPolicy(CacheEnum useCache, long timeStamp) {
        this.cacheUse = useCache;
        this.catalogTimeStamp = CatalogCachingPolicy.truncToSec(timeStamp);
    }

    @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: {
                return CacheAction.VALIDATE;
            }
            case UPDATE: {
                return CacheAction.UPDATE;
            }
            case DELETE: {
                return CacheAction.DELETE;
            }
            case ON: {
                if (this.catalogTimeStamp != CatalogCachingPolicy.truncToSec(cacheTimeStamp)) {
                    LOGGER.log(Level.FINE, "Need to validate cache.  Catalog time stamp is " + this.catalogTimeStamp + " and cache time stamp truncated to second is " + CatalogCachingPolicy.truncToSec(cacheTimeStamp));
                    return CacheAction.VALIDATE;
                }
                LOGGER.log(Level.FINE, "No need to validate cache.  Catalog time stamp is " + this.catalogTimeStamp + " and cache time stamp truncated to second is " + CatalogCachingPolicy.truncToSec(cacheTimeStamp));
                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 static long truncToSec(long value) {
        return value / 1000 * 1000;
    }

}