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); }
/** * Build a temporary table with the list of IDs of records we need to update. * The table has a deleted flag to indicate newly deleted records. * @param objcet $db Database connection. * @param string $table Name of the table being cached, e.g. occurrences. * @param string $query A query which selects a list of IDs for all new, updated or * deleted records (including looking for updates or deletions caused by related * records). * @param string $last_run_date Date/time of the last time the cache builder was * run, used to filter records to only the recent changes. Supplied as a string * suitable for injection into an SQL query. */ private static function get_changelist($db, $table, $queries, $last_run_date) { $query = str_replace('#date#', $last_run_date, $queries['get_changed_items_query']); $db->query("create temporary table needs_update_{$table} as {$query}"); if (!variable::get("populated-{$table}")) { // as well as the changed records, pick up max 5000 previous records, which is important for initial population. // 5000 is an arbitrary number to compromise between performance and cache population. // of the cache $query = $queries['get_missing_items_query'] . ' limit 5000'; $result = $db->query("insert into needs_update_{$table} {$query}"); if ($result->count() === 0) { // Flag that we don't need to do any more previously existing records as they are all done. // Future cache updates can just pick up changes from now on. variable::set("populated-{$table}", true); echo "{$table} population completed<br/>"; } } $db->query("ALTER TABLE needs_update_{$table} ADD CONSTRAINT ix_nu_{$table} PRIMARY KEY (id)"); $r = $db->query("select count(*) as count from needs_update_{$table}")->result_array(false); $row = $r[0]; if (variable::get("populated-{$table}")) { if ($row['count'] > 0) { echo "Updating {$table} with {$row['count']} changes<br/>"; } else { echo "No changes for {$table}<br/>"; } } return $row['count']; }