[Cleanup] Remove Cache.hash

Rails already handles long keys for us
This commit is contained in:
Earlopain 2023-05-19 22:14:34 +02:00
parent 6841fe3b7e
commit 05b100f8da
No known key found for this signature in database
GPG Key ID: 6CFB948E15246897
10 changed files with 16 additions and 30 deletions

View File

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

View File

@ -103,7 +103,6 @@ GEM
builder (3.2.4)
case_transform (0.2)
activesupport
cityhash (0.9.0)
concurrent-ruby (1.2.2)
connection_pool (2.4.0)
crack (0.4.5)
@ -399,7 +398,6 @@ DEPENDENCIES
addressable
bcrypt
bootsnap
cityhash
dalli
diff-lcs
dotenv-rails

View File

@ -73,8 +73,8 @@ module BulkUpdateRequestsHelper
end
def script_with_line_breaks(bur, with_decorations:)
hash = Cache.hash "#{CurrentUser.is_admin? ? "mod" : ""}#{with_decorations ? "color" : ""}#{bur.status}#{bur.script}"
Cache.fetch(hash, 3600) do
cache_key = "#{CurrentUser.is_admin? ? 'mod' : ''}#{with_decorations ? 'color' : ''}#{bur.updated_at}"
Cache.fetch(cache_key, 1.hour) do
script_tokenized = BulkUpdateRequestImporter.tokenize(bur.script)
script_tags = collect_script_tags(script_tokenized)
script_tokenized.map do |cmd, arg1, arg2, arg3|

View File

@ -1,6 +1,6 @@
class Cache
def self.read_multi(keys, prefix)
sanitized_key_to_key_hash = keys.index_by { |key| "#{prefix}:#{Cache.hash(key)}" }
sanitized_key_to_key_hash = keys.index_by { |key| "#{prefix}:#{key}" }
sanitized_keys = sanitized_key_to_key_hash.keys
sanitized_key_to_value_hash = Rails.cache.read_multi(*sanitized_keys)
@ -24,10 +24,6 @@ class Cache
Rails.cache.clear
end
def self.hash(string)
CityHash.hash64(string).to_s(36)
end
def self.redis
# Using a shared variable like this here is OK
# since unicorn spawns a new process for each worker

View File

@ -15,7 +15,7 @@ class TagCorrection
end
def category_cache
Cache.fetch("tc:#{Cache.hash(tag.name)}")
Cache.fetch("tc:#{tag.name}")
end
def fix!

View File

@ -1030,7 +1030,7 @@ class Post < ApplicationRecord
tags += " -status:deleted" if !Tag.has_metatag?(tags, "status", "-status")
tags = Tag.normalize_query(tags)
cache_key = "pfc:#{Cache.hash(tags)}"
cache_key = "pfc:#{tags}"
count = Cache.fetch(cache_key)
if count.nil?
count = Post.tag_match(tags).count_only

View File

@ -125,7 +125,7 @@ class Tag < ApplicationRecord
if disable_cache
select_category_for(tag_name)
else
Cache.fetch("tc:#{Cache.hash(tag_name)}") do
Cache.fetch("tc:#{tag_name}") do
select_category_for(tag_name)
end
end
@ -144,7 +144,7 @@ class Tag < ApplicationRecord
if not_found.count > 0
# Is multi_write worth it here? Normal usage of this will be short put lists and then never touched.
Tag.where(name: not_found).select([:id, :name, :category]).find_each do |tag|
Cache.write("tc:#{Cache.hash(tag.name)}", tag.category)
Cache.write("tc:#{tag.name}", tag.category)
found[tag.name] = tag.category
end
end
@ -181,7 +181,7 @@ class Tag < ApplicationRecord
end
def update_category_cache
Cache.write("tc:#{Cache.hash(name)}", category, 3.hours)
Cache.write("tc:#{name}", category, 3.hours)
end
def user_can_change_category?
@ -948,11 +948,9 @@ class Tag < ApplicationRecord
end
def update_related_if_outdated
key = Cache.hash(name)
if Cache.fetch("urt:#{key}").nil? && should_update_related?
if Cache.fetch("urt:#{name}").nil? && should_update_related?
TagUpdateRelatedJob.perform_later(id)
Cache.write("urt:#{key}", true, 600) # mutex to prevent redundant updates
Cache.write("urt:#{name}", true, 600) # mutex to prevent redundant updates
end
end

View File

@ -146,13 +146,8 @@ class User < ApplicationRecord
module ClassMethods
def name_to_id(name)
normalized_name = normalize_name(name)
Cache.fetch("uni:#{Cache.hash(normalized_name)}", 4.hours) do
val = select_value_sql("SELECT id FROM users WHERE lower(name) = ?", normalized_name)
if val.present?
val.to_i
else
nil
end
Cache.fetch("uni:#{normalized_name}", 4.hours) do
select_value_sql("SELECT id FROM users WHERE lower(name) = ?", normalized_name)&.to_i
end
end
@ -205,7 +200,7 @@ class User < ApplicationRecord
def update_cache
Cache.write("uin:#{id}", name, 4.hours)
Cache.write("uni:#{Cache.hash(name)}", id, 4.hours)
Cache.write("uni:#{name}", id, 4.hours)
end
end

View File

@ -505,7 +505,7 @@ class PostTest < ActiveSupport::TestCase
end
should "1234 update the category cache of the tag" do
assert_equal(Tag.categories.copyright, Cache.fetch("tc:#{Cache.hash('abc')}"))
assert_equal(Tag.categories.copyright, Cache.fetch("tc:abc"))
end
should "update the tag counts of the posts" do

View File

@ -74,10 +74,10 @@ class TagTest < ActiveSupport::TestCase
should "reset its category after updating" do
tag = create(:artist_tag)
assert_equal(Tag.categories.artist, Cache.fetch("tc:#{Cache.hash(tag.name)}"))
assert_equal(Tag.categories.artist, Cache.fetch("tc:#{tag.name}"))
tag.update_attribute(:category, Tag.categories.copyright)
assert_equal(Tag.categories.copyright, Cache.fetch("tc:#{Cache.hash(tag.name)}"))
assert_equal(Tag.categories.copyright, Cache.fetch("tc:#{tag.name}"))
end
context "not be settable to an invalid category" do