Skip to content

Commit af74edf

Browse files
committed
Improved cache system
1 parent 49e7a40 commit af74edf

3 files changed

Lines changed: 36 additions & 13 deletions

File tree

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# PHP Database Web API
22
![](cover.png)
33

4-
**Version:** 0.6.128 beta
4+
**Version:** 0.6.129 beta
55

66
**Github:** https://github.com/marcocesarato/Database-Web-API
77

@@ -62,12 +62,13 @@ define("__API_NAME__", "Database Web API"); // API Name
6262

6363
| Settings | Description | Default |
6464
|------------------|------------------------------------------------------------------------------------|-----------|
65-
| default | Default dataset | false |
66-
| api | Accessible through API | true |
65+
| default | Default dataset | false |
66+
| api | Accessible through API | true |
6767
| name | Database Name | |
6868
| username | Database Username | root |
6969
| password | Database Password | root |
7070
| server | Database Server Address | localhost |
71+
| ttl | Cache time to live (set 1 for disable) | 3600 |
7172
| port | Database Port | 3306 |
7273
| type | Database Type (ex. `mysql`, `psql` ecc..) | mysql |
7374
| table_list | Database Tables Whitelist (Allow only the tables in this list, if empty allow all) | null |
@@ -85,6 +86,7 @@ define("__API_DATASETS__", serialize(array(
8586
'name' => 'database_name', // Database name
8687
'username' => 'user', // root is default
8788
'password' => 'passwd', // root is default
89+
'ttl' => 1, // Cache time to live. Disable cache (1 second only)
8890
'server' => 'localhost', // localhost default
8991
'port' => 5432, // 3306 is default
9092
'type' => 'pgsql', // mysql is default
@@ -133,6 +135,7 @@ array(
133135
'username' => 'website',
134136
'password' => 's3cr3tpa55w0rd',
135137
'server' => 'localhost',
138+
'ttl' => 1,
136139
'port' => 3306,
137140
'type' => 'mysql',
138141
'table_docs' => array(),

config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
'password' => 'root', // root is default
4444
'server' => 'localhost', // localhost default
4545
'port' => 3306, // 3306 is default
46+
'ttl' => 1, // Cache time to live. Disable cache (1 second only)
4647
'type' => 'mysql', // mysql is default
4748
'table_docs' => $docs['dataset'],
4849
'table_list' => array(), // Tables's whitelist (Allow only the tables in this list, if empty allow all)

includes/classes/API.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ private function documentation($query, $db = null) {
400400

401401
$final_result = array();
402402

403-
$key = md5(serialize($query) . $this->getDatabase($db)->name . '_docs');
403+
$key = md5(json_encode($query) . $this->getDatabase($db)->name . '_docs');
404404

405405
if($cache = $this->getCache($key)) {
406406
return $cache;
@@ -503,7 +503,7 @@ private function documentation($query, $db = null) {
503503
*/
504504
private function get($query, $db = null) {
505505

506-
$key = md5(serialize($query) . $this->getDatabase($db)->name);
506+
$key = md5(json_encode($query) . $this->getDatabase($db)->name);
507507

508508
if($cache = $this->getCache($key)) {
509509
return $cache;
@@ -1749,11 +1749,21 @@ private function sanitizeResults($results) {
17491749
*/
17501750
private function getCache($key) {
17511751

1752-
if(!extension_loaded('apc') || (ini_get('apc.enabled') != 1)) {
1753-
if(!empty($this->cache[$key])) {
1754-
return $this->cache[$key];
1752+
$tmpdir = sys_get_temp_dir();
1753+
if(!empty($this->cache[$key])) {
1754+
return $this->cache[$key];
1755+
} else if((ini_get('opcache.enable') == 0 || ini_get('apc.enabled') == 0) && is_writable($tmpdir)) {
1756+
$file = $tmpdir . DIRECTORY_SEPARATOR . $key;
1757+
$ttl = (!empty($this->db->ttl)) ? $this->db->ttl : $this->ttl;
1758+
if(file_exists($file)) {
1759+
if((time() - @filectime($file) < $ttl)) {
1760+
@unlink($file);
1761+
} else {
1762+
@include($file);
1763+
}
17551764
}
1756-
} else {
1765+
return isset($value) ? $value : false;
1766+
} else if(extension_loaded('apc') || (ini_get('apc.enabled') == 1)) {
17571767
return apc_fetch($key);
17581768
}
17591769

@@ -1769,18 +1779,27 @@ private function getCache($key) {
17691779
*/
17701780
private function setCache($key, $value, $ttl = null) {
17711781

1772-
if($ttl == null) {
1782+
$this->cache[$key] = $value;
1783+
1784+
$tmpdir = sys_get_temp_dir();
1785+
if((ini_get('opcache.enable') == 0 || ini_get('apc.enabled') == 0) && is_writable($tmpdir)) {
1786+
$value = var_export($value, true);
1787+
// HHVM fails at __set_state, so just use object cast for now
1788+
$value = str_replace('stdClass::__set_state', '(object)', $value);
1789+
// Write to temp file first to ensure atomicity
1790+
$tmp = $tmpdir . DIRECTORY_SEPARATOR . $key . "." . uniqid('', true) . '.tmp';
1791+
file_put_contents($tmp, '<?php $val = ' . $value . ';', LOCK_EX);
1792+
rename($tmp, $tmpdir . DIRECTORY_SEPARATOR . $key);
1793+
1794+
} else if($ttl == null) {
17731795
$ttl = (!empty($this->db->ttl)) ? $this->db->ttl : $this->ttl;
17741796
}
1775-
17761797
$key = 'api_' . $key;
17771798

17781799
if(extension_loaded('apc') && (ini_get('apc.enabled') == 1)) {
17791800
return apc_store($key, $value, $ttl);
17801801
}
17811802

1782-
$this->cache[$key] = $value;
1783-
17841803
return true;
17851804
}
17861805
}

0 commit comments

Comments
 (0)