Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

First pass at pre-caching the cassettes.

  • Loading branch information...
commit 0fc42e7525c1a051320de73365cf911c5ebf5de6 1 parent 70ca447
@myronmarston myronmarston authored
Showing with 36 additions and 3 deletions.
  1. +35 −3 lib/vcr/cassette/cache.rb
  2. +1 −0  lib/vcr/configuration.rb
View
38 lib/vcr/cassette/cache.rb
@@ -1,3 +1,5 @@
+require 'thread'
+
module VCR
class Cassette
class Cache
@@ -7,14 +9,21 @@ 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
@@ -22,7 +31,30 @@ 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
View
1  lib/vcr/configuration.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.