Permalink
Browse files

First pass at pre-caching the cassettes.

  • Loading branch information...
1 parent 70ca447 commit 0fc42e7525c1a051320de73365cf911c5ebf5de6 @myronmarston committed Dec 3, 2011
Showing with 36 additions and 3 deletions.
  1. +35 −3 lib/vcr/cassette/cache.rb
  2. +1 −0 lib/vcr/configuration.rb
@@ -1,3 +1,5 @@
+require 'thread'
+
module VCR
class Cassette
class Cache
@@ -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
@@ -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

0 comments on commit 0fc42e7

Please sign in to comment.