file-cache icon indicating copy to clipboard operation
file-cache copied to clipboard

Create cache based on custom timestamp

Open peterpostmann opened this issue 7 years ago • 0 comments

Save data with lifetime 0 to make it valid forever, and check against custom timestamp with get. e.g.

// some function which generates data
function getData()
{
    return array(
    'name' => 'John',
    'age'  => 20,
    'sex'  => 'f');
}

// a timestamp which indicates if the data changed
$timestamp = filemtime(__FILE__);

// Get data from cache if cache is newer than timestamp
$user = $cache->get($id, $timestamp);

// If cache is expired or does not exist, re-generate data and store in cache
if(!$user)
{
    $user = getData();
    $cache->save($id, $user, 0);
}

peterpostmann avatar Sep 16 '17 11:09 peterpostmann