示例#1
0
 public function testGet()
 {
     variable::set('unittestget', 'testvalueget');
     $value = variable::get('unittestget');
     $this->assertEquals('testvalueget', $value);
     $value = variable::get('doesntexist', 'default');
     $this->assertEquals('default', $value);
     variable::delete('unittestget');
 }
 public static function populate_tables($db, $last_run_date)
 {
     $lastDoneInfo = (array) variable::get('custom_cache_tables', array(), false);
     foreach (glob(MODPATH . "custom_cache_tables/definitions/*.php") as $filename) {
         require_once $filename;
         $defname = preg_replace('/\\.php$/', '', basename($filename));
         if (!function_exists("get_{$defname}_query") || !function_exists("get_{$defname}_metadata")) {
             kohana::log('error', "Skipping incomplete custom_cache_tables definition {$filename}");
             continue;
             // foreach
         }
         $metadata = call_user_func("get_{$defname}_metadata");
         if (empty($metadata['frequency'])) {
             kohana::log('error', "Definition {$filename} omits metadata frequency for custom_cache_tables");
             continue;
             // foreach
         }
         if (empty($lastDoneInfo[$defname]) || strtotime($lastDoneInfo[$defname]) < strtotime("-{$metadata['frequency']}")) {
             // for a new cache table, use now as the starting point to trigger population
             if (empty($lastDoneInfo[$defname])) {
                 $lastDoneInfo[$defname] = date(DATE_ISO8601);
             }
             // Even if we are due an update, we might not have to do anything if there is a detect_changes_query
             // which returns nothing
             if (!empty($metadata['detect_changes_query'])) {
                 $check = $db->query(str_replace('#date#', date('Y-m-d H:i:s', strtotime($lastDoneInfo[$defname])), $metadata['detect_changes_query']))->current();
                 if (!$check->count) {
                     kohana::log('debug', "Skipping {$defname} as no changes available to process");
                     // reset the time to the next check
                     $lastDoneInfo[$defname] = date(DATE_ISO8601);
                     continue;
                     // foreach
                 }
             }
             // if the table already exists, delete it
             if (!empty($lastDoneInfo[$defname])) {
                 $db->query("DROP TABLE custom_cache_tables.{$defname}");
             }
             echo "building cache table {$defname}<br/>";
             self::build_table($db, $defname);
             $lastDoneInfo[$defname] = date(DATE_ISO8601);
         }
     }
     variable::set('custom_cache_tables', $lastDoneInfo);
 }
/**
 * Hook into the task scheduler. This uses the queries defined in the cache_builder.php
 * file to create and populate cache tables. The tables are not always up to date as they
 * are only updated when the scheduler runs, but they have the advantage of simplifying
 * the data model for reporting as well as reducing the need to join in queries, therefore
 * significantly improving report performance.
 * @param string $last_run_date Date last run, or null if never run
 * @param object $db Database object.
 */
function cache_builder_scheduled_task($last_run_date, $db)
{
    if (isset($_GET['force_cache_rebuild'])) {
        $last_run_date = date('Y-m-d', time() - 60 * 60 * 24 * 365 * 200);
    } elseif ($last_run_date === null) {
        // first run, so get all records changed in last day. Query will automatically gradually pick up the rest.
        $last_run_date = date('Y-m-d', time() - 60 * 60 * 24);
    }
    try {
        foreach (kohana::config('cache_builder') as $table => $queries) {
            cache_builder::populate_cache_table($db, $table, $last_run_date);
            if (!variable::get("populated-{$table}")) {
                // don't bother populating the next table, as there can be dependencies.
                break;
            }
        }
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}
 /**
  * Runs an insert or update statemnet to update one of 
  * the cache tables. 
  * @param object $db Database connection.
  * @param string $query Query used to perform the update or insert. Can be a string, or an 
  *   associative array of SQL strings if multiple required to do the task.
  * @param string $action Term describing the action, used for feedback only.
  */
 private static function run_statement($db, $table, $query, $action)
 {
     $master_list_id = Kohana::config('cache_builder_variables.master_list_id', FALSE, FALSE);
     $master_list_id = $master_list_id ? $master_list_id : 0;
     // default so nothing breaks
     if (is_array($query)) {
         foreach ($query as $title => $sql) {
             $sql = str_replace('#master_list_id#', $master_list_id, $sql);
             $count = $db->query($sql)->count();
             if (variable::get("populated-{$table}")) {
                 echo ", {$count} {$action}(s) for {$title}";
             }
         }
     } else {
         $sql = str_replace('#master_list_id#', $master_list_id, $query);
         $count = $db->query($query)->count();
         if (variable::get("populated-{$table}")) {
             echo ", {$count} {$action}(s)";
         }
     }
 }