Skip to content

Commit

Permalink
redisのキャッシュで高速化
Browse files Browse the repository at this point in the history
  • Loading branch information
parrot-studio committed Oct 22, 2016
1 parent 50ae7d6 commit 42947de
Show file tree
Hide file tree
Showing 14 changed files with 404 additions and 160 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ Style/ParenthesesAroundCondition:
Style/RedundantSelf: Style/RedundantSelf:
Enabled: false Enabled: false


Style/SafeNavigation:
Enabled: false

Style/SignalException: Style/SignalException:
Enabled: false Enabled: false


Expand All @@ -64,6 +67,7 @@ Metrics/ClassLength:
- 'app/controllers/viewer_controller.rb' - 'app/controllers/viewer_controller.rb'
- 'lib/arcana_searcher.rb' - 'lib/arcana_searcher.rb'
- 'lib/arcana_importer.rb' - 'lib/arcana_importer.rb'
- 'lib/arcana_cache.rb'


# Configuration parameters: Include. # Configuration parameters: Include.
Rails/Output: Rails/Output:
Expand Down
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -59,3 +59,10 @@ gem 'annotate'
# pry # pry
gem 'pry' gem 'pry'
gem 'pry-rails' gem 'pry-rails'

# cache
gem 'hiredis'
gem 'redis', '>= 3.2.0', require: ['redis', 'redis/connection/hiredis']
gem 'redis-namespace'
gem 'oj'
gem 'oj_mimic_json'
58 changes: 35 additions & 23 deletions Gemfile.lock
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ GEM
i18n (~> 0.7) i18n (~> 0.7)
minitest (~> 5.1) minitest (~> 5.1)
tzinfo (~> 1.1) tzinfo (~> 1.1)
addressable (2.4.0)
annotate (2.7.1) annotate (2.7.1)
activerecord (>= 3.2, < 6.0) activerecord (>= 3.2, < 6.0)
rake (>= 10.4, < 12.0) rake (>= 10.4, < 12.0)
arel (7.1.1) arel (7.1.4)
ast (2.3.0) ast (2.3.0)
autoprefixer-rails (6.4.0.2) autoprefixer-rails (6.5.1)
execjs execjs
babel-source (5.8.35) babel-source (5.8.35)
babel-transpiler (0.7.0) babel-transpiler (0.7.0)
Expand All @@ -52,11 +53,12 @@ GEM
bootstrap-sass (3.3.7) bootstrap-sass (3.3.7)
autoprefixer-rails (>= 5.2.1) autoprefixer-rails (>= 5.2.1)
sass (>= 3.3.4) sass (>= 3.3.4)
browserify-rails (3.1.0) browserify-rails (3.2.0)
addressable (>= 2.4.0)
railties (>= 4.0.0, < 5.1) railties (>= 4.0.0, < 5.1)
sprockets (>= 3.5.2) sprockets (>= 3.5.2)
builder (3.2.2) builder (3.2.2)
byebug (9.0.5) byebug (9.0.6)
coderay (1.1.1) coderay (1.1.1)
coffee-rails (4.1.1) coffee-rails (4.1.1)
coffee-script (>= 2.2.0) coffee-script (>= 2.2.0)
Expand All @@ -66,9 +68,9 @@ GEM
execjs execjs
coffee-script-source (1.10.0) coffee-script-source (1.10.0)
concurrent-ruby (1.0.2) concurrent-ruby (1.0.2)
config (1.2.1) config (1.3.0)
activesupport (>= 3.0) activesupport (>= 3.0)
deep_merge (~> 1.0, >= 1.0.1) deep_merge (~> 1.1.1)
connection_pool (2.2.0) connection_pool (2.2.0)
debug_inspector (0.0.2) debug_inspector (0.0.2)
deep_merge (1.1.1) deep_merge (1.1.1)
Expand All @@ -79,13 +81,14 @@ GEM
sass (>= 3.2) sass (>= 3.2)
globalid (0.3.7) globalid (0.3.7)
activesupport (>= 4.1.0) activesupport (>= 4.1.0)
hiredis (0.6.1)
i18n (0.7.0) i18n (0.7.0)
i18n_generators (2.1.1) i18n_generators (2.1.1)
rails (>= 3.0.0) rails (>= 3.0.0)
jbuilder (2.6.0) jbuilder (2.6.0)
activesupport (>= 3.0.0, < 5.1) activesupport (>= 3.0.0, < 5.1)
multi_json (~> 1.2) multi_json (~> 1.2)
jquery-rails (4.1.1) jquery-rails (4.2.1)
rails-dom-testing (>= 1, < 3) rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0) railties (>= 4.2.0)
thor (>= 0.14, < 2.0) thor (>= 0.14, < 2.0)
Expand All @@ -102,16 +105,16 @@ GEM
mime-types-data (~> 3.2015) mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521) mime-types-data (3.2016.0521)
mini_portile2 (2.1.0) mini_portile2 (2.1.0)
minitest (5.9.0) minitest (5.9.1)
multi_json (1.12.1) multi_json (1.12.1)
mysql2 (0.4.4) mysql2 (0.4.4)
nio4r (1.2.1) nio4r (1.2.1)
nokogiri (1.6.8) nokogiri (1.6.8.1)
mini_portile2 (~> 2.1.0) mini_portile2 (~> 2.1.0)
pkg-config (~> 1.1.7) oj (2.17.4)
parser (2.3.1.2) oj_mimic_json (1.0.1)
parser (2.3.1.4)
ast (~> 2.2) ast (~> 2.2)
pkg-config (1.1.7)
powerpack (0.1.1) powerpack (0.1.1)
pry (0.10.4) pry (0.10.4)
coderay (~> 1.1.0) coderay (~> 1.1.0)
Expand Down Expand Up @@ -147,19 +150,22 @@ GEM
rake (>= 0.8.7) rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0) thor (>= 0.18.1, < 2.0)
rainbow (2.1.0) rainbow (2.1.0)
rake (11.2.2) rake (11.3.0)
rb-fsevent (0.9.7) rb-fsevent (0.9.7)
rb-inotify (0.9.7) rb-inotify (0.9.7)
ffi (>= 0.5.0) ffi (>= 0.5.0)
react-rails (1.8.2) react-rails (1.9.0)
babel-transpiler (>= 0.7.0) babel-transpiler (>= 0.7.0)
coffee-script-source (~> 1.8) coffee-script-source (~> 1.8)
connection_pool connection_pool
execjs execjs
railties (>= 3.2) railties (>= 3.2)
tilt tilt
redis (3.3.1)
redis-namespace (1.5.2)
redis (~> 3.0, >= 3.0.4)
ref (2.0.0) ref (2.0.0)
rubocop (0.42.0) rubocop (0.44.1)
parser (>= 2.3.1.1, < 3.0) parser (>= 2.3.1.1, < 3.0)
powerpack (~> 0.1) powerpack (~> 0.1)
rainbow (>= 1.99.1, < 3.0) rainbow (>= 1.99.1, < 3.0)
Expand All @@ -174,18 +180,19 @@ GEM
sprockets-rails (>= 2.0, < 4.0) sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3) tilt (>= 1.1, < 3)
slop (3.6.0) slop (3.6.0)
spring (1.7.2) spring (2.0.0)
spring-watcher-listen (2.0.0) activesupport (>= 4.2)
spring-watcher-listen (2.0.1)
listen (>= 2.7, < 4.0) listen (>= 2.7, < 4.0)
spring (~> 1.2) spring (>= 1.2, < 3.0)
sprockets (3.7.0) sprockets (3.7.0)
concurrent-ruby (~> 1.0) concurrent-ruby (~> 1.0)
rack (> 1, < 3) rack (> 1, < 3)
sprockets-es6 (0.9.1) sprockets-es6 (0.9.2)
babel-source (>= 5.8.11) babel-source (>= 5.8.11)
babel-transpiler babel-transpiler
sprockets (>= 3.0.0) sprockets (>= 3.0.0)
sprockets-rails (3.1.1) sprockets-rails (3.2.0)
actionpack (>= 4.0) actionpack (>= 4.0)
activesupport (>= 4.0) activesupport (>= 4.0)
sprockets (>= 3.0.0) sprockets (>= 3.0.0)
Expand All @@ -197,9 +204,9 @@ GEM
tilt (2.0.5) tilt (2.0.5)
tzinfo (1.2.2) tzinfo (1.2.2)
thread_safe (~> 0.1) thread_safe (~> 0.1)
uglifier (3.0.1) uglifier (3.0.2)
execjs (>= 0.3.0, < 3) execjs (>= 0.3.0, < 3)
unicode-display_width (1.1.0) unicode-display_width (1.1.1)
web-console (3.3.1) web-console (3.3.1)
actionview (>= 5.0) actionview (>= 5.0)
activemodel (>= 5.0) activemodel (>= 5.0)
Expand All @@ -220,16 +227,21 @@ DEPENDENCIES
coffee-rails (~> 4.1.0) coffee-rails (~> 4.1.0)
config config
font-awesome-sass font-awesome-sass
hiredis
i18n_generators i18n_generators
jbuilder (~> 2.0) jbuilder (~> 2.0)
jquery-rails jquery-rails
listen (~> 3.0.5) listen (~> 3.0.5)
mysql2 (>= 0.4.4, < 0.5) mysql2 (>= 0.4.4, < 0.5)
oj
oj_mimic_json
pry pry
pry-rails pry-rails
puma (~> 3.0) puma (~> 3.0)
rails (>= 5.0.0, < 5.1) rails (>= 5.0.0, < 5.1)
react-rails react-rails
redis (>= 3.2.0)
redis-namespace
rubocop rubocop
sass-rails (~> 5.0) sass-rails (~> 5.0)
spring spring
Expand All @@ -241,4 +253,4 @@ DEPENDENCIES
web-console web-console


BUNDLED WITH BUNDLED WITH
1.12.5 1.13.4
72 changes: 14 additions & 58 deletions app/controllers/viewer_controller.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ def database
end end


def conditions def conditions
if stale?(last_modified: ServerSettings.data_update_time, etag: 'conditions') stale = stale?(last_modified: ServerSettings.data_update_time, etag: 'conditions')
conds = with_cache('conditions') { create_conditions } render json: ArcanaCache.conditions if stale
render json: conds
end
end end


def arcanas def arcanas
Expand Down Expand Up @@ -69,42 +67,6 @@ def changelogs


private private


def voiceactors
ret = []
VoiceActor.order(:name).each do |act|
ret << [act.id, act.name]
end
ret
end

def illustrators
ret = []
Illustrator.order(:name).each do |ill|
ret << [ill.id, ill.name]
end
ret
end

def create_conditions
{
unions: Arcana::UNION_NAMES.reject { |k, _| k == :unknown }.to_a,
sourcecategorys: Arcana::SOURCE_CATEGORYS,
sources: Arcana::SOURCE_CONDS,
skillcategorys: SkillEffect::CATEGORY_CONDS,
skillsubs: SkillEffect::SUBCATEGORY_CONDS,
skilleffects: SkillEffect::SUBEFFECT_CONDS,
abilitycategorys: AbilityEffect::CATEGORY_CONDS,
abilityeffects: AbilityEffect::EFFECT_CONDS,
abilityconditions: AbilityEffect::CONDITION_CONDS,
chainabilitycategorys: AbilityEffect.chain_ability_categorys,
chainabilityeffects: AbilityEffect.chain_ability_effects,
chainabilityconditions: AbilityEffect.chain_ability_conditions,
voiceactors: voiceactors,
illustrators: illustrators,
latestinfo: Changelog.latest.as_json
}
end

def parse_pt_code(code) def parse_pt_code(code)
return if code.blank? return if code.blank?
parser = /\AV(\d+)(.+)\z/ parser = /\AV(\d+)(.+)\z/
Expand Down Expand Up @@ -168,13 +130,9 @@ def parse_pt_code_with_chain(code)


def create_member_title(mems) def create_member_title(mems)
keys = [:mem1, :mem2, :mem3, :mem4, :sub1, :sub2, :friend] keys = [:mem1, :mem2, :mem3, :mem4, :sub1, :sub2, :friend]
names = [] codes = keys.map { |k| mems[k] }.compact
keys.each do |k| as = from_arcana_cache(codes)
c = mems[k] as.map { |a| a['name'] }.join(', ')
next if c.blank?
names << Arcana.find_by(job_code: c).name
end
names.join(', ')
end end


def query_params def query_params
Expand All @@ -185,10 +143,8 @@ def search_arcanas(query)
searcher = ArcanaSearcher.parse(query) searcher = ArcanaSearcher.parse(query)
rsl = { detail: searcher.query_detail, result: [] } rsl = { detail: searcher.query_detail, result: [] }
return rsl if searcher.blank? || searcher.query_key.blank? return rsl if searcher.blank? || searcher.query_key.blank?
with_cache("arcanas_#{searcher.query_key}") do rsl[:result] = searcher.search
rsl[:result] = searcher.search.map(&:serialize) rsl
rsl
end
end end


def search_members(ptm) def search_members(ptm)
Expand All @@ -198,13 +154,13 @@ def search_members(ptm)


cs = mems.values.uniq.compact cs = mems.values.uniq.compact
return {} if cs.empty? return {} if cs.empty?
as = Arcana.where(job_code: cs).index_by(&:job_code) as = from_arcana_cache(cs).each_with_object({}) { |o, h| h[o['job_code']] = o }


ret = {} ret = {}
mems.each do |po, co| mems.each do |po, co|
a = as[co] a = as[co]
next unless a next unless a
ret[po] = as[co].serialize ret[po] = as[co]
end end
ret ret
end end
Expand All @@ -213,7 +169,7 @@ def specified_arcanas(codes)
return [] if codes.blank? return [] if codes.blank?
cs = codes.split('/') cs = codes.split('/')
return [] if cs.blank? return [] if cs.blank?
Arcana.where(job_code: cs).map(&:serialize) from_arcana_cache(cs)
end end


def search_from_name(name) def search_from_name(name)
Expand All @@ -227,12 +183,12 @@ def search_from_name(name)
).order( ).order(
'arcanas.rarity DESC, arcanas.cost DESC, arcanas.job_type, arcanas.job_index DESC' 'arcanas.rarity DESC, arcanas.cost DESC, arcanas.job_type, arcanas.job_index DESC'
) )
arel.map(&:serialize) codes = arel.distinct.pluck(:job_code)
from_arcana_cache(codes)
end end


def with_cache(name, &b) def from_arcana_cache(codes)
return unless (name && b) ArcanaCache.for_codes(codes)
Rails.cache.fetch(name, &b)
end end


end end
10 changes: 10 additions & 0 deletions app/models/illustrator.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@


class Illustrator < ApplicationRecord class Illustrator < ApplicationRecord


class << self
def conditions
ret = []
self.order(:name).each do |ill|
ret << [ill.id, ill.name]
end
ret
end
end

validates :name, validates :name,
presence: true, presence: true,
uniqueness: true, uniqueness: true,
Expand Down
10 changes: 10 additions & 0 deletions app/models/voice_actor.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@


class VoiceActor < ApplicationRecord class VoiceActor < ApplicationRecord


class << self
def conditions
ret = []
self.order(:name).each do |act|
ret << [act.id, act.name]
end
ret
end
end

validates :name, validates :name,
presence: true, presence: true,
uniqueness: true, uniqueness: true,
Expand Down
2 changes: 1 addition & 1 deletion config/environments/production.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
config.log_tags = [:request_id] config.log_tags = [:request_id]


# Use a different cache store in production. # Use a different cache store in production.
# config.cache_store = :mem_cache_store config.cache_store = :memory_store


# Use a real queuing backend for Active Job (and separate queues per environment) # Use a real queuing backend for Active Job (and separate queues per environment)
# config.active_job.queue_adapter = :resque # config.active_job.queue_adapter = :resque
Expand Down
6 changes: 5 additions & 1 deletion config/settings.yml
Original file line number Original file line Diff line number Diff line change
@@ -1,9 +1,13 @@
api: api:
recently: 24 recently: 32
pt_version: 2 pt_version: 2
mail: mail:
use: <%= ENV['MAIL_USE'].to_i > 0 ? true : false %> use: <%= ENV['MAIL_USE'].to_i > 0 ? true : false %>
from: <%= ENV['MAIL_FROM'] %> from: <%= ENV['MAIL_FROM'] %>
admin: admin:
to: <%= ENV['MAIL_ADMIN_TO'] %> to: <%= ENV['MAIL_ADMIN_TO'] %>
delivery_method: <%= ENV['MAIL_METHOD'] %> delivery_method: <%= ENV['MAIL_METHOD'] %>
redis:
host: <%= ENV['REDIS_HOST'] %>
port: <%= ENV['REDIS_PORT'] %>
db: <%= ENV['REDIS_DB'] %>
3 changes: 3 additions & 0 deletions envrc.sample
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ export MAIL_USE=1
export MAIL_METHOD=test export MAIL_METHOD=test
export MAIL_FROM=hoge@example.com export MAIL_FROM=hoge@example.com
export MAIL_ADMIN_TO=hoge@example.com export MAIL_ADMIN_TO=hoge@example.com
export REDIS_HOST=localhost
export REDIS_PORT=6379
export REDIS_DB=0
Loading

0 comments on commit 42947de

Please sign in to comment.