Skip to content

Commit

Permalink
First pass at pre-caching the cassettes.
Browse files Browse the repository at this point in the history
  • Loading branch information
myronmarston committed Dec 3, 2011
1 parent 70ca447 commit 0fc42e7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
38 changes: 35 additions & 3 deletions lib/vcr/cassette/cache.rb
@@ -1,3 +1,5 @@
require 'thread'

module VCR
class Cassette
class Cache
Expand All @@ -7,22 +9,52 @@ def initialize
File.read(file_name) :
nil
end

@mutex = Mutex.new
@file_queue = Queue.new
end

def [](file_name)
@files[file_name]
@mutex.synchronize do
@files[file_name]
end
end

def exists_with_content?(file_name)
@files[file_name].to_s.size > 0
@mutex.synchronize do
@files[file_name].to_s.size > 0
end
end

# TODO: test me
def []=(file_name, content)
directory = File.dirname(file_name)
FileUtils.mkdir_p directory unless File.exist?(directory)
File.open(file_name, 'w') { |f| f.write content }
@files[file_name] = content

@mutex.synchronize do
@files[file_name] = content
end
end

def prefetch_from(directory)
@threads = 20.times.map do
Thread.new do
loop do
file_name = @file_queue.pop
break if file_name == :exit
@mutex.synchronize do
@files[file_name] = File.read(file_name)
end
end
end
end

Dir["#{directory}/**/*.yml"].each do |file_name|
@file_queue << file_name
end

20.times { @file_queue << :exit }
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/vcr/configuration.rb
Expand Up @@ -25,6 +25,7 @@ def initialize
def cassette_library_dir=(cassette_library_dir)
@cassette_library_dir = cassette_library_dir
FileUtils.mkdir_p(cassette_library_dir) if cassette_library_dir
VCR.cassette_cache.prefetch_from(cassette_library_dir)
end

attr_reader :default_cassette_options
Expand Down

0 comments on commit 0fc42e7

Please sign in to comment.