Skip to content

Commit 949c73e

Browse files
authored
Merge pull request #126 from rails/cache-refactor
Use a Hash for caching things
2 parents 4d02a20 + 24b88bf commit 949c73e

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed

lib/importmap/map.rb

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class InvalidFile < StandardError; end
77

88
def initialize
99
@packages, @directories = {}, {}
10+
@cache = {}
1011
end
1112

1213
def draw(path = nil, &block)
@@ -87,24 +88,15 @@ def cache_sweeper(watches: nil)
8788
MappedFile = Struct.new(:name, :path, :preload, keyword_init: true)
8889

8990
def cache_as(name)
90-
if result = instance_variable_get("@cached_#{name}")
91+
if result = @cache[name.to_s]
9192
result
9293
else
93-
remember_cache_key(name)
94-
instance_variable_set("@cached_#{name}", yield)
94+
@cache[name.to_s] = yield
9595
end
9696
end
9797

98-
def remember_cache_key(name)
99-
@cache_keys ||= Set.new
100-
@cache_keys.add name
101-
end
102-
103-
10498
def clear_cache
105-
@cache_keys&.each do |name|
106-
instance_variable_set("@cached_#{name}", nil)
107-
end
99+
@cache.clear
108100
end
109101

110102
def rescuable_asset_error?(error)

0 commit comments

Comments
 (0)