/** * Garbage collector * * @param int $mode - 1 - old, 2 - all * @param array $tags * @return boolean */ public function gc($mode = 1, $tags = []) { if (($cookies = glob($this->options['dir'] . 'cache--cookie--*')) === false) { return false; } $time = time(); $tags = array_fix($tags); foreach ($cookies as $file) { $flag_delete = false; do { if (!is_file($file)) { break; } $cookie = unserialize(helper_file::read($file)); // if we delete all caches if ($mode == 2) { $flag_delete = true; break; } // processing tags if (!empty($tags) && !empty($cookie['tags'])) { $temp = current($cookie['tags']); if (is_array($temp)) { $cookie['tags'] = $temp; } if (array_intersect($tags, $cookie['tags'])) { $flag_delete = true; break; } } // if file expired if ($time > $cookie['expire']) { $flag_delete = true; break; } } while (0); // if we need to delete if ($flag_delete) { unlink($file); unlink($cookie['file']); } } return true; }
/** * Remove a token from array * * @param array $tokens * @param string $token * @return array */ function array_remove_token($tokens, $token) { $tokens = array_fix($tokens); foreach ($tokens as $k => $v) { if ($v == $token) { unset($tokens[$k]); } } return $tokens; }
/** * Garbage collector * * @param int $mode - 1 - old, 2 - all * @param array $tags * @return boolean */ public function gc($mode = 1, $tags = []) { if ($mode == 2) { $sql = 'DELETE FROM ' . $this->model_cache->name; } else { if ($mode == 1) { $sql = 'DELETE FROM ' . $this->model_cache->name . ' WHERE sm_cache_expire < \'' . format::now('timestamp') . '\''; if (!empty($tags)) { $tags2 = array_fix($tags); $temp = []; foreach ($tags2 as $v) { $temp[] = "sm_cache_tags LIKE '% {$v} %'"; } $sql .= ' OR (' . implode(' OR ', $temp) . ')'; } } } $db = new db($this->model_cache->db_link); $result = $db->query($sql); return $result['success']; }
/** * Save row to database * * @param string $table * @param array $data * @param mixed $keys * @param array $options * @return boolean */ public function save($table, $data, $keys, $options = []) { do { // fixing keys $keys = array_fix($keys); // where clause $where = []; $empty = true; foreach ($keys as $key) { if (!empty($data[$key])) { $empty = false; } $where[$key] = array_key_exists($key, $data) ? $data[$key] : null; } // if keys are empty we must insert $row_found = false; if (!$empty) { $result = $this->query("SELECT * FROM {$table} WHERE " . $this->prepare_condition($where, 'AND')); if (!$result['success']) { break; } else { if ($result['num_rows']) { $row_found = true; } } } // if we are in inser mode we exit if ($row_found && !empty($options['flag_insert_only'])) { $result['success'] = true; break; } // if row found we update if ($row_found) { $flag_inserted = false; $sql = "UPDATE {$table} SET " . $this->prepare_condition($data, ', ') . ' WHERE ' . $this->prepare_condition($where, 'AND'); } else { $flag_inserted = true; // we need to unset key fields if ($empty) { foreach ($keys as $key) { unset($data[$key]); } } // if we have a sequence if (!empty($options['sequences'])) { foreach ($options['sequences'] as $k => $v) { if (empty($data[$k])) { $temp = $this->sequence($v['sequence_name']); $data[$k] = $temp['rows'][0]['counter']; } } } // we insert $sql = "INSERT INTO {$table} (" . $this->prepare_expression(array_keys($data)) . ') VALUES (' . $this->prepare_values($data) . ')'; } $result = $this->query($sql, $this->prepare_keys($keys)); if ($result['success']) { $result['inserted'] = $flag_inserted; } // processing returning clause last $temp = $this->query("SELECT * FROM {$table} WHERE " . $this->prepare_condition($where, 'AND')); if ($temp['success']) { $result['rows'] = $temp['rows']; $result['num_rows'] = $temp['num_rows']; } } while (0); return $result; }