Skip to content

Commit 2373a56

Browse files
committedNov 22, 2014
Initial cache store
1 parent 665fee6 commit 2373a56

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed
 

‎examples/rack/app/application.rb

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'user'
33

44
module MyApp
5+
56
class Application
67
def initialize
78
@user = User.new('Bill')

‎examples/rack/config.ru

+2
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ Bundler.require
44
run Opal::Server.new { |s|
55
s.main = 'application'
66
s.append_path 'app'
7+
8+
s.sprockets.cache = Sprockets::Cache::FileStore.new("tmp/cache/assets")
79
}

‎lib/opal/builder.rb

+60
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,49 @@
44

55
module Opal
66
class Builder
7+
class CachedAsset
8+
def initialize(data)
9+
@data = data
10+
end
11+
12+
def requires
13+
@data[:requires]
14+
end
15+
16+
def to_s
17+
@data[:contents]
18+
end
19+
20+
def source_map
21+
""
22+
end
23+
end
24+
25+
class CacheStore
26+
attr_reader :environment
27+
28+
def initialize(environment)
29+
@environment = environment
30+
end
31+
32+
def store(key, contents, requires)
33+
environment.cache_set("opal/#{key}.cache", {
34+
:contents => contents, :requires => requires})
35+
end
36+
37+
def retrieve(key)
38+
if obj = environment.cache_get("opal/#{key}.cache")
39+
return CachedAsset.new(obj)
40+
else
41+
nil
42+
end
43+
end
44+
end
45+
746
include BuilderProcessors
847

48+
attr_accessor :cache_store
49+
950
def initialize(options = nil)
1051
(options || {}).each_pair do |k,v|
1152
public_send("#{k}=", v)
@@ -32,6 +73,14 @@ def build(path, options = {})
3273
end
3374

3475
def build_str source, filename, options = {}
76+
#if cached = cache_store.retrieve(filename) and
77+
# puts "CACHED: #{filename}"
78+
# process_requires cached.requires, options
79+
# processed << cached
80+
81+
# return self
82+
#end
83+
3584
path = path_reader.expand(filename).to_s unless stub?(filename)
3685
asset = processor_for(source, filename, path, options)
3786
requires = preload + asset.requires + tree_requires(asset, path)
@@ -110,10 +159,21 @@ def process_require(filename, options)
110159
end
111160
end
112161

162+
if cached = cache_store.retrieve(filename)
163+
puts "CACHED: #{filename}"
164+
process_requires cached.requires, options
165+
processed << cached
166+
167+
return
168+
end
169+
170+
puts ">>>> COMPILE #{filename}"
113171
path = path_reader.expand(filename).to_s unless stub?(filename)
114172
asset = processor_for(source, filename, path, options.merge(requirable: true))
115173
process_requires(asset.requires+tree_requires(asset, path), options)
116174
processed << asset
175+
176+
cache_store.store filename, asset.to_s, asset.requires
117177
end
118178

119179
def process_requires(requires, options)

‎lib/opal/sprockets/processor.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def evaluate(context, locals, &block)
7373
builder = self.class.new_builder(context)
7474
result = builder.build_str(data, path, :prerequired => prerequired)
7575

76-
if self.class.source_map_enabled
76+
if self.class.source_map_enabled and false
7777
register_source_map(context.logical_path, result.source_map.to_s)
7878
"#{result.to_s}\n//# sourceMappingURL=#{File.basename(context.logical_path)}.map\n"
7979
else
@@ -108,10 +108,12 @@ def self.new_builder(context)
108108
}
109109

110110
path_reader = ::Opal::Sprockets::PathReader.new(context.environment, context)
111+
cache_store = ::Opal::Builder::CacheStore.new(context.environment)
111112
return Builder.new(
112113
compiler_options: compiler_options,
113114
stubs: stubbed_files,
114-
path_reader: path_reader
115+
path_reader: path_reader,
116+
cache_store: cache_store
115117
)
116118
end
117119
end

5 commit comments

Comments
 (5)

elia commented on Nov 22, 2014

@elia
Member

@adambeynon what about changing the cache interface to be similar to that of a hash?

cache.fetch(key) { "do the work here for cache misses" }

I think most caches around do that, at least in rails world :)

ryanstout commented on Nov 22, 2014

@ryanstout
Contributor

Is this for caching compiled files?

adambeynon commented on Nov 22, 2014

@adambeynon
ContributorAuthor

@ryanstout Yeap, makes page loads much faster using the new builder. We should be down to just having to recompile individual changed files.

adambeynon commented on Nov 22, 2014

@adambeynon
ContributorAuthor

@elia Sure thing! For non-sprockets contexts (e.g. using Builder directly) we could add a default memory store. By default sprockets doesnt supply one, rails has to set one up (the File store).

ryanstout commented on Nov 22, 2014

@ryanstout
Contributor

@adambeynon awesome. I was hoping this would be added. Great work!

Please sign in to comment.