Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Lib;
- if(!class_exists('\\RedBeanPHP\\SimpleModel', false))
- require_once APPPATH . 'third_party' . DS . 'RedBean' . DS . 'rb.php';
- use App\Core\AppCacheTrait;
- /**
- * Caches queries.
- *
- * @version 0.2
- */
- class CachedOodb extends \RedBeanPHP\OODB implements \RedBeanPHP\Plugin
- {
- use AppCacheTrait;
- /**
- * @var integer
- */
- protected $hits = 0;
- /**
- * @var integer
- */
- protected $misses = 0;
- /**
- * Constructor
- */
- public function __construct(\RedBeanPHP\QueryWriter $writer)
- {
- parent::__construct($writer);
- $this->_ci =& get_instance();
- }
- public function find($type, $conditions = array(), $sql = NULL, $bindings = array())
- {
- $enabled = false;
- if($enabled)
- {
- $key = $type . '_' . md5($type . '_' . implode('_', $conditions) . '_' . $sql);
- if(!($ret = $this->_cache($key, ['group' => $type])))
- {
- $ret = parent::find( $type, $conditions, $sql, $bindings );
- $this->_cache($key, ['value' => $ret, 'group' => $type]);
- }
- return $ret;
- }
- return parent::find( $type, $conditions, $sql, $bindings );
- }
- /**
- * Loads a bean by type and id. If the bean cannot be found an
- * empty bean will be returned instead. This is a cached version
- * of the loader, if the bean has been cached it will be served
- * from cache, otherwise the bean will be retrieved from the database
- * as usual an a new cache entry will be added..
- *
- * @param string $type type of bean you are looking for
- * @param integer $id identifier of the bean
- *
- * @return \RedBeanPHP\OODBBean $bean the bean object found
- */
- public function load( $type, $id )
- {
- $bean = null;
- if ($bean = $this->_cache($type.'_load_'.$id, ['group' => $type]))
- {
- $this->hits++;
- }
- else
- {
- $this->misses++;
- $bean = parent::load( $type, $id );
- if ( $bean->id )
- $this->_cache($type.'_load_'.$id, ['value' => $bean, 'group' => $type]);
- }
- return $bean;
- }
- /**
- * Stores a RedBean OODBBean and caches it.
- *
- * @param \RedBeanPHP\OODBBean $bean the bean you want to store
- *
- * @return mixed
- */
- public function store( $bean )
- {
- $id = parent::store( $bean );
- $type = $bean->getMeta( 'type' );
- $this->_clearCacheGroup($type);
- $this->_cache($type.'_load_'.$id, ['value' => $bean, 'group' => $type]);
- return $id;
- }
- /**
- * Trashes a RedBean OODBBean and removes it from cache.
- *
- * @param OODBBean $bean bean
- *
- * @return mixed
- */
- public function trash( $bean )
- {
- $type = $bean->getMeta( 'type' );
- $id = $bean->id;
- $ret = parent::trash( $bean );
- $this->_clearCacheGroup($type);
- return $ret;
- }
- /**
- * Flushes the cache completely.
- *
- * @return RedBean_Plugin_Cache
- */
- public function flushAll()
- {
- $this->_cleanCache();
- return $this;
- }
- /**
- * Returns the number of hits. If a call to load() or
- * batch() can use the cache this counts as a hit.
- * Otherwise it's a miss.
- *
- * @return integer
- */
- public function getHits()
- {
- return $this->hits;
- }
- /**
- * Returns the number of hits. If a call to load() or
- * batch() can use the cache this counts as a hit.
- * Otherwise it's a miss.
- *
- * @return integer
- */
- public function getMisses()
- {
- return $this->misses;
- }
- /**
- * Resets hits counter to 0.
- */
- public function resetHits()
- {
- $this->hits = 0;
- }
- /**
- * Resets misses counter to 0.
- */
- public function resetMisses()
- {
- $this->misses = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement