/surosql/SurosqlPDOCache.class.php
PHP | 47 lines | 33 code | 13 blank | 1 comment | 2 complexity | a68d9df55ff82b7e541442f1ab9e2144 MD5 | raw file
Possible License(s): LGPL-3.0, AGPL-1.0, GPL-2.0, LGPL-2.1
- <?php
-
- // version: 2.0.1
-
- namespace ns;
-
-
- class SurosqlPDOCache {
-
- static function register($osql = null, $suracache = null) {
- $osql = is_null($osql) ? SurosqlPDO::$osql : $osql;
- $suracache = is_null($suracache) ? suracache() : $suracache;
-
- if(!isset($osql->pdo)) {
- throw new Exception('Must register pdo plugin to surosql first');
- }
-
- $osql->suracache = $suracache;
-
- foreach(array('var', 'all', 'col', 'first') as $funcname) {
- $osql->imported_query_functions["cached_$funcname"] = create_function('', "\$args = func_get_args(); return call_user_func_array('" . addslashes(__CLASS__) . "::cached_operator', array_merge(array('$funcname'), \$args));");
- }
- $osql->imported_query_functions['cache_options'] = array(__CLASS__, 'set_cache_options');
- }
-
- static function set_cache_options($query, $options) {
- $query->suracache_options = $options;
- }
-
- static function cached_operator($orig_func, $query) {
- $args = func_get_args();
- $key = array($query->sql(), $orig_func, array_slice($args, 2));
- $suracache = $query->osql->suracache;
- $options = isset($query->suracache_options) ? $query->suracache_options : array();
-
- $cache_status = $suracache->get_var_status($key);
- if($cache_status->cache_available) {
- return $cache_status->content;
- }
-
- $content = call_user_func(array($query, $orig_func), array_slice($args, 2));
- $suracache->cache_var($key, $content, $options);
- return $content;
- }
- }
-
- ?>