public static function fast_count($tags = null) { # A small sanitation $tags = preg_replace('/ +/', ' ', trim($tags)); $cache_version = (int) Rails::cache()->read('$cache_version'); $key = ['post_count' => $tags, 'v' => $cache_version]; $count = (int) Rails::cache()->fetch($key, function () use($tags) { list($sql, $params) = Post::generate_sql($tags, ['count' => true]); array_unshift($params, $sql); return Post::countBySql($params); }); return $count; # This is just too brittle, and hard to make work with other features that may # hide posts from the index. # if tags.blank? # return select_value_sql("SELECT row_count FROM table_data WHERE name = 'posts'").to_i # else # c = select_value_sql("SELECT post_count FROM tags WHERE name = ?", tags).to_i # if c == 0 # return Post.count_by_sql(Post.generate_sql(tags, :count => true)) # else # return c # end # end }
public function get_translated_formatted_body($target_lang, array $source_langs) { $source_lang_list = implode(',', $source_langs); $key = "comment:" . $this->id . ":" . strtotime($this->updated_at) . ":" . $target_lang . ":" . $source_lang_list; return Rails::cache()->fetch($key, function () use($target_lang, $source_langs) { return $this->get_translated_formatted_body_uncached($target_lang, $source_langs); }); }
public static function new_deleted_posts($user) { if ($user->is_anonymous()) { return 0; } return Rails::cache()->fetch('deleted_posts:' . $user->id . ':' . $user->last_deleted_post_seen_at, ['expires_in' => '1 minute'], function () use($user) { return self::connection()->selectValue("SELECT COUNT(*) FROM flagged_post_details fpd JOIN posts p ON (p.id = fpd.post_id) " . "WHERE p.status = 'deleted' AND p.user_id = ? AND fpd.user_id <> ? AND fpd.created_at > ?", $user->id, $user->id, $user->last_deleted_post_seen_at); }); }
</ul> </li> <li class="static"><?php echo $this->linkTo($this->t('.more'), ['controller' => 'static', 'action' => 'more']); ?> </li> <li class="has-mail"> <?php echo $this->linkTo($this->t('.new_mail'), ['controller' => 'dmail', 'action' => 'inbox'], ['id' => 'has-mail-notice']); ?> </li> </ul> <span id='cn' style="display: none;"> </span> <?php $menu = ob_get_clean(); Rails::cache()->write($key, $menu); } if (!current_user()->is_anonymous()) { $menu = substr_replace($menu, current_user()->id, strpos($menu, '-user.id-'), 9); echo substr_replace($menu, current_user()->name, strpos($menu, '-user.name-'), 11); } else { echo $menu; } ?> </div>
public function execute_calculate_tag_subscriptions() { if (Rails::cache()->read("delay-tag-sub-calc")) { return; } Rails::cache()->write("delay-tag-sub-calc", ['expires_in' => '360 minutes']); TagSubscription::process_all(); $this->updateAttributes(['data' => ['last_run' => date('Y-m-d H:i')]]); }
public function held_post_count() { $version = (int) Rails::cache()->read('$cache_version'); $key = 'held-post-count/v=' . $version . '/u=' . $this->id; return Rails::cache()->fetch($key, function () { return Post::where(['user_id' => $this->id, 'is_held' => true])->where('status <> ?', 'deleted')->count(); }); }
public static function expire_tag_version() { # $tag_version is bumped when the type of a tag is changed in Tags, if # a new tag is created, or if a tag's post_count becomes nonzero. \Rails::cache()->write('$tag_version', time()); }
public function index() { $tags = $this->params()->tags; $split_tags = $tags ? array_filter(explode(' ', $tags)) : array(); $page = $this->page_number(); $this->tag_suggestions = $this->searching_pool = array(); /* if $this->current_user.is_member_or_lower? && count(split_tags) > 2 # $this->respond_to_error("You can only search up to two tags at once with a basic account", 'action' => "error") # return; # elseif count(split_tags) > 6 */ if (count($split_tags) > CONFIG()->tag_query_limit) { $this->respond_to_error("You can only search up to " . CONFIG()->tag_query_limit . " tags at once", "#error"); return; } $q = Tag::parse_query($tags); $limit = (int) $this->params()->limit; isset($q['limit']) && ($limit = (int) $q['limit']); $limit <= 0 && ($limit = CONFIG()->post_index_default_limit); $limit > 1000 && ($limit = 1000); $count = 0; $this->set_title("/" . str_replace("_", " ", $tags)); // try { $count = Post::fast_count($tags); // vde($count); // } catch(Exception $x) { // $this->respond_to_error("Error: " . $x->getMessage(), "#error"); // return; // } $this->ambiguous_tags = Tag::select_ambiguous($split_tags); if (isset($q['pool']) and is_int($q['pool'])) { $this->searching_pool = Pool::where(['id' => $q['pool']])->first(); } $from_api = $this->params()->format == "json" || $this->params()->format == "xml"; // $this->posts = Post::find_all(array('page' => $page, 'per_page' => $limit, $count)); // $this->posts = WillPaginate::Collection.new(page, limit, count); // $offset = $this->posts->offset(); // $posts_to_load = $this->posts->per_page(); $per_page = $limit; $offset = ($page - 1) * $per_page; $posts_to_load = $per_page; if (!$from_api) { # For forward preloading: // $posts_to_load += $this->posts->per_page(); $posts_to_load += $per_page; # If we're not on the first page, load the previous page for prefetching. Prefetching # the previous page when the user is scanning forward should be free, since it'll already # be in cache, so this makes scanning the index from back to front as responsive as from # front to back. if ($page and $page > 1) { // $offset -= $this->posts->per_page(); // $posts_to_load += $this->posts->per_page(); $offset -= $per_page; $posts_to_load += $per_page; } } $this->showing_holds_only = isset($q['show_holds']) && $q['show_holds'] == 'only'; list($sql, $params) = Post::generate_sql($q, array('original_query' => $tags, 'from_api' => $from_api, 'order' => "p.id DESC", 'offset' => $offset, 'limit' => $posts_to_load)); $results = Post::findBySql($sql, $params); $this->preload = new Rails\ActiveRecord\Collection(); if (!$from_api) { if ($page && $page > 1) { $this->preload = $results->slice(0, $limit); $results = $results->slice($limit); } $this->preload->merge($results->slice($limit)); $results = $results->slice(0, $limit); } # Apply can_be_seen_by filtering to the results. For API calls this is optional, and # can be enabled by specifying filter=1. if (!$from_api or $this->params()->filter) { $results->deleteIf(function ($post) { return !$post->can_be_seen_by(current_user(), array('show_deleted' => true)); }); $this->preload->deleteIf(function ($post) { return !$post->can_be_seen_by(current_user()); }); } if ($from_api and $this->params()->api_version == "2" and $this->params()->format != "json") { $this->respond_to_error("V2 API is JSON-only", array(), array('status' => 424)); return; } $this->posts = new Rails\ActiveRecord\Collection($results->members(), ['page' => $page, 'perPage' => $per_page, 'totalRows' => $count]); if ($count < CONFIG()->post_index_default_limit && count($split_tags) == 1) { $this->tag_suggestions = Tag::find_suggestions($tags); } // exit; $this->respondTo(array('html' => function () use($split_tags, $tags) { if ($split_tags) { $this->tags = Tag::parse_query($tags); } else { $this->tags = Rails::cache()->fetch('$poptags', ['expires_in' => '1 hour'], function () { return array('include' => Tag::count_by_period(date('Y-m-d', strtotime('-' . CONFIG()->post_index_tags_limit)), date('Y-m-d H:i:s'), array('limit' => 25, 'exclude_types' => CONFIG()->exclude_from_tag_sidebar))); }); } }, 'xml' => function () { $this->setLayout(false); }, 'json' => function () { if ($this->params()->api_version != "2") { $this->render(array('json' => array_map(function ($p) { return $p->api_attributes(); }, $this->posts->members()))); return; } $api_data = Post::batch_api_data($this->posts->members(), array('exclude_tags' => $this->params()->include_tags != "1", 'exclude_votes' => $this->params()->include_votes != "1", 'exclude_pools' => $this->params()->include_pools != "1", 'fake_sample_url' => CONFIG()->fake_sample_url)); $this->render(array('json' => json_encode($api_data))); })); }
protected function clear_cache() { Rails::cache()->delete("forum_posts"); }
public static function get_json_summary() { $summary_version = self::get_summary_version(); $key = 'tag_summary/' . $summary_version; $data = Rails::cache()->fetch($key, ['expires_in' => '1 hour'], function () { $data = Tag::get_json_summary_no_cache(); return json_encode($data); }); return $data; }
public static function cache() { if (!self::$cache) { self::$cache = new Rails\Cache\Cache(self::application()->config()->cache_store); } return self::$cache; }
public function resetPostStats() { $keys = []; foreach ([0, 20, 30, 35, 40, 50] as $level) { $keys[] = "stats/count/level=" . $level; foreach ([0, 1, 2, 3, 4, 5] as $tag_count) { $keys[] = "stats/tags/level=" . $level . "&tags=" . $tag_count; } $keys[] = "stats/page/level={$level}&page=0-10"; $keys[] = "stats/page/level={$level}&page=10-20"; $keys[] = "stats/page/level={$level}&page=20+"; } foreach ($keys as $key) { Rails::cache()->write($key, 0); } $this->redirectTo('#cache_stats'); }