Memoization
- Intent
- Cache result after computing it.
- Motivation
- Eliminate repeated database queries
- Implementation
- Simply cache the result into an array or other appropriate type
- Examples
- with functions
function db_get_something( $x ) {
static $cache = array();
if(empty($cache[$x])) {
$cache[$x] = //some db query result;
}
return $cache[$x];
}
- with objects
class db_get_somethning {
private $cache = array();
function func() {
if(empty($this->cache[$x]))
$this->cache[$x] = //some db query result;
return $this->cache[$x];
}
}
- Complications
- cache lifetime, size, invalidation