@@ -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