Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions public/include/classes/setting.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,30 @@

class Setting extends Base {
protected $table = 'settings';
private $cache = array();

/**
* Fetch all values available and cache them in this class
* That way we don't fetch them from DB for each call
*/
public function createCache() {
if ($aSettings = $this->getAllAssoc()) {
foreach ($aSettings as $key => $aData) {
$this->cache[$aData['name']] = $aData['value'];
}
return true;
}
return false;
}

/**
* Fetch a value from our table
* @param name string Setting name
* @return value string Value
**/
public function getValue($name, $default="") {
// Try our class cache first
if (isset($this->cache[$name])) return $this->cache[$name];
$stmt = $this->mysqli->prepare("SELECT value FROM $this->table WHERE name = ? LIMIT 1");
if ($this->checkStmt($stmt) && $stmt->bind_param('s', $name) && $stmt->execute() && $result = $stmt->get_result()) {
if ($result->num_rows > 0) {
Expand All @@ -30,6 +47,8 @@ public function getValue($name, $default="") {
* @return bool
**/
public function setValue($name, $value) {
// Update local cache too
$this->cache[$name] = $value;
$stmt = $this->mysqli->prepare("
INSERT INTO $this->table (name, value)
VALUES (?, ?)
Expand All @@ -44,3 +63,5 @@ public function setValue($name, $value) {
$setting->setDebug($debug);
$setting->setMysql($mysqli);
$setting->setErrorCodes($aErrorCodes);
// Fill our class cache with data so we don't have to run SQL queries all the time
$setting->createCache();