From 534df964a6542891982e95698795d2f35e7ad821 Mon Sep 17 00:00:00 2001 From: Oskar Eichler Date: Fri, 28 Aug 2026 16:41:55 +0300 Subject: [PATCH] Update file cache size accounting when replacing an entry --- CHANGELOG.md | 2 ++ README.md | 2 ++ lib/sprockets/cache/file_store.rb | 8 ++++---- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6366a4d92..a8e216be1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ **Master** +- Update file cache size accounting when replacing an entry. [Oskar Eichler](https://github.com/OskarEichler) + Get upgrade notes from Sprockets 3.x to 4.x at https://github.com/rails/sprockets/blob/master/UPGRADING.md # 4.4.1 diff --git a/README.md b/README.md index 958b44cd4..1d63914bd 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,8 @@ If you want to work on Sprockets or better understand how it works read [How Spr ## Behavior Overview +Replacing an existing file cache entry adjusts size accounting by the change in stored bytes, so growth can trigger eviction and shrinkage releases capacity. + You can interact with Sprockets primarily through directives and file extensions. This section covers how to use each of these things, and the defaults that ship with Sprockets. Since you are likely using Sprockets through another framework (such as the [Rails asset pipeline](http://guides.rubyonrails.org/asset_pipeline.html)), there will be configuration options you can toggle that will change behavior such as what directories or files get compiled. For that documentation you should see your framework's documentation. diff --git a/lib/sprockets/cache/file_store.rb b/lib/sprockets/cache/file_store.rb index 85d7f7972..223fb6564 100644 --- a/lib/sprockets/cache/file_store.rb +++ b/lib/sprockets/cache/file_store.rb @@ -88,8 +88,8 @@ def set(key, value) # Ensure directory exists FileUtils.mkdir_p File.dirname(path) - # Check if cache exists before writing - exists = File.exist?(path) + # Record the previous size before replacing the cache file + previous_size = safe_stat(path)&.size || 0 # Serialize value marshaled = Marshal.dump(value) @@ -111,8 +111,8 @@ def set(key, value) # Write data PathUtils.atomic_write(path) do |f| f.write(raw) - if defined?(@size) && !exists - @size += f.size + if defined?(@size) + @size += f.size - previous_size end end