4
4
5
5
module Opal
6
6
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
+
7
46
include BuilderProcessors
8
47
48
+ attr_accessor :cache_store
49
+
9
50
def initialize ( options = nil )
10
51
( options || { } ) . each_pair do |k , v |
11
52
public_send ( "#{ k } =" , v )
@@ -32,6 +73,14 @@ def build(path, options = {})
32
73
end
33
74
34
75
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
+
35
84
path = path_reader . expand ( filename ) . to_s unless stub? ( filename )
36
85
asset = processor_for ( source , filename , path , options )
37
86
requires = preload + asset . requires + tree_requires ( asset , path )
@@ -110,10 +159,21 @@ def process_require(filename, options)
110
159
end
111
160
end
112
161
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 } "
113
171
path = path_reader . expand ( filename ) . to_s unless stub? ( filename )
114
172
asset = processor_for ( source , filename , path , options . merge ( requirable : true ) )
115
173
process_requires ( asset . requires +tree_requires ( asset , path ) , options )
116
174
processed << asset
175
+
176
+ cache_store . store filename , asset . to_s , asset . requires
117
177
end
118
178
119
179
def process_requires ( requires , options )
5 commit comments
elia commentedon Nov 22, 2014
@adambeynon what about changing the cache interface to be similar to that of a hash?
I think most caches around do that, at least in rails world :)
ryanstout commentedon Nov 22, 2014
Is this for caching compiled files?
adambeynon commentedon Nov 22, 2014
@ryanstout Yeap, makes page loads much faster using the new builder. We should be down to just having to recompile individual changed files.
adambeynon commentedon Nov 22, 2014
@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 commentedon Nov 22, 2014
@adambeynon awesome. I was hoping this would be added. Great work!