php - Caching large arrays -
i have next function:
function f($idx, $arr) { static $a; if ($a == null) { foreach ($arr $v) { $key = $v['key']; if (!isset($a[$key])) { $a[$key] = array(); } $a[$key][] = $v; } } homecoming $a[$idx]; } and initial conditions are:
functionf() called many-many times in 1 request $arr large $arr may differ in different function calls (low cardinality) $idx differs in every function phone call (high cardinality) and need know, if $arr cached , if not, create "cached version", maintain previous arrays.
according 2. i'm not able utilize md5(serialize($arr)) utilize identifier, need way determine that. have thought how accomplish such hight-performance caching functionality (let's i'm not able changes outside function)?
if it's not imperative $arr not modified, i'd add together optimized key access straight it:
// note argument has been changed &$arr - we're passing reference function f($idx, &$arr) { if (empty($arr['___cached'])) { $arr['___cached'] = array(); foreach ($arr $k => $v) { if ($k === '___cached') continue; if (!isset($arr['___cached'][$v['key']])) { $arr['___cached'][$v['key']] = array(); } $arr['___cached'][$v['key']][] = $v; } } homecoming $arr['___cached'][$idx]; } php arrays performance caching
No comments:
Post a Comment