Skip to content

Commit 22f0318

Browse files
committed
rake task to dump script cache to redis, attempt to get script cache from redis before the db
1 parent 0964e77 commit 22f0318

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

dashboard/app/models/script.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,35 @@ def starting_level
7373
candidate_level
7474
end
7575

76-
def self.script_cache
77-
@@script_cache ||= {}.tap do |cache|
76+
def self.redis
77+
@@redis ||= CDO.level_sources_redis_url ? Redis.connect(url:CDO.level_sources_redis_url) : Hash.new
78+
end
79+
80+
def self.script_cache_from_redis
81+
return unless redis
82+
if marshalled = self.redis['script-cache']
83+
Marshal.load marshalled
84+
end
85+
end
86+
87+
def self.script_cache_to_redis
88+
return unless redis
89+
redis['script-cache'] = Marshal.dump(script_cache_from_db)
90+
end
91+
92+
def self.script_cache_from_db
93+
{}.tap do |cache|
7894
[twenty_hour_script, frozen_script, hoc_script, flappy_script, playlab_script].each do |script|
7995
cache[script.name] = script
8096
cache[script.id.to_s] = script
8197
end
8298
end
8399
end
84100

101+
def self.script_cache
102+
@@script_cache ||= script_cache_from_redis || script_cache_from_db
103+
end
104+
85105
def self.get_from_cache(id)
86106
if self.script_cache[id.to_s]
87107
self.script_cache[id.to_s]

dashboard/lib/tasks/seed.rake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,8 @@ namespace :seed do
295295

296296
task all: [:videos, :concepts, :scripts, :trophies, :prize_providers, :callouts, STANFORD_HINTS_IMPORTED, :secret_words, :secret_pictures]
297297
task incremental: [:videos, :concepts, :scripts_incremental, :trophies, :prize_providers, :callouts, STANFORD_HINTS_IMPORTED, :secret_words, :secret_pictures]
298+
299+
task script_cache_to_redis: :environment do
300+
Script.script_cache_to_redis
301+
end
298302
end

dashboard/test/models/script_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,13 @@ def setup
208208
artist.get_script_level_by_stage_and_position(11, 1)
209209
end
210210
end
211+
212+
test 'gets script cache from redis (or fake redis)' do
213+
Script.script_cache_to_redis # in test this is just a hash
214+
215+
Script.connection.disconnect! # we don't need no stinkin db
216+
217+
assert_equal 'Flappy', Script.get_from_cache('flappy').script_levels[3].level.game.name
218+
assert_equal 'anna', Script.get_from_cache('frozen').script_levels[5].level.skin
219+
end
211220
end

0 commit comments

Comments
 (0)