Permalink
Browse files

redisのキャッシュで高速化

  • Loading branch information...
parrot-studio committed Oct 15, 2016
1 parent 50ae7d6 commit 42947de1d2e39f3967785db26124c850b5981f16
View
@@ -40,6 +40,9 @@ Style/ParenthesesAroundCondition:
Style/RedundantSelf:
Enabled: false
Style/SafeNavigation:
Enabled: false
Style/SignalException:
Enabled: false
@@ -64,6 +67,7 @@ Metrics/ClassLength:
- 'app/controllers/viewer_controller.rb'
- 'lib/arcana_searcher.rb'
- 'lib/arcana_importer.rb'
- 'lib/arcana_cache.rb'
# Configuration parameters: Include.
Rails/Output:
View
@@ -59,3 +59,10 @@ gem 'annotate'
# pry
gem 'pry'
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'
View
@@ -38,12 +38,13 @@ GEM
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
addressable (2.4.0)
annotate (2.7.1)
activerecord (>= 3.2, < 6.0)
rake (>= 10.4, < 12.0)
arel (7.1.1)
arel (7.1.4)
ast (2.3.0)
autoprefixer-rails (6.4.0.2)
autoprefixer-rails (6.5.1)
execjs
babel-source (5.8.35)
babel-transpiler (0.7.0)
@@ -52,11 +53,12 @@ GEM
bootstrap-sass (3.3.7)
autoprefixer-rails (>= 5.2.1)
sass (>= 3.3.4)
browserify-rails (3.1.0)
browserify-rails (3.2.0)
addressable (>= 2.4.0)
railties (>= 4.0.0, < 5.1)
sprockets (>= 3.5.2)
builder (3.2.2)
byebug (9.0.5)
byebug (9.0.6)
coderay (1.1.1)
coffee-rails (4.1.1)
coffee-script (>= 2.2.0)
@@ -66,9 +68,9 @@ GEM
execjs
coffee-script-source (1.10.0)
concurrent-ruby (1.0.2)
config (1.2.1)
config (1.3.0)
activesupport (>= 3.0)
deep_merge (~> 1.0, >= 1.0.1)
deep_merge (~> 1.1.1)
connection_pool (2.2.0)
debug_inspector (0.0.2)
deep_merge (1.1.1)
@@ -79,13 +81,14 @@ GEM
sass (>= 3.2)
globalid (0.3.7)
activesupport (>= 4.1.0)
hiredis (0.6.1)
i18n (0.7.0)
i18n_generators (2.1.1)
rails (>= 3.0.0)
jbuilder (2.6.0)
activesupport (>= 3.0.0, < 5.1)
multi_json (~> 1.2)
jquery-rails (4.1.1)
jquery-rails (4.2.1)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
@@ -102,16 +105,16 @@ GEM
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mini_portile2 (2.1.0)
minitest (5.9.0)
minitest (5.9.1)
multi_json (1.12.1)
mysql2 (0.4.4)
nio4r (1.2.1)
nokogiri (1.6.8)
nokogiri (1.6.8.1)
mini_portile2 (~> 2.1.0)
pkg-config (~> 1.1.7)
parser (2.3.1.2)
oj (2.17.4)
oj_mimic_json (1.0.1)
parser (2.3.1.4)
ast (~> 2.2)
pkg-config (1.1.7)
powerpack (0.1.1)
pry (0.10.4)
coderay (~> 1.1.0)
@@ -147,19 +150,22 @@ GEM
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rainbow (2.1.0)
rake (11.2.2)
rake (11.3.0)
rb-fsevent (0.9.7)
rb-inotify (0.9.7)
ffi (>= 0.5.0)
react-rails (1.8.2)
react-rails (1.9.0)
babel-transpiler (>= 0.7.0)
coffee-script-source (~> 1.8)
connection_pool
execjs
railties (>= 3.2)
tilt
redis (3.3.1)
redis-namespace (1.5.2)
redis (~> 3.0, >= 3.0.4)
ref (2.0.0)
rubocop (0.42.0)
rubocop (0.44.1)
parser (>= 2.3.1.1, < 3.0)
powerpack (~> 0.1)
rainbow (>= 1.99.1, < 3.0)
@@ -174,18 +180,19 @@ GEM
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
slop (3.6.0)
spring (1.7.2)
spring-watcher-listen (2.0.0)
spring (2.0.0)
activesupport (>= 4.2)
spring-watcher-listen (2.0.1)
listen (>= 2.7, < 4.0)
spring (~> 1.2)
spring (>= 1.2, < 3.0)
sprockets (3.7.0)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-es6 (0.9.1)
sprockets-es6 (0.9.2)
babel-source (>= 5.8.11)
babel-transpiler
sprockets (>= 3.0.0)
sprockets-rails (3.1.1)
sprockets-rails (3.2.0)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
@@ -197,9 +204,9 @@ GEM
tilt (2.0.5)
tzinfo (1.2.2)
thread_safe (~> 0.1)
uglifier (3.0.1)
uglifier (3.0.2)
execjs (>= 0.3.0, < 3)
unicode-display_width (1.1.0)
unicode-display_width (1.1.1)
web-console (3.3.1)
actionview (>= 5.0)
activemodel (>= 5.0)
@@ -220,16 +227,21 @@ DEPENDENCIES
coffee-rails (~> 4.1.0)
config
font-awesome-sass
hiredis
i18n_generators
jbuilder (~> 2.0)
jquery-rails
listen (~> 3.0.5)
mysql2 (>= 0.4.4, < 0.5)
oj
oj_mimic_json
pry
pry-rails
puma (~> 3.0)
rails (>= 5.0.0, < 5.1)
react-rails
redis (>= 3.2.0)
redis-namespace
rubocop
sass-rails (~> 5.0)
spring
@@ -241,4 +253,4 @@ DEPENDENCIES
web-console
BUNDLED WITH
1.12.5
1.13.4
@@ -25,10 +25,8 @@ def database
end
def conditions
if stale?(last_modified: ServerSettings.data_update_time, etag: 'conditions')
conds = with_cache('conditions') { create_conditions }
render json: conds
end
stale = stale?(last_modified: ServerSettings.data_update_time, etag: 'conditions')
render json: ArcanaCache.conditions if stale
end
def arcanas
@@ -69,42 +67,6 @@ def changelogs
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)
return if code.blank?
parser = /\AV(\d+)(.+)\z/
@@ -168,13 +130,9 @@ def parse_pt_code_with_chain(code)
def create_member_title(mems)
keys = [:mem1, :mem2, :mem3, :mem4, :sub1, :sub2, :friend]
names = []
keys.each do |k|
c = mems[k]
next if c.blank?
names << Arcana.find_by(job_code: c).name
end
names.join(', ')
codes = keys.map { |k| mems[k] }.compact
as = from_arcana_cache(codes)
as.map { |a| a['name'] }.join(', ')
end
def query_params
@@ -185,10 +143,8 @@ def search_arcanas(query)
searcher = ArcanaSearcher.parse(query)
rsl = { detail: searcher.query_detail, result: [] }
return rsl if searcher.blank? || searcher.query_key.blank?
with_cache("arcanas_#{searcher.query_key}") do
rsl[:result] = searcher.search.map(&:serialize)
rsl
end
rsl[:result] = searcher.search
rsl
end
def search_members(ptm)
@@ -198,13 +154,13 @@ def search_members(ptm)
cs = mems.values.uniq.compact
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 = {}
mems.each do |po, co|
a = as[co]
next unless a
ret[po] = as[co].serialize
ret[po] = as[co]
end
ret
end
@@ -213,7 +169,7 @@ def specified_arcanas(codes)
return [] if codes.blank?
cs = codes.split('/')
return [] if cs.blank?
Arcana.where(job_code: cs).map(&:serialize)
from_arcana_cache(cs)
end
def search_from_name(name)
@@ -227,12 +183,12 @@ def search_from_name(name)
).order(
'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
def with_cache(name, &b)
return unless (name && b)
Rails.cache.fetch(name, &b)
def from_arcana_cache(codes)
ArcanaCache.for_codes(codes)
end
end
View
@@ -15,6 +15,16 @@
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,
presence: true,
uniqueness: true,
View
@@ -16,6 +16,16 @@
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,
presence: true,
uniqueness: true,
@@ -53,7 +53,7 @@
config.log_tags = [:request_id]
# 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)
# config.active_job.queue_adapter = :resque
View
@@ -1,9 +1,13 @@
api:
recently: 24
recently: 32
pt_version: 2
mail:
use: <%= ENV['MAIL_USE'].to_i > 0 ? true : false %>
from: <%= ENV['MAIL_FROM'] %>
admin:
to: <%= ENV['MAIL_ADMIN_TO'] %>
delivery_method: <%= ENV['MAIL_METHOD'] %>
redis:
host: <%= ENV['REDIS_HOST'] %>
port: <%= ENV['REDIS_PORT'] %>
db: <%= ENV['REDIS_DB'] %>
View
@@ -7,3 +7,6 @@ export MAIL_USE=1
export MAIL_METHOD=test
export MAIL_FROM=hoge@example.com
export MAIL_ADMIN_TO=hoge@example.com
export REDIS_HOST=localhost
export REDIS_PORT=6379
export REDIS_DB=0
Oops, something went wrong.

0 comments on commit 42947de

Please sign in to comment.