PageRenderTime 54ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/surosql/SurosqlPDOCache.class.php

https://bitbucket.org/SuRaMoN/surosql
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
  1. <?php
  2. // version: 2.0.1
  3. namespace ns;
  4. class SurosqlPDOCache {
  5. static function register($osql = null, $suracache = null) {
  6. $osql = is_null($osql) ? SurosqlPDO::$osql : $osql;
  7. $suracache = is_null($suracache) ? suracache() : $suracache;
  8. if(!isset($osql->pdo)) {
  9. throw new Exception('Must register pdo plugin to surosql first');
  10. }
  11. $osql->suracache = $suracache;
  12. foreach(array('var', 'all', 'col', 'first') as $funcname) {
  13. $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));");
  14. }
  15. $osql->imported_query_functions['cache_options'] = array(__CLASS__, 'set_cache_options');
  16. }
  17. static function set_cache_options($query, $options) {
  18. $query->suracache_options = $options;
  19. }
  20. static function cached_operator($orig_func, $query) {
  21. $args = func_get_args();
  22. $key = array($query->sql(), $orig_func, array_slice($args, 2));
  23. $suracache = $query->osql->suracache;
  24. $options = isset($query->suracache_options) ? $query->suracache_options : array();
  25. $cache_status = $suracache->get_var_status($key);
  26. if($cache_status->cache_available) {
  27. return $cache_status->content;
  28. }
  29. $content = call_user_func(array($query, $orig_func), array_slice($args, 2));
  30. $suracache->cache_var($key, $content, $options);
  31. return $content;
  32. }
  33. }
  34. ?>