Skip to content

Commit 0f82956

Browse files
committed
Merge pull request code-dot-org#832 from code-dot-org/l2-redis
use redis as an L2 cache for the Script.script_cache stuff
2 parents 7dba0a7 + 8807432 commit 0f82956

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

aws/build.rake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ $websites = build_task('websites', [deploy_dir('rebuild'), BLOCKLY_COMMIT_TASK])
158158
RakeUtils.system 'rake', 'build'
159159

160160
if rack_env?(:production) && CDO.daemon
161+
HipChat.log "Putting <b>dashboard</b> scripts in redis..."
162+
RakeUtils.rake 'seed:script_cache_to_redis'
163+
161164
thread_count = 1 + (CDO.app_servers.keys.count / 10)
162165
threaded_each CDO.app_servers.keys, thread_count do |name|
163166
upgrade_frontend name, CDO.app_servers[name]

dashboard/app/models/script.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,34 @@ 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+
if marshalled = self.redis['script-cache']
82+
Script.connection # rails doesn't load the mysql libraries unless you ask it to, this confuses Marshal
83+
Marshal.load marshalled
84+
end
85+
end
86+
87+
def self.script_cache_to_redis
88+
redis['script-cache'] = Marshal.dump(script_cache_from_db)
89+
end
90+
91+
def self.script_cache_from_db
92+
{}.tap do |cache|
7893
[twenty_hour_script, frozen_script, hoc_script, flappy_script, playlab_script].each do |script|
7994
cache[script.name] = script
8095
cache[script.id.to_s] = script
8196
end
8297
end
8398
end
8499

100+
def self.script_cache
101+
@@script_cache ||= script_cache_from_redis || script_cache_from_db
102+
end
103+
85104
def self.get_from_cache(id)
86105
if self.script_cache[id.to_s]
87106
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)