[Misc] Remove memoist gem dependency

The author has passed away and  some functionality is broken with recent
ruby versions already. We only use a subset of it, so it's easy to replace.

Some places:
* Didn't even use it
* Used it but the method was already cached through memcached
* Just don't need it

Some places I question if it is needed but I'll just leave them there
since I don't want to benchmark
This commit is contained in:
Earlopain 2023-11-08 22:21:11 +01:00
parent e4ce4f312f
commit f8098721ec
No known key found for this signature in database
GPG Key ID: 48860312319ADF61
10 changed files with 52 additions and 87 deletions

View File

@ -13,7 +13,6 @@ gem 'draper'
gem 'streamio-ffmpeg'
gem 'responders'
gem 'dtext_rb', :git => "https://github.com/e621ng/dtext_rb.git", branch: "master", :require => "dtext"
gem 'memoist'
gem 'bootsnap'
gem 'addressable'
gem 'httparty'

View File

@ -175,7 +175,6 @@ GEM
mailgun-ruby (1.2.10)
rest-client (>= 2.0.2)
marcel (1.0.2)
memoist (0.16.2)
method_source (1.0.0)
mime-types (3.5.0)
mime-types-data (~> 3.2015)
@ -385,7 +384,6 @@ DEPENDENCIES
listen
mailgun-ruby
marcel
memoist
mocha
newrelic_rpm
opensearch-ruby

View File

@ -16,8 +16,6 @@ module Sources
class Base
attr_reader :url, :urls, :parsed_url
extend Memoist
# * <tt>url</tt> - Should point to a resource suitable for
# downloading. This may sometimes point to the binary file.
# It may also point to the artist's profile page, in cases
@ -96,12 +94,6 @@ module Sources
return Danbooru.config.http_headers
end
# Returns the size of the image resource without actually downloading the file.
def size
Downloads::File.new(image_url).size
end
memoize :size
def file_url
image_url
end

View File

@ -1,7 +1,5 @@
class UploadService
class Replacer
extend Memoist
attr_reader :post, :replacement
def initialize(post:, replacement:)

View File

@ -1,5 +1,4 @@
class Artist < ApplicationRecord
extend Memoist
class RevertError < Exception ; end
attr_accessor :url_string_changed
@ -191,10 +190,6 @@ class Artist < ApplicationRecord
end
end
included do
memoize :domains
end
def sorted_urls
urls.sort {|a, b| b.priority <=> a.priority}
end
@ -337,8 +332,6 @@ class Artist < ApplicationRecord
end
def reload(options = nil)
flush_cache
if instance_variable_defined?(:@notes)
remove_instance_variable(:@notes)
end

View File

@ -1,6 +1,4 @@
class DmailFilter < ApplicationRecord
extend Memoist
belongs_to :user
validates :user, presence: true
before_validation :initialize_user
@ -21,9 +19,9 @@ class DmailFilter < ApplicationRecord
end
def regexp
union = words.split(/[[:space:]]+/).map { |word| Regexp.escape(word) }.join("|")
/\b#{union}\b/i
@regexp ||= begin
union = words.split(/[[:space:]]+/).map { |word| Regexp.escape(word) }.join("|")
/\b#{union}\b/i
end
end
memoize :regexp
end

View File

@ -1,7 +1,5 @@
class PostVersion < ApplicationRecord
class UndoError < StandardError; end
extend Memoist
belongs_to :post
belongs_to_updater
user_status_counter :post_update_count, foreign_key: :updater_id
@ -167,7 +165,7 @@ class PostVersion < ApplicationRecord
end
def tag_array
(tags || "").split
@tag_array ||= tags.split
end
def locked_tag_array
@ -178,23 +176,20 @@ class PostVersion < ApplicationRecord
PostVersionPresenter.new(self)
end
def reload
flush_cache
super
end
def previous
# HACK: If this if the first version we can avoid a lookup because we know there are no previous versions.
if version <= 1
return nil
end
return @previous if defined?(@previous)
# HACK: if all the post versions for this post have already been preloaded,
# we can use that to avoid a SQL query.
if association(:post).loaded? && post && post.association(:versions).loaded?
post.versions.sort_by(&:version).reverse.find {|v| v.version < version}
@previous = post.versions.sort_by(&:version).reverse.find { |v| v.version < version }
else
PostVersion.where("post_id = ? and version < ?", post_id, version).order("version desc").first
@previous = PostVersion.where("post_id = ? and version < ?", post_id, version).order("version desc").first
end
end
@ -251,12 +246,14 @@ class PostVersion < ApplicationRecord
end
def changes
return @changes if defined?(@changes)
delta = {
:added_tags => added_tags,
:removed_tags => removed_tags,
:obsolete_removed_tags => [],
:obsolete_added_tags => [],
:unchanged_tags => []
added_tags: added_tags,
removed_tags: removed_tags,
obsolete_removed_tags: [],
obsolete_added_tags: [],
unchanged_tags: [],
}
latest_tags = post.tag_array
@ -301,7 +298,7 @@ class PostVersion < ApplicationRecord
delta[:unchanged_tags] = []
end
delta
@changes = delta
end
def undo
@ -371,6 +368,4 @@ class PostVersion < ApplicationRecord
changes[:unchanged_tags].join(" ")
end
end
memoize :previous, :tag_array, :changes
end

View File

@ -1,6 +1,4 @@
class TagImplication < TagRelationship
extend Memoist
has_many :tag_rel_undos, as: :tag_rel
array_attribute :descendant_names
@ -17,7 +15,6 @@ class TagImplication < TagRelationship
module DescendantMethods
extend ActiveSupport::Concern
extend Memoist
module ClassMethods
# assumes names are normalized
@ -40,16 +37,18 @@ class TagImplication < TagRelationship
end
def descendants
[].tap do |all|
@descendants ||= begin
result = []
children = [consequent_name]
until children.empty?
all.concat(children)
result.concat(children)
children = TagImplication.active.where(antecedent_name: children).pluck(:consequent_name)
end
end.sort.uniq
result.sort.uniq
end
end
memoize :descendants
def invalidate_cached_descendants
descendant_names.each do |tag_name|
@ -76,12 +75,9 @@ class TagImplication < TagRelationship
end
module ParentMethods
extend Memoist
def parents
self.class.duplicate_relevant.where("consequent_name = ?", antecedent_name)
@parents ||= self.class.duplicate_relevant.where(consequent_name: antecedent_name)
end
memoize :parents
end
module ValidationMethods
@ -118,8 +114,6 @@ class TagImplication < TagRelationship
end
module ApprovalMethods
extend Memoist
def process!(update_topic: true)
unless valid?
raise errors.full_messages.join("; ")
@ -208,7 +202,6 @@ class TagImplication < TagRelationship
skip_update: !TagRelationship::SUPPORT_HARD_CODED
)
end
memoize :forum_updater
def process_undo!(update_topic: true)
unless valid?
@ -261,4 +254,9 @@ class TagImplication < TagRelationship
flush_cache
super
end
def flush_cache
@dedescendants = nil
@parents = nil
end
end

View File

@ -430,8 +430,6 @@ class User < ApplicationRecord
end
module LimitMethods
extend Memoist
def younger_than(duration)
return false if Danbooru.config.disable_age_checks?
created_at > duration.ago
@ -554,41 +552,39 @@ class User < ApplicationRecord
end
def hourly_upload_limit
post_count = posts.where("created_at >= ?", 1.hour.ago).count
replacement_count = can_approve_posts? ? 0 : post_replacements.where("created_at >= ? and status != ?", 1.hour.ago, "original").count
Danbooru.config.hourly_upload_limit - post_count - replacement_count
@hourly_upload_limit ||= begin
post_count = posts.where("created_at >= ?", 1.hour.ago).count
replacement_count = can_approve_posts? ? 0 : post_replacements.where("created_at >= ? and status != ?", 1.hour.ago, "original").count
Danbooru.config.hourly_upload_limit - post_count - replacement_count
end
end
memoize :hourly_upload_limit
def upload_limit
pieces = upload_limit_pieces
base_upload_limit + (pieces[:approved] / 10) - (pieces[:deleted] / 4) - pieces[:pending]
end
memoize :upload_limit
def upload_limit_pieces
deleted_count = Post.deleted.for_user(id).count
rejected_replacement_count = post_replacement_rejected_count
replaced_penalize_count = own_post_replaced_penalize_count
unapproved_count = Post.pending_or_flagged.for_user(id).count
unapproved_replacements_count = post_replacements.pending.count
approved_count = Post.for_user(id).where('is_flagged = false AND is_deleted = false AND is_pending = false').count
@upload_limit_pieces ||= begin
deleted_count = Post.deleted.for_user(id).count
rejected_replacement_count = post_replacement_rejected_count
replaced_penalize_count = own_post_replaced_penalize_count
unapproved_count = Post.pending_or_flagged.for_user(id).count
unapproved_replacements_count = post_replacements.pending.count
approved_count = Post.for_user(id).where(is_flagged: false, is_deleted: false, is_pending: false).count
{
deleted: deleted_count + replaced_penalize_count + rejected_replacement_count,
deleted_ignore: own_post_replaced_count - replaced_penalize_count,
approved: approved_count,
pending: unapproved_count + unapproved_replacements_count
}
{
deleted: deleted_count + replaced_penalize_count + rejected_replacement_count,
deleted_ignore: own_post_replaced_count - replaced_penalize_count,
approved: approved_count,
pending: unapproved_count + unapproved_replacements_count,
}
end
end
memoize :upload_limit_pieces
def post_upload_throttle
return hourly_upload_limit if is_privileged?
[hourly_upload_limit, post_edit_limit].min
@post_upload_throttle ||= is_privileged? ? hourly_upload_limit : [hourly_upload_limit, post_edit_limit].min
end
memoize :post_upload_throttle
def tag_query_limit
Danbooru.config.tag_query_limit

View File

@ -4,9 +4,7 @@ class WikiPageVersion < ApplicationRecord
belongs_to_updater
user_status_counter :wiki_edit_count, foreign_key: :updater_id
belongs_to :artist, optional: true
delegate :visible?, :to => :wiki_page
extend Memoist
delegate :visible?, to: :wiki_page
module SearchMethods
def for_user(user_id)
@ -42,9 +40,9 @@ class WikiPageVersion < ApplicationRecord
end
def previous
WikiPageVersion.where("wiki_page_id = ? and id < ?", wiki_page_id, id).order("id desc").first
return @previous if defined?(@previous)
@previous = WikiPageVersion.where("wiki_page_id = ? and id < ?", wiki_page_id, id).order("id desc").first
end
memoize :previous
def category_id
Tag.category_for(title)