Преглед на файлове

Add pagination by max_id instead of offset/limit

master
Eugen Rochko преди 8 години
родител
ревизия
12559b01ea
променени са 7 файла, в които са добавени 19 реда и са изтрити 10 реда
  1. +1
    -1
      app/controllers/api/accounts_controller.rb
  2. +2
    -2
      app/controllers/api/statuses_controller.rb
  3. +3
    -3
      app/models/feed.rb
  4. +3
    -2
      app/models/status.rb
  5. +1
    -1
      app/services/fan_out_on_write_service.rb
  6. +1
    -1
      app/services/precompute_feed_service.rb
  7. +8
    -0
      lib/tasks/feeds.rake

+ 1
- 1
app/controllers/api/accounts_controller.rb Целия файл

@@ -15,7 +15,7 @@ class Api::AccountsController < ApiController
end

def statuses
@statuses = @account.statuses.with_includes.with_counters.order('created_at desc')
@statuses = @account.statuses.with_includes.with_counters.paginate_by_max_id(20, params[:max_id])
end

def follow


+ 2
- 2
app/controllers/api/statuses_controller.rb Целия файл

@@ -23,11 +23,11 @@ class Api::StatusesController < ApiController

def home
feed = Feed.new(:home, current_user.account)
@statuses = feed.get(20, (params[:offset] || 0).to_i)
@statuses = feed.get(20, params[:max_id] || '+inf')
end

def mentions
feed = Feed.new(:mentions, current_user.account)
@statuses = feed.get(20, (params[:offset] || 0).to_i)
@statuses = feed.get(20, params[:max_id] || '+inf')
end
end

+ 3
- 3
app/models/feed.rb Целия файл

@@ -4,12 +4,12 @@ class Feed
@account = account
end

def get(limit, offset = 0)
unhydrated = redis.zrevrange(key, offset, limit)
def get(limit, max_id = '+inf')
unhydrated = redis.zrevrangebyscore(key, "(#{max_id}", '-inf', limit: [0, limit])
status_map = Hash.new

# If we're after most recent items and none are there, we need to precompute the feed
return PrecomputeFeedService.new.(@type, @account).take(limit) if unhydrated.empty? && offset == 0
return PrecomputeFeedService.new.(@type, @account).take(limit) if unhydrated.empty? && max_id == '+inf'

Status.where(id: unhydrated).with_includes.with_counters.each { |status| status_map[status.id.to_s] = status }
return unhydrated.map { |id| status_map[id] }.compact


+ 3
- 2
app/models/status.rb Целия файл

@@ -15,8 +15,9 @@ class Status < ActiveRecord::Base
validates :uri, uniqueness: true, unless: 'local?'
validates :text, presence: true, if: Proc.new { |s| s.local? && !s.reblog? }

scope :with_counters, -> { select('statuses.*, (select count(r.id) from statuses as r where r.reblog_of_id = statuses.id) as reblogs_count, (select count(f.id) from favourites as f where f.status_id = statuses.id) as favourites_count') }
scope :with_includes, -> { includes(:account, reblog: :account, thread: :account) }
scope :with_counters, -> { select('statuses.*, (select count(r.id) from statuses as r where r.reblog_of_id = statuses.id) as reblogs_count, (select count(f.id) from favourites as f where f.status_id = statuses.id) as favourites_count') }
scope :with_includes, -> { includes(:account, reblog: :account, thread: :account) }
scope :paginate_by_max_id, -> (limit, max_id) { order('id desc').limit(limit).where('id < ?', max_id) }

def local?
self.uri.nil?


+ 1
- 1
app/services/fan_out_on_write_service.rb Целия файл

@@ -31,7 +31,7 @@ class FanOutOnWriteService < BaseService
end

def push(type, receiver_id, status)
redis.zadd(key(type, receiver_id), status.created_at.to_i, status.id)
redis.zadd(key(type, receiver_id), status.id, status.id)
trim(type, receiver_id)
end



+ 1
- 1
app/services/precompute_feed_service.rb Целия файл

@@ -14,7 +14,7 @@ class PrecomputeFeedService < BaseService
private

def push(type, receiver_id, status)
redis.zadd(key(type, receiver_id), status.created_at.to_i, status.id)
redis.zadd(key(type, receiver_id), status.id, status.id)
end

def home(account)


+ 8
- 0
lib/tasks/feeds.rake Целия файл

@@ -0,0 +1,8 @@
namespace :feeds do

desc "Removes all feeds from Redis, forcing a precompute on next request for each user"
task clear: :environment do
$redis.keys('feed:*').each { |key| $redis.del(key) }
end

end

Зареждане…
Отказ
Запис