PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Caching » Cache_Lite » Bug #5109

nested calls crash file key

Details

Request #5109nested calls crash file key
Submitted2005-08-16 16:42 UTC
Fromrumata at gmx dot co dot uk
Assignedfab
StatusWont fix
PackageCache_Lite
PHP Version4.3.11
OSWindows
Roadmaps(Not assigned)

Comments

[2005-08-16 16:42 UTC] rumata at gmx dot co dot uk

Description:
------------
Nested calls to get & save spoil caching because code in save is dependent on call of $this->_setFileName($id, $group); in get.

As far as I understand it's kind of optimization to avoid calling rather "expensive" method _setFileName twice. But this optimization got me to 2 hour debug =)

Maybe I'm using cache in a wrong way?

Test script:
---------------
In my case, I was using cache in this way:
function &GetCacheInstance() {
static $cache = null;
if ($cache == null) {
require_once('extlib/pear/Cache/Lite.php');
$cache = new Cache_Lite(array(
...
));
}
return $cache;
}

function func1($id1) {
$cache =& GetCacheInstance();
$res = $cache->get($id1, 'id1');
if ($res === false) {
$res = GetId1FromDB($id1);
$cache->save($res, $id1, 'id1');
}
return $res;
}

function func2($id2, $id1) {
$cache =& GetCacheInstance();
$res = $cache->get($id2, 'id2');
if ($res === false) {
$res =
GetId2FromDBUsingId1Object($id2, func1($id1));
$cache->save($res, $id2, 'id2');
}
return $res;
}

Now I wish to call func2(1,2);

Expected result:
----------------
Normal work

Actual result:
--------------
1. $res = $cache->get($id2, 'id2');
is called and
$cache->_setFileName() is called for 'id2' and $id2
2. $res = $cache->get($id1, 'id1');
is called and
$cache->_setFileName() is called for 'id1' and $id1
3. $cache->save($res, $id1, 'id1');
saves to id1 group
4. $cache->save($res, $id2, 'id2');
also saves data to id1.

[2005-09-19 21:39 UTC] fab at php dot net

Thanks for the patch. I review it for the next version

[2005-11-17 09:27 UTC] fab at php dot net

It's really interresting but your hack change the API of Cache_Lite ($id and $group are not optionnal anymore in the save() method).

So I will consider it only for an API break (Cache_Lite2 ?)

Thanks anyways