JamesMoss\Flywheel\CachedQuery::execute PHP Метод

execute() публичный Метод

Checks the cache to see if this query exists and returns it. If it's not in the cache then the query is run, stored and then returned.
public execute ( ) : Result
Результат Result The result of the query.
    public function execute()
    {
        static $apcPrefix = null;
        if ($apcPrefix === null) {
            $apcPrefix = function_exists('apcu_fetch') ? 'apcu' : 'apc';
        }
        // Generate a cache key by comparing our parameters to see if we've
        // made this query before
        $key = $this->getParameterHash() . $this->getFileHash();
        // Try and fetch a cached result object from APC
        $funcName = $apcPrefix . '_fetch';
        $success = false;
        $result = $funcName($key, $success);
        // If the result isn't in the cache then we run the real query
        if (!$success) {
            $result = parent::execute();
            $funcName = $apcPrefix . '_store';
            $funcName($key, $result);
        }
        return $result;
    }

Usage Example

Пример #1
0
 public function testCache()
 {
     if (!ini_get('apc.enable_cli')) {
         $this->markTestSkipped('The APC extension is not enabled on the command line. Set apc.enable_cli=1.');
     }
     $path = __DIR__ . '/fixtures/datastore/querytest';
     $config = new Config($path . '/');
     $repo = new Repository('countries', $config);
     $query = new CachedQuery($repo);
     $total = 0;
     for ($i = 0; $i < 100; $i++) {
         $start = microtime(true);
         $query->where('cca2', '==', 'GB');
         $query->execute();
         $total += microtime(true) - $start;
     }
 }