asset_ram 1.1.0 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +7 -7
- data/README.md +32 -13
- data/lib/asset_ram/version.rb +1 -1
- data/lib/asset_ram.rb +79 -10
- metadata +28 -27
- data/CHANGELOG.md +0 -10
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: de4320ecb7b33543a8a2e6da028adca12dd4f1161ee6c9e965ed795c21a34ed6
|
|
4
|
+
data.tar.gz: 8af184d2a6dbaf8702f3ec5f459a73831f9a2174da2648f5380ba66b6c82b7c3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ea8061d129a7c90c2ebbaeea58c60776a8cbdbb3e0be974eec9305ebf73f22ad36dc1b496ef5bb8ccb13e10955c9a2bddc2547fa74eb5a12a15d2dcc3d5c976d
|
|
7
|
+
data.tar.gz: 6de127e18aae28bde1fcf0ce2881a48b004a3e601f73cc3dad33223717e6f4b097e7d9a0cdc612e057a628827a29890cb223f3bcb4a70ffeb86abee349762ffb
|
data/Gemfile.lock
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
asset_ram (1.1
|
|
4
|
+
asset_ram (1.2.1)
|
|
5
5
|
|
|
6
6
|
GEM
|
|
7
7
|
remote: https://rubygems.org/
|
|
8
8
|
specs:
|
|
9
9
|
diff-lcs (1.6.2)
|
|
10
|
-
rake (13.3.
|
|
11
|
-
rspec (3.13.
|
|
10
|
+
rake (13.3.1)
|
|
11
|
+
rspec (3.13.2)
|
|
12
12
|
rspec-core (~> 3.13.0)
|
|
13
13
|
rspec-expectations (~> 3.13.0)
|
|
14
14
|
rspec-mocks (~> 3.13.0)
|
|
15
|
-
rspec-core (3.13.
|
|
15
|
+
rspec-core (3.13.6)
|
|
16
16
|
rspec-support (~> 3.13.0)
|
|
17
17
|
rspec-expectations (3.13.5)
|
|
18
18
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
19
19
|
rspec-support (~> 3.13.0)
|
|
20
|
-
rspec-mocks (3.13.
|
|
20
|
+
rspec-mocks (3.13.7)
|
|
21
21
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
22
22
|
rspec-support (~> 3.13.0)
|
|
23
|
-
rspec-support (3.13.
|
|
23
|
+
rspec-support (3.13.7)
|
|
24
24
|
|
|
25
25
|
PLATFORMS
|
|
26
26
|
arm64-darwin-24
|
|
@@ -32,4 +32,4 @@ DEPENDENCIES
|
|
|
32
32
|
rspec (~> 3.0)
|
|
33
33
|
|
|
34
34
|
BUNDLED WITH
|
|
35
|
-
|
|
35
|
+
4.0.6
|
data/README.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# AssetRam
|
|
2
2
|
|
|
3
|
+
## tldr;
|
|
4
|
+
|
|
5
|
+
Change template lines like this:
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
<%= javascript_include_tag('application.js') %>
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
to this:
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
<%= AssetRam.cache { javascript_include_tag('application.js') } %>
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Performance testing
|
|
18
|
+
|
|
3
19
|
**Rails 7 update: I measured a 35% reduction in allocations.** I tested
|
|
4
20
|
[my app's home page](https://www.public.law) in production:
|
|
5
21
|
|
|
@@ -40,8 +56,9 @@ $ bundle install
|
|
|
40
56
|
|
|
41
57
|
## Usage
|
|
42
58
|
|
|
43
|
-
Wrap every asset helper call with `#cache
|
|
59
|
+
Wrap every asset helper call with `#cache` so that code reading `xyz` becomes `AssetRam.cache { xyz }`.
|
|
44
60
|
|
|
61
|
+
**Real examples from my app:**
|
|
45
62
|
|
|
46
63
|
### Before
|
|
47
64
|
|
|
@@ -52,7 +69,7 @@ Wrap every asset helper call with `#cache`, like this:
|
|
|
52
69
|
```
|
|
53
70
|
|
|
54
71
|
|
|
55
|
-
### After
|
|
72
|
+
### After
|
|
56
73
|
|
|
57
74
|
```ruby
|
|
58
75
|
<%= AssetRam.cache { favicon_link_tag('favicon/favicon.ico', rel: 'icon') } %>
|
|
@@ -60,11 +77,6 @@ Wrap every asset helper call with `#cache`, like this:
|
|
|
60
77
|
<%= AssetRam.cache { javascript_include_tag('application.js') } %>
|
|
61
78
|
```
|
|
62
79
|
|
|
63
|
-
Or, for compatibility, you can still use:
|
|
64
|
-
|
|
65
|
-
```ruby
|
|
66
|
-
<%= AssetRam::Helper.cache { favicon_link_tag('favicon/favicon.ico', rel: 'icon') } %>
|
|
67
|
-
```
|
|
68
80
|
|
|
69
81
|
After booting up, AssetRam sends a message like this _once_ to the log for each usage:
|
|
70
82
|
|
|
@@ -78,7 +90,7 @@ so we can see what it's caching. This is the line of code that, without AssetRam
|
|
|
78
90
|
would be exectued on every request.
|
|
79
91
|
|
|
80
92
|
|
|
81
|
-
I use it in my footer for social icons as well. I **used to** have this
|
|
93
|
+
I use it in my footer for social icons as well. I **used to** have this in my `_footer_for_screen.haml`:
|
|
82
94
|
|
|
83
95
|
```ruby
|
|
84
96
|
= link_to AssetRam.cache { image_tag("social/instagram-logo.svg", alt: 'Instagram', loading: 'lazy', decoding: 'async') }, "https://www.instagram.com/law.is.code/"
|
|
@@ -88,7 +100,13 @@ I use it in my footer for social icons as well. I **used to** have this: (HAML s
|
|
|
88
100
|
```
|
|
89
101
|
|
|
90
102
|
|
|
91
|
-
But my whole footer partial is static. So now I
|
|
103
|
+
But my whole footer partial is static. So now I changed the application layout from this:
|
|
104
|
+
|
|
105
|
+
```ruby
|
|
106
|
+
render 'footer_for_screen'
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
to this, which caches the whole partial with one `#cache` call:
|
|
92
110
|
|
|
93
111
|
```ruby
|
|
94
112
|
= AssetRam.cache { render 'footer_for_screen' }
|
|
@@ -119,9 +137,10 @@ These asset fingerprints are potentially re-generated on every deploy. Maybe I e
|
|
|
119
137
|
I haven't modified any ActiveRecord models. This means that **the asset links cannot be stored in
|
|
120
138
|
the standard Rails cache.** (If the Rails cache had a lifetime option of, "until next boot", that would solve the problem.)
|
|
121
139
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
140
|
+
> [!NOTE]
|
|
141
|
+
> This is when I realized that storing the computed paths in a simple hash (in RAM only) would be fast and never
|
|
142
|
+
> return stale data: The RAM cache goes away on a deploy/restart, which is
|
|
143
|
+
> when asset fingerprints could change.
|
|
125
144
|
|
|
126
145
|
And so one-by-one I started storing the computed asset paths in a hash, and saw pretty dramatic results.
|
|
127
146
|
|
|
@@ -132,7 +151,7 @@ that the caching needs to be done within the context of a view. This is why the
|
|
|
132
151
|
the way it does.
|
|
133
152
|
|
|
134
153
|
To make it as easy as possible to use, the lib finds the view's source filename and the line number of
|
|
135
|
-
the code being cached. This has been working well and in production for four months in a large Rails app.
|
|
154
|
+
the code being cached. This has been working well and in production for ~~four months~~ years in a large Rails app.
|
|
136
155
|
|
|
137
156
|
|
|
138
157
|
## Development
|
data/lib/asset_ram/version.rb
CHANGED
data/lib/asset_ram.rb
CHANGED
|
@@ -28,6 +28,9 @@ require_relative "asset_ram/version"
|
|
|
28
28
|
module AssetRam
|
|
29
29
|
class Error < StandardError; end
|
|
30
30
|
|
|
31
|
+
_rev = ENV['APP_REVISION']
|
|
32
|
+
APP_REVISION = (_rev.nil? || _rev.empty?) ? nil : _rev.freeze
|
|
33
|
+
|
|
31
34
|
##
|
|
32
35
|
# The simpler API: AssetRam.cache { ... }
|
|
33
36
|
#
|
|
@@ -38,31 +41,97 @@ module AssetRam
|
|
|
38
41
|
##
|
|
39
42
|
# Our own asset helper which memoizes Rails' asset helper calls.
|
|
40
43
|
#
|
|
44
|
+
# Entries are nested by source path, line and key, so a cache hit only
|
|
45
|
+
# looks up those three values and never builds a composite key.
|
|
46
|
+
#
|
|
41
47
|
class Helper
|
|
42
48
|
@@_cache = {}
|
|
49
|
+
@@_rails_keys = {}
|
|
50
|
+
@@_cumulative_size = 0
|
|
51
|
+
|
|
52
|
+
NO_KEY = Object.new.freeze
|
|
43
53
|
|
|
44
54
|
|
|
45
55
|
def self.cache(key: '', &blk)
|
|
46
|
-
|
|
47
|
-
cache_key << key if !key.to_s.empty?
|
|
56
|
+
return yield if ENV['ASSET_RAM_DISABLE']
|
|
48
57
|
|
|
49
|
-
|
|
58
|
+
path, line = blk.source_location
|
|
59
|
+
key = NO_KEY if blank_key?(key)
|
|
60
|
+
|
|
61
|
+
if rails_cache?
|
|
62
|
+
fetch_from_rails_cache(rails_cache_key(path, line, key), &blk)
|
|
63
|
+
else
|
|
64
|
+
entries = slot(@@_cache, path, line)
|
|
65
|
+
entries.fetch(key) { entries[key] = remember(location_name(path, line, key), &blk) }
|
|
66
|
+
end
|
|
50
67
|
end
|
|
51
68
|
|
|
52
69
|
|
|
53
70
|
def self.cache_by_key(cache_key, &blk)
|
|
54
71
|
return yield if ENV['ASSET_RAM_DISABLE']
|
|
55
72
|
|
|
56
|
-
if
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
Rails.logger.warn("Caching #{cache_key}")
|
|
61
|
-
@@_cache[cache_key] = yield
|
|
73
|
+
if rails_cache?
|
|
74
|
+
fetch_from_rails_cache("asset_ram/#{APP_REVISION}/#{cache_key.join('/')}", &blk)
|
|
75
|
+
else
|
|
76
|
+
@@_cache.fetch(cache_key) { @@_cache[cache_key] = remember(cache_key, &blk) }
|
|
62
77
|
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def self.rails_cache?
|
|
82
|
+
APP_REVISION && !ENV['ASSET_RAM_HASH_ONLY']
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def self.fetch_from_rails_cache(rails_cache_key)
|
|
87
|
+
Rails.cache.fetch(rails_cache_key) do
|
|
88
|
+
Rails.logger.warn("Caching #{rails_cache_key} in Rails cache")
|
|
89
|
+
yield
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def self.rails_cache_key(path, line, key)
|
|
95
|
+
keys = slot(@@_rails_keys[APP_REVISION] ||= {}, path, line)
|
|
96
|
+
keys.fetch(key) do
|
|
97
|
+
keys[key] = "asset_ram/#{APP_REVISION}/#{location_name(path, line, key).join('/')}".freeze
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
#
|
|
103
|
+
# Cached strings are frozen copies, so no caller can alter what
|
|
104
|
+
# every later page receives.
|
|
105
|
+
#
|
|
106
|
+
def self.remember(name)
|
|
107
|
+
value = yield
|
|
108
|
+
value = value.dup.freeze if value.is_a?(String) && !value.frozen?
|
|
109
|
+
|
|
110
|
+
# Using WARN level because it should only output
|
|
111
|
+
# once during any Rails run. If it's output multiple
|
|
112
|
+
# times, then caching isn't working correctly.
|
|
113
|
+
@@_cumulative_size += value.to_s.bytesize
|
|
114
|
+
Rails.logger.warn("Caching #{name} in RAM cache (total size: #{@@_cumulative_size} bytes)")
|
|
115
|
+
value
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def self.slot(store, path, line)
|
|
120
|
+
lines = store[path] ||= {}
|
|
121
|
+
lines[line] ||= {}
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def self.blank_key?(key)
|
|
126
|
+
key.is_a?(String) || key.is_a?(Symbol) ? key.empty? : key.to_s.empty?
|
|
127
|
+
end
|
|
128
|
+
|
|
63
129
|
|
|
64
|
-
|
|
130
|
+
def self.location_name(path, line, key)
|
|
131
|
+
key.equal?(NO_KEY) ? [path, line] : [path, line, key]
|
|
65
132
|
end
|
|
66
133
|
|
|
134
|
+
private_class_method :rails_cache?, :fetch_from_rails_cache, :rails_cache_key,
|
|
135
|
+
:remember, :slot, :blank_key?, :location_name
|
|
67
136
|
end
|
|
68
137
|
end
|
metadata
CHANGED
|
@@ -1,53 +1,54 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: asset_ram
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.2.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
- Robert Shecter
|
|
7
|
+
- Robert Shecter
|
|
8
8
|
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies: []
|
|
12
12
|
email:
|
|
13
|
-
- robert@public.law
|
|
13
|
+
- robert@public.law
|
|
14
14
|
executables: []
|
|
15
15
|
extensions: []
|
|
16
16
|
extra_rdoc_files: []
|
|
17
17
|
files:
|
|
18
|
-
-
|
|
19
|
-
-
|
|
20
|
-
- Gemfile
|
|
21
|
-
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
24
|
-
-
|
|
25
|
-
- bin/
|
|
26
|
-
-
|
|
27
|
-
- lib/asset_ram.rb
|
|
28
|
-
-
|
|
29
|
-
|
|
30
|
-
homepage: https://github.com/public-law/asset_ram
|
|
18
|
+
- .rspec
|
|
19
|
+
- Gemfile
|
|
20
|
+
- Gemfile.lock
|
|
21
|
+
- LICENSE.txt
|
|
22
|
+
- README.md
|
|
23
|
+
- Rakefile
|
|
24
|
+
- bin/console
|
|
25
|
+
- bin/setup
|
|
26
|
+
- lib/asset_ram.rb
|
|
27
|
+
- lib/asset_ram/version.rb
|
|
28
|
+
- test-data.png
|
|
29
|
+
homepage: "https://github.com/public-law/asset_ram"
|
|
31
30
|
licenses:
|
|
32
|
-
- MIT
|
|
31
|
+
- MIT
|
|
33
32
|
metadata:
|
|
34
|
-
homepage_uri: https://github.com/public-law/asset_ram
|
|
35
|
-
source_code_uri: https://github.com/public-law/asset_ram
|
|
33
|
+
homepage_uri: "https://github.com/public-law/asset_ram"
|
|
34
|
+
source_code_uri: "https://github.com/public-law/asset_ram"
|
|
36
35
|
rdoc_options: []
|
|
37
36
|
require_paths:
|
|
38
|
-
- lib
|
|
37
|
+
- lib
|
|
39
38
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
40
39
|
requirements:
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
40
|
+
-
|
|
41
|
+
- ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: 2.4.0
|
|
44
44
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
45
45
|
requirements:
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
-
|
|
47
|
+
- ">="
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: "0"
|
|
49
50
|
requirements: []
|
|
50
|
-
rubygems_version:
|
|
51
|
+
rubygems_version: 4.1.0
|
|
51
52
|
specification_version: 4
|
|
52
53
|
summary: Improves Rails performance by caching asset path calculations
|
|
53
54
|
test_files: []
|
data/CHANGELOG.md
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
## [1.1.0] - 2025-06-12
|
|
2
|
-
|
|
3
|
-
- Added a new, simpler API: `AssetRam.cache { ... }` as the preferred way to cache asset computations in views. The legacy `AssetRam::Helper.cache` API is still supported for compatibility.
|
|
4
|
-
- Improved documentation to highlight the new API and update usage examples.
|
|
5
|
-
- Added comprehensive RSpec tests for caching behavior, keying, and environment variable handling.
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
## [0.1.0] - 2021-09-25
|
|
9
|
-
|
|
10
|
-
- Initial release
|