1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <?php
define("CACHE_FILE", "coords.cache");
if (!is_file(CACHE_FILE) || !@include(CACHE_FILE)) {
$query = "SELECT x,y,z FROM table WHERE blah";
if (!$results = $pdo->query($query))
throw new RuntimeException("Query execution fails");
$data = $results->fetchAll(PDO::FETCH_ASSOC);
// écriture du cache
if (!file_put_contents(CACHE_FILE, '<?php $data='.var_export($data,true).';')) {
throw new RuntimeException("Could not write cache file " . CACHE_FILE);
}
}
// sait on jamais
!empty($data) or $data = array();
shuffle($data);
$firsts = array_slice($data, 0, 16);
var_dump($firsts); |
Partager