/** * Function remove all redis data for specified language * * @param $language */ public function clear($language) { if (Redis::del(Key::posts($language))) { $this->info('Remove all entries for key ' . Key::posts($language)); } if (Redis::del(Key::tags($language))) { $this->info('Remove all entries for key ' . Key::tags($language)); } foreach (Redis::keys(Key::post('*', $language)) as $key) { if (Redis::del($key)) { $this->info('Remove all entries for keys ' . $key); } } }
public function byTag($tag) { $posts = Redis::smembers(Key::tags($tag)); foreach ($posts as &$post) { $post = $this->loader->load($post); } uasort($posts, function ($a, $b) { return $a['published'] > $b['published'] ? -1 : 1; }); $total = count($posts); $amount = config('blog.posts_on_page'); $current = Request::get('page') ?: 1; $start = $amount * $current - $amount; return (new LengthAwarePaginator(array_slice($posts, $start, $amount), $total, $amount, $current))->setPath($tag); }
public function load($slug, array $file = []) { if (!($post = Redis::hgetall(Key::post($slug, $this->language)))) { if (!$file) { $list = $this->loadList(); $file = array_key_exists($slug, $list) ? $list[$slug] : null; } if (!$file) { return null; } $processed = self::process((new Filesystem())->get($file['path'])); $post = array_merge($file, $processed); $post['published'] = $post['published']->format(\DateTime::ISO8601); $post['tags'] = json_encode($post['tags']); foreach ($post as $field => $value) { Redis::hsetnx(Key::post($slug, $this->language), $field, $value); } } $post['published'] = new \DateTime($post['published']); $post['tags'] = json_decode($post['tags']); return $post; }
public function all($limit = 10) { if (!($tagKeys = Redis::keys("tags:{$this->language}:*"))) { $posts = $this->loader->loadList(); foreach ($posts as $item) { $post = $this->loader->load($item['slug'], $item); foreach ($post['tags'] as $tag) { if (Redis::sadd(Key::tags($tag), $item['slug']) && !in_array(Key::tags($tag), $tagKeys)) { $tagKeys[] = Key::tags($tag); } } } } $tags = []; foreach ($tagKeys as $key) { $chunks = explode(':', $key); $key = array_pop($chunks); $tags[$key] = Redis::scard(Key::tags($key)); } arsort($tags); return array_slice($tags, 0, $limit); }