/** * __call * * The heart of the RecordProxy. The method and params go in, the result is then cached and servered next time. * * @param string $method * @param array $params * @return mixed */ public function __call($method, $params) { // Is filterable? if (!in_array($method, Memoize::$objectNames[$this->_model])) { return parent::__call($method, $params); } // Variables $hash = Memoize::hashArgs($params); // Create array if it doesn't exist if (!isset($this->_memoizeResults[$method])) { $this->_memoizeResults[$method] = array(); } // Check if method + params have been ran already if (isset($this->_memoizeResults[$method][$hash])) { return $this->_memoizeResults[$method][$hash]; } // Set and return return $this->_memoizeResults[$method][$hash] = parent::__call($method, $params); }
/** * __call * * The heart of the HelperProxy. The method and params go in, the result is then cached and servered next time. * * @param string $method * @param array $params * @return mixed */ public function __call($method, $params) { // To filter or not to filter. That is the question if (!in_array($method, Memoize::$objectNames[$this->_objectName])) { return call_user_func_array(array($this->_object, $method), $params); } // Eval $key = Memoize::hashArgs($params); // Create array if it doesn't exist if (!isset($this->_memoizeResults[$method])) { $this->_memoizeResults[$method] = array(); } // Check if method + params have been ran already if (isset($this->_memoizeResults[$method][$key])) { return $this->_memoizeResults[$method][$key]; } // Get results switch (count($params)) { case 0: $this->_memoizeResults[$method][$key] = $this->_object->{$method}(); break; case 1: $this->_memoizeResults[$method][$key] = $this->_object->{$method}($params[0]); break; case 2: $this->_memoizeResults[$method][$key] = $this->_object->{$method}($params[0], $params[1]); break; case 3: $this->_memoizeResults[$method][$key] = $this->_object->{$method}($params[0], $params[1], $params[2]); break; case 4: $this->_memoizeResults[$method][$key] = $this->_object->{$method}($params[0], $params[1], $params[2], $params[3]); break; case 5: $this->_memoizeResults[$method][$key] = $this->_object->{$method}($params[0], $params[1], $params[2], $params[3], $params[4]); break; default: $this->_memoizeResults[$method][$key] = call_user_func_array(array(&$this->_object, $method), $params); } return $this->_memoizeResults[$method][$key]; }
public function testSpeedSteady() { // Filtering Memoize::add(array(array('name' => 'li3_memoize\\tests\\mocks\\Users', 'method' => array('doesNotExist')))); // Model $name = 'lithium'; $user = new Record(array('model' => 'li3_memoize\\tests\\mocks\\Users', 'data' => array('name' => $name))); // Timing $times = array(); $times[0] = microtime(true); $results[0] = $user->name(); $times[1] = microtime(true); $results[1] = $user->name(); $times[2] = microtime(true); // The first iteration needs to be MORE than a second $this->assertTrue($times[1] - $times[0] > 1); // The second needs to be MORE than a second $this->assertFalse($times[2] - $times[1] < 1); // Results should equal $this->assertEqual($results[0], $results[1]); }
public function testSpeedSteady() { // Filtering Memoize::add(array(array('name' => 'li3_memoize\\tests\\mocks\\Prose', 'method' => array('doesNotExist')))); // Helper $prose = new Prose(); Memoize::catchHelper($prose); // Timing $times = array(); $times[0] = microtime(true); $results[0] = $prose->slowSpeak('lithium'); $times[1] = microtime(true); $results[1] = $prose->slowSpeak('lithium'); $times[2] = microtime(true); // The first iteration needs to be MORE than a second $this->assertTrue($times[1] - $times[0] > 1); // The second needs to be LESS than a second $this->assertTrue($times[2] - $times[1] < 1); // Results should equal $this->assertEqual($results[0], $results[1]); }
<?php use lithium\util\collection\Filters; use li3_memoize\extensions\Memoize; /** * Filters the creation of helpers/models */ Filters::apply('lithium\\core\\Libraries', 'instance', function ($self, $params, $chain) { // Prescan Model if (isset($params['options']['model'])) { Memoize::catchModel($self, $params, $chain); } $object = $chain->next($self, $params, $chain); // Postscan Helper if ($params['type'] == 'helper') { Memoize::catchHelper($object); } return $object; });
public function testDifferentHashArgs() { $objs = array(new \stdClass(), new \stdClass()); $first = Memoize::hashArgs(array('String', 35, M_PI, $objs[0])); $second = Memoize::hashArgs(array('String', 35, M_PI, $objs[1])); $this->assertNotEqual($first, $second); }