cache.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # frozen_string_literal: true
  2. require_relative 'base'
  3. module Mastodon::CLI
  4. class Cache < Base
  5. desc 'clear', 'Clear out the cache storage'
  6. def clear
  7. Rails.cache.clear
  8. say('OK', :green)
  9. end
  10. option :concurrency, type: :numeric, default: 5, aliases: [:c]
  11. option :verbose, type: :boolean, aliases: [:v]
  12. desc 'recount TYPE', 'Update hard-cached counters'
  13. long_desc <<~LONG_DESC
  14. Update hard-cached counters of TYPE by counting referenced
  15. records from scratch. TYPE can be "accounts" or "statuses".
  16. It may take a very long time to finish, depending on the
  17. size of the database.
  18. LONG_DESC
  19. def recount(type)
  20. case type
  21. when 'accounts'
  22. processed, = parallelize_with_progress(accounts_with_stats) do |account|
  23. recount_account_stats(account)
  24. end
  25. when 'statuses'
  26. processed, = parallelize_with_progress(statuses_with_stats) do |status|
  27. recount_status_stats(status)
  28. end
  29. else
  30. say("Unknown type: #{type}", :red)
  31. exit(1)
  32. end
  33. say
  34. say("OK, recounted #{processed} records", :green)
  35. end
  36. private
  37. def accounts_with_stats
  38. Account.local.includes(:account_stat)
  39. end
  40. def statuses_with_stats
  41. Status.includes(:status_stat)
  42. end
  43. def recount_account_stats(account)
  44. account.account_stat.tap do |account_stat|
  45. account_stat.following_count = account.active_relationships.count
  46. account_stat.followers_count = account.passive_relationships.count
  47. account_stat.statuses_count = account.statuses.where.not(visibility: :direct).count
  48. account_stat.save if account_stat.changed?
  49. end
  50. end
  51. def recount_status_stats(status)
  52. status.status_stat.tap do |status_stat|
  53. status_stat.replies_count = status.replies.where.not(visibility: :direct).count
  54. status_stat.reblogs_count = status.reblogs.count
  55. status_stat.favourites_count = status.favourites.count
  56. status_stat.save if status_stat.changed?
  57. end
  58. end
  59. end
  60. end