Class: Gem::SourceInfoCache
- Inherits:
-
Object
- Object
- Gem::SourceInfoCache
- Includes:
- UserInteraction
- Defined in:
- lib/rubygems/source_info_cache.rb
Overview
SourceInfoCache stores a copy of the gem index for each gem source.
There are two possible cache locations, the system cache and the user cache:
-
The system cache is prefered if it is writable or can be created.
-
The user cache is used otherwise
Once a cache is selected, it will be used for all operations. SourceInfoCache will not switch between cache files dynamically.
Cache data is a Hash mapping a source URI to a SourceInfoCacheEntry.
– To keep things straight, this is how the cache objects all fit together:
Gem::SourceInfoCache
@cache_data = {
source_uri => Gem::SourceInfoCacheEntry
@size => source index size
@source_index => Gem::SourceIndex
...
}
Class Method Summary collapse
- .cache ⇒ Object
- .cache_data ⇒ Object
-
.search(pattern) ⇒ Object
Search all source indexes for
pattern
.
Instance Method Summary collapse
-
#cache_data ⇒ Object
The most recent cache data.
-
#cache_file ⇒ Object
The name of the cache file to be read.
-
#flush ⇒ Object
Write the cache to a local file (if it is dirty).
-
#initialize ⇒ SourceInfoCache
constructor
:nodoc:.
-
#refresh ⇒ Object
Refreshes each source in the cache from its repository.
-
#search(pattern) ⇒ Object
Searches all source indexes for
pattern
. -
#set_cache_data(hash) ⇒ Object
Set the source info cache data directly.
-
#system_cache_file ⇒ Object
The name of the system cache file.
-
#update ⇒ Object
Mark the cache as updated (i.e. dirty).
-
#user_cache_file ⇒ Object
The name of the user cache file.
-
#write_cache ⇒ Object
Write data to the proper cache.
Methods included from DefaultUserInteraction
#ui, ui, #ui=, ui=, #use_ui, use_ui
Constructor Details
#initialize ⇒ SourceInfoCache
:nodoc:
52 53 54 55 56 57 58 59 |
# File 'lib/rubygems/source_info_cache.rb', line 52 def initialize # :nodoc: @cache_data = nil @cache_file = nil @dirty = false @system_cache_file = nil @user_cache_file = nil end |
Class Method Details
.cache ⇒ Object
36 37 38 39 40 41 |
# File 'lib/rubygems/source_info_cache.rb', line 36 def self.cache return @cache if @cache @cache = new @cache.refresh @cache end |
.cache_data ⇒ Object
43 44 45 |
# File 'lib/rubygems/source_info_cache.rb', line 43 def self.cache_data cache.cache_data end |
.search(pattern) ⇒ Object
Search all source indexes for pattern
.
48 49 50 |
# File 'lib/rubygems/source_info_cache.rb', line 48 def self.search(pattern) cache.search(pattern) end |
Instance Method Details
#cache_data ⇒ Object
The most recent cache data.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rubygems/source_info_cache.rb', line 62 def cache_data return @cache_data if @cache_data @dirty = false cache_file # HACK writable check # Marshal loads 30-40% faster from a String, and 2MB on 20061116 is small begin data = File.open cache_file, 'rb' do |fp| fp.read end @cache_data = Marshal.load data rescue {} end end |
#cache_file ⇒ Object
The name of the cache file to be read
78 79 80 81 82 83 |
# File 'lib/rubygems/source_info_cache.rb', line 78 def cache_file return @cache_file if @cache_file @cache_file = (try_file(system_cache_file) or try_file(user_cache_file) or raise "unable to locate a writable cache file") end |
#flush ⇒ Object
Write the cache to a local file (if it is dirty).
86 87 88 89 |
# File 'lib/rubygems/source_info_cache.rb', line 86 def flush write_cache if @dirty @dirty = false end |
#refresh ⇒ Object
Refreshes each source in the cache from its repository.
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/rubygems/source_info_cache.rb', line 92 def refresh Gem.sources.each do |source_uri| cache_entry = cache_data[source_uri] if cache_entry.nil? then cache_entry = Gem::SourceInfoCacheEntry.new nil, 0 cache_data[source_uri] = cache_entry end cache_entry.refresh source_uri end update flush end |
#search(pattern) ⇒ Object
Searches all source indexes for pattern
.
107 108 109 110 111 |
# File 'lib/rubygems/source_info_cache.rb', line 107 def search(pattern) cache_data.map do |source, sic_entry| sic_entry.source_index.search pattern end.flatten end |
#set_cache_data(hash) ⇒ Object
Set the source info cache data directly. This is mainly used for unit testing when we don’t want to read a file system for to grab the cached source index information. The hash
should map a source URL into a SourceIndexCacheEntry.
140 141 142 143 |
# File 'lib/rubygems/source_info_cache.rb', line 140 def set_cache_data(hash) @cache_data = hash @dirty = false end |
#system_cache_file ⇒ Object
The name of the system cache file.
114 115 116 |
# File 'lib/rubygems/source_info_cache.rb', line 114 def system_cache_file @system_cache_file ||= File.join(Gem.dir, "source_cache") end |
#update ⇒ Object
Mark the cache as updated (i.e. dirty).
119 120 121 |
# File 'lib/rubygems/source_info_cache.rb', line 119 def update @dirty = true end |
#user_cache_file ⇒ Object
The name of the user cache file.
124 125 126 127 |
# File 'lib/rubygems/source_info_cache.rb', line 124 def user_cache_file @user_cache_file ||= ENV['GEMCACHE'] || File.join(Gem.user_home, ".gem", "source_cache") end |
#write_cache ⇒ Object
Write data to the proper cache.
130 131 132 133 134 |
# File 'lib/rubygems/source_info_cache.rb', line 130 def write_cache open cache_file, "wb" do |f| f.write Marshal.dump(cache_data) end end |