Skip to content
cache.php 3.13 KiB
Newer Older
klimplant's avatar
klimplant committed
<?php
// ---------------------------------------------------------
// Global definition for the cache
// ---------------------------------------------------------
define('cacheFile', '.cache'); // File where the cache is being stored
define('cacheMaxAge', 300); // Time in s how long to hold the cache until reset
define('cacheHashAlgo', 'sha512'); // Hash algo to use for cache validation  - http://de2.php.net/manual/de/function.hash.php#104987
// ---------------------------------------------------------

function getCache($dataKey = '') {
    if (!file_exists(cacheFile)) {
        if (debug) {
            echo "cache file not found: " . __FILE__ . ":". __LINE__  . " \n<br>";
        }
        return false;
    }

    $cache = file_get_contents(cacheFile);
    $cache = unserialize($cache);
    if (!is_array($cache)) {
        if (debug) {
            echo "cache is no array: " . __FILE__ . ":". __LINE__  . " \n<br>";
        }
        resetCache();
        return false;
    }

    if (!validateCache($cache)) {
        if (debug) {
            echo "invalid cache: " . __FILE__ . ":". __LINE__  . " \n<br>";
        }
        resetCache();
        return false;
    }

    $data = $cache['data'];
    if (!array_key_exists($dataKey, $data)) {
        if (debug) {
            echo "array key not found: " . __FILE__ . ":". __LINE__  . " \n<br>";
        }
        return false;
    }
    if (!empty($dataKey)) {
        return $data[$dataKey];
    }
    return $data;
}

function writeCache($data) {
    $hash = serialize($data);
    $hash = hash('sha512', $hash);
    $meta = [
        'timestamp' => time(),
        'hash' => $hash,
    ];
    $cache = [
        'meta' => $meta,
        'data' => $data
    ];
    $cacheSerialized = serialize($cache);
    file_put_contents(cacheFile, $cacheSerialized);
    if (validateCache($cache, $data)) {
        if (debug) {
            echo "valid cache: " . __FILE__ . ":". __LINE__  . " \n<br>";
        }
        return true;
    }
    resetCache();
    return false;
}

function resetCache() {
    if (file_exists(cacheFile)) {
        return unlink(cacheFile);
    } else {
        return true;
    }
}

function validateCache($cache, $data = null) {
    $meta = $cache['meta'];
    $data = $cache['data'];

    $hashMeta = $meta['hash'];
    $dataHash = serialize($data);
    $dataHash = hash(cacheHashAlgo, $dataHash);
klimplant's avatar
klimplant committed
    if (debug) {
        var_dump([$hashMeta, $dataHash, $hashMeta !== $dataHash]);
    }
    if ($hashMeta !== $dataHash) {
klimplant's avatar
klimplant committed
        if (debug) {
            echo "hashes don't match: " . __FILE__ . ":". __LINE__  . " \n<br>";
        }
        return false;
    }
    $timestamp = $meta['timestamp'];
klimplant's avatar
klimplant committed
    if (debug) {
        var_dump([$timestamp, time(), (time() - cacheMaxAge), (time() - cacheMaxAge) > $timestamp]);
    }
klimplant's avatar
klimplant committed
    if ((time() - cacheMaxAge) > $timestamp) {
        if (debug) {
            echo "cache expired: " . __FILE__ . ":". __LINE__  . " \n<br>";
        }
        return false;
    }

    if (!is_null($data) && $cache['data'] !== $data) {
        if (debug) {
            echo "data doesn't math: " . __FILE__ . ":". __LINE__  . " \n<br>";
        }
        return false;
    }

    return true;
}