Advertisement
pushrbx

RedBeanPHP QueryCache Plugin

Aug 16th, 2014
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.47 KB | None | 0 0
  1. <?php
  2. namespace App\Lib;
  3.  
  4. if(!class_exists('\\RedBeanPHP\\SimpleModel', false))
  5.     require_once APPPATH . 'third_party' . DS . 'RedBean' . DS . 'rb.php';
  6.  
  7. use App\Core\AppCacheTrait;
  8.  
  9. /**
  10.  * Caches queries.
  11.  *
  12.  * @version 0.2
  13.  */
  14. class CachedOodb extends \RedBeanPHP\OODB implements \RedBeanPHP\Plugin
  15. {
  16.  
  17.     use AppCacheTrait;
  18.  
  19. /**
  20.  * @var integer
  21.  */
  22.     protected $hits = 0;
  23.  
  24. /**
  25.  * @var integer
  26.  */
  27.     protected $misses = 0;
  28.  
  29. /**
  30.  * Constructor
  31.  */
  32.     public function __construct(\RedBeanPHP\QueryWriter $writer)
  33.     {
  34.         parent::__construct($writer);
  35.         $this->_ci =& get_instance();
  36.     }
  37.  
  38.     public function find($type, $conditions = array(), $sql = NULL, $bindings = array())
  39.     {
  40.         $enabled = false;
  41.  
  42.         if($enabled)
  43.         {
  44.             $key = $type . '_' . md5($type . '_' . implode('_', $conditions) . '_' . $sql);
  45.             if(!($ret = $this->_cache($key, ['group' => $type])))
  46.             {
  47.                 $ret = parent::find( $type, $conditions, $sql, $bindings );
  48.                 $this->_cache($key, ['value' => $ret, 'group' => $type]);
  49.             }
  50.  
  51.             return $ret;
  52.         }
  53.  
  54.         return parent::find( $type, $conditions, $sql, $bindings );
  55.     }
  56.  
  57. /**
  58.  * Loads a bean by type and id. If the bean cannot be found an
  59.  * empty bean will be returned instead. This is a cached version
  60.  * of the loader, if the bean has been cached it will be served
  61.  * from cache, otherwise the bean will be retrieved from the database
  62.  * as usual an a new cache entry will be added..
  63.  *
  64.  * @param string  $type type of bean you are looking for
  65.  * @param integer $id   identifier of the bean
  66.  *
  67.  * @return \RedBeanPHP\OODBBean $bean the bean object found
  68.  */
  69.     public function load( $type, $id )
  70.     {
  71.         $bean = null;
  72.  
  73.         if ($bean = $this->_cache($type.'_load_'.$id, ['group' => $type]))
  74.         {
  75.             $this->hits++;
  76.         }
  77.         else
  78.         {
  79.             $this->misses++;
  80.  
  81.             $bean = parent::load( $type, $id );
  82.  
  83.             if ( $bean->id )
  84.                 $this->_cache($type.'_load_'.$id, ['value' => $bean, 'group' => $type]);
  85.         }
  86.  
  87.         return $bean;
  88.     }
  89.  
  90.     /**
  91.      * Stores a RedBean OODBBean and caches it.
  92.      *
  93.      * @param \RedBeanPHP\OODBBean $bean the bean you want to store
  94.      *
  95.      * @return mixed
  96.      */
  97.     public function store( $bean )
  98.     {
  99.         $id   = parent::store( $bean );
  100.         $type = $bean->getMeta( 'type' );
  101.  
  102.         $this->_clearCacheGroup($type);
  103.         $this->_cache($type.'_load_'.$id, ['value' => $bean, 'group' => $type]);
  104.  
  105.         return $id;
  106.     }
  107.  
  108.     /**
  109.      * Trashes a RedBean OODBBean and removes it from cache.
  110.      *
  111.      * @param OODBBean $bean bean
  112.      *
  113.      * @return mixed
  114.      */
  115.     public function trash( $bean )
  116.     {
  117.         $type = $bean->getMeta( 'type' );
  118.         $id   = $bean->id;
  119.  
  120.         $ret = parent::trash( $bean );
  121.         $this->_clearCacheGroup($type);
  122.  
  123.         return $ret;
  124.     }
  125.  
  126.     /**
  127.      * Flushes the cache completely.
  128.      *
  129.      * @return RedBean_Plugin_Cache
  130.      */
  131.     public function flushAll()
  132.     {
  133.         $this->_cleanCache();
  134.  
  135.         return $this;
  136.     }
  137.  
  138.     /**
  139.      * Returns the number of hits. If a call to load() or
  140.      * batch() can use the cache this counts as a hit.
  141.      * Otherwise it's a miss.
  142.      *
  143.      * @return integer
  144.      */
  145.     public function getHits()
  146.     {
  147.         return $this->hits;
  148.     }
  149.  
  150. /**
  151.  * Returns the number of hits. If a call to load() or
  152.  * batch() can use the cache this counts as a hit.
  153.  * Otherwise it's a miss.
  154.  *
  155.  * @return integer
  156.  */
  157.     public function getMisses()
  158.     {
  159.         return $this->misses;
  160.     }
  161.  
  162. /**
  163.  * Resets hits counter to 0.
  164.  */
  165.     public function resetHits()
  166.     {
  167.         $this->hits = 0;
  168.     }
  169.  
  170. /**
  171.  * Resets misses counter to 0.
  172.  */
  173.     public function resetMisses()
  174.     {
  175.         $this->misses = 0;
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement