Skip to content

Create cache based on custom timestamp#3

Open
peterpostmann wants to merge 1 commit into
inouet:masterfrom
peterpostmann:filetime_cache
Open

Create cache based on custom timestamp#3
peterpostmann wants to merge 1 commit into
inouet:masterfrom
peterpostmann:filetime_cache

Conversation

@peterpostmann

@peterpostmann peterpostmann commented Sep 16, 2017

Copy link
Copy Markdown

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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant