Skip to content

Commit

Permalink
refactor cache
Browse files Browse the repository at this point in the history
  • Loading branch information
HillLiu committed Sep 15, 2022
1 parent cfe8fe0 commit 35d904f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 57 deletions.
134 changes: 77 additions & 57 deletions src/_cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,83 +70,103 @@ function () use ($r) {
'cache'
); // dev
};

/**
* hasCache -> return cache
* noCache -> run $this->_curl->get
* isExipre default to true
* finial process will call with $this->finish
*/
$isExpire = true;
$r = null;
if ($this->hasCache($hash)) {
$r = $this->getCache($hash);
$createTime = \PMVC\get($r, 'createTime', 0);
if (!$this->_isExpire($createTime, $ttl)) {
if (!empty($more)) {
$r->more = \PMVC\get($r->more, $more);
} else {
$r->more = null;
}

$r->info = function () use ($r, $ttl) {
return $this->caller->cache_dev(
$r,
$this->_getPurgeDevKey($r->hash),
$ttl
);
};

if (is_callable($callback)) {
$CurlHelper = new CurlHelper();
$CurlHelper->setOptions(null, $setCacheCallback);
$CurlHelper->resetOptions($options);
$r->cache = true;
$ttl = call_user_func_array($callback, [$r, $CurlHelper]);
}
if ($this->_isExpire($createTime, $ttl)) {
$isExpire = $this->_isExpire($r->createTime, $ttl);
}
if ($isExpire) {
$oCurl = $this->_curl->get(null, $setCacheCallback);
$oCurl->resetOptions($options);
} else {
$r->more = empty($more) ? null : \PMVC\get($r->more, $more);
if (is_callable($callback)) {
$CurlHelper = new CurlHelper();
$CurlHelper->setOptions(null, $setCacheCallback);
$CurlHelper->resetOptions($options);
$r->cache = true;
$nextTtl = call_user_func_array($callback, [$r, $CurlHelper]);
if ($this->_isExpire($r->createTime, $nextTtl)) {
$r->purge();
}

\PMVC\dev(
/**
* @help PURGE: [url]
*/
function () use ($r) {
$r->purge();
return [
'Clean-Cache' => [
'hash' => $r->hash,
'url' => $r->url,
],
];
},
}
// locate after callback for update $ttl
$r->info = function () use ($r, $ttl, $nextTtl) {
if (is_numeric($nextTtl)) {
$r->expire = $this->_calExpireSec($r->createTime, $nextTtl);
$ttl = $nextTtl;
}
return $this->caller->cache_dev(
$r,
$this->_getPurgeDevKey($r->hash),
['url' => $r->url]
$ttl
);
};

\PMVC\dev(
/**
* @help Minons cache status. could use with ?--trace=curl
*/
function () use ($r) {
return $r->info();
},
'cache'
); // dev
return;
}
\PMVC\dev(
/**
* @help PURGE: [url]
*/
function () use ($r) {
$r->purge();
return [
'Clean-Cache' => [
'hash' => $r->hash,
'url' => $r->url,
],
];
},
$this->_getPurgeDevKey($r->hash),
['url' => $r->url]
);

\PMVC\dev(
/**
* @help Minons cache status. could use with ?--trace=curl
*/
function () use ($r) {
return $r->info();
},
'cache'
); // dev
}
$oCurl = $this->_curl->get(null, $setCacheCallback);
$oCurl->resetOptions($options);
}

private function _getPurgeDevKey($hash)
{
return 'purge-' . substr($hash, 0, 8);
}

private function _calExpireSec($createTime, $ttl)
{
if (!$createTime) {
$createTime = 0;
}
if (is_numeric($ttl)) {
return $createTime + $ttl - time();
} else {
/**
* use for $this->_isExpire
* only $ttl equal `false` or `number` will check expire, else will assume not expire.
*/
return 0;
}
}

private function _isExpire($createTime, $ttl)
{
if ($ttl === false) {
return true;
} else {
if (is_numeric($ttl)) {
return $createTime + $ttl - time() < 0;
} else {
return false;
}
return $this->_calExpireSec($createTime, $ttl) < 0;
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/_cache_dev.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ function () use (&$rinfo, $r) {
'curl' => 'Decode body with json',
'trace' => 'get trace info',
];
$rinfo['serverLocalTime'] = date('Y/m/d H:i:s');
$rinfo['createLocalTime'] = date('Y/m/d H:i:s', $rinfo['createTime']);
$rinfo['expireLocalTime'] = date(
'Y/m/d H:i:s',
$rinfo['expire'] + time()
);
$rinfo['-url'] = $r->url;
$rinfo['purge'] = $purgeKey;
$rinfo['ttl'] = $ttl;
Expand Down

0 comments on commit 35d904f

Please sign in to comment.