public static function make($name, Database $model, $overAge = false) { $name = Inflector::urlize($name, '.'); $ageChange = $model->cache()->get(sha1($model->dir)); $ageChange = strlen($ageChange) ? $ageChange : 0; $hash = "dbjson::cachedQueries::{$model->db}::{$model->table}"; $cachedAge = "dbjson::cachedQueries::{$model->db}::{$model->table}::{$name}::age"; $ageCached = $model->cache()->get($cachedAge); $ageCached = strlen($ageCached) ? $ageCached : 0; $cached = $model->cache()->hget($hash, $name); if (strlen($cached)) { if ($ageCached < $ageChange) { if ($overAge) { return new Cachedresults($cached); } else { $model->cache()->hdel($hash, $name); $model->cache()->del($cachedAge); } } else { return new Cachedresults($cached); } } Event::listen("{$model->db}.{$model->table}.put.in.cache", function ($collection) use($model, $cachedAge, $hash, $name) { $model->cache()->hset($hash, $name, serialize($collection)); $model->cache()->set($cachedAge, time()); }); return $model; }
public function find($id, $object = true) { $start = $this->getTime(); if (!is_numeric($id)) { return $object ? null : []; } if ($id <= 0) { return $object ? null : []; } $key = "lock::{$this->db}::{$this->table}::read"; $hooks = $this->hooks(); $before = isAke($hooks, 'before_read', false); $after = isAke($hooks, 'after_read', false); if (true === $this->cacheEnabled) { $lock = strlen($this->cache()->get($key)) > 0 ? true : false; if (true === $lock) { throw new Exception("This table {$this->db} {$this->table} is locked to read."); } } $file = $this->dir . DS . $id . '.row'; $row = $this->readFile($file); $customFields = isAke($hooks, 'custom_fields', false); if (false !== $before) { $before($id); Event::run("{$this->db}.{$this->table}.before.read", [$id]); } if (strlen($row)) { $tab = json_decode($row, true); if (true === $customFields) { $tab = array_merge($tab, customFields($this->table, $tab['id'])); } $data = $object ? $this->row($tab) : $tab; if (false !== $after) { $backup = $data; $data = $after($data); $data = empty($data) ? $backup : $data; if (true === Event::listeners("{$this->db}.{$this->table}.after.read")) { $data = Event::run("{$this->db}.{$this->table}.after.read", [$data]); } } return $data; } $this->countQuery($start); return $object ? null : []; }
/** * Log the query and fire the core query event. * * @param string $sql * @param array $bindings * @param int $time * @return void */ protected function log($sql, $bindings, $time) { Event::fire('Query', array($sql, $bindings, $time)); static::$queries = compact('sql', 'bindings', 'time'); }