Skip to content

Commit d078bc7

Browse files
authored
feat: rename persistent cache maxGenerations to maxVersions (#14545)
1 parent d823a96 commit d078bc7

27 files changed

Lines changed: 161 additions & 193 deletions

File tree

crates/node_binding/napi-binding.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,6 +1926,8 @@ export interface RawCacheOptionsMemory {
19261926
export interface RawCacheOptionsPersistent {
19271927
buildDependencies?: Array<string>
19281928
version?: string
1929+
maxAge: number
1930+
maxVersions: number
19291931
snapshot?: RawSnapshotOptions
19301932
storage?: RawStorageOptions
19311933
portable?: boolean
@@ -3096,8 +3098,6 @@ export interface RawStatsOptions {
30963098
export interface RawStorageOptions {
30973099
type: "filesystem"
30983100
directory: string
3099-
maxAge: number
3100-
maxGenerations: number
31013101
}
31023102

31033103
export interface RawSubresourceIntegrityPluginOptions {

crates/rspack_binding_api/src/raw_options/raw_cache/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use rspack_core::{CacheOptions, cache::persistent::PersistentCacheOptions};
1717
pub struct RawCacheOptionsPersistent {
1818
pub build_dependencies: Option<Vec<String>>,
1919
pub version: Option<String>,
20+
pub max_age: u32,
21+
pub max_versions: u32,
2022
pub snapshot: Option<RawSnapshotOptions>,
2123
pub storage: Option<RawStorageOptions>,
2224
pub portable: Option<bool>,
@@ -27,7 +29,7 @@ impl TryFrom<RawCacheOptionsPersistent> for PersistentCacheOptions {
2729
type Error = rspack_error::Error;
2830

2931
fn try_from(value: RawCacheOptionsPersistent) -> rspack_error::Result<Self> {
30-
let (storage, max_age, max_generations) = value.storage.unwrap_or_default().normalize()?;
32+
let storage = value.storage.unwrap_or_default().normalize()?;
3133
Ok(Self {
3234
build_dependencies: value
3335
.build_dependencies
@@ -40,8 +42,8 @@ impl TryFrom<RawCacheOptionsPersistent> for PersistentCacheOptions {
4042
storage,
4143
portable: value.portable.unwrap_or_default(),
4244
readonly: value.readonly.unwrap_or_default(),
43-
max_age,
44-
max_generations,
45+
max_age: value.max_age.into(),
46+
max_versions: value.max_versions,
4547
})
4648
}
4749
}

crates/rspack_binding_api/src/raw_options/raw_cache/raw_storage.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,14 @@ pub struct RawStorageOptions {
77
#[napi(ts_type = r#""filesystem""#)]
88
pub r#type: String,
99
pub directory: String,
10-
pub max_age: u32,
11-
pub max_generations: u32,
1210
}
1311

1412
impl RawStorageOptions {
15-
pub(super) fn normalize(self) -> rspack_error::Result<(StorageOptions, u64, u32)> {
13+
pub(super) fn normalize(self) -> rspack_error::Result<StorageOptions> {
1614
match self.r#type.as_str() {
17-
"filesystem" => Ok((
18-
StorageOptions::FileSystem {
19-
directory: self.directory.into(),
20-
},
21-
self.max_age.into(),
22-
self.max_generations,
23-
)),
15+
"filesystem" => Ok(StorageOptions::FileSystem {
16+
directory: self.directory.into(),
17+
}),
2418
storage_type => Err(rspack_error::error!(
2519
"unsupported storage type {storage_type}"
2620
)),

crates/rspack_core/src/cache/persistent/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ pub struct PersistentCacheOptions {
4545
/// Filesystem cache max age in seconds.
4646
#[cacheable(with=Skip)]
4747
pub max_age: u64,
48-
/// Filesystem generation count limit for the current storage directory.
48+
/// Filesystem version count limit for the current storage directory.
4949
#[cacheable(with=Skip)]
50-
pub max_generations: u32,
50+
pub max_versions: u32,
5151
}
5252

5353
/// Persistent cache implementation
@@ -79,7 +79,6 @@ impl PersistentCache {
7979
None
8080
};
8181
let codec = Arc::new(CacheCodec::new(project_root));
82-
let max_generations = option.max_generations;
8382
// use codec.encode to transform the absolute path in option,
8483
// it will ensure that same project in different directory have the same version.
8584
let option_bytes = codec
@@ -98,7 +97,7 @@ impl PersistentCache {
9897
option.storage.clone(),
9998
version,
10099
option.max_age,
101-
max_generations,
100+
option.max_versions,
102101
intermediate_filesystem,
103102
);
104103
let snapshot = Arc::new(Snapshot::new(

crates/rspack_core/src/cache/persistent/storage/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ pub fn create_storage(
2222
options: StorageOptions,
2323
version: String,
2424
max_age: u64,
25-
max_generations: u32,
25+
max_versions: u32,
2626
fs: Arc<dyn IntermediateFileSystem>,
2727
) -> BoxStorage {
2828
match options {
2929
StorageOptions::FileSystem { directory } => {
3030
let option = FileSystemOptions {
3131
directory,
3232
version,
33-
max_generations,
33+
max_versions,
3434
max_pack_size: 500 * 1024,
3535
expire: max_age,
3636
fs,

crates/rspack_storage/src/filesystem/meta.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl Meta {
7575
Ok(())
7676
}
7777

78-
/// Updates the active version and removes versions rejected by age or generation limits.
78+
/// Updates the active version and removes versions rejected by age or version limits.
7979
///
8080
/// Returns `(removed_versions, next_check_time)`.
8181
/// - `removed_versions`: version directories that should be deleted.
@@ -84,7 +84,7 @@ impl Meta {
8484
&mut self,
8585
active_version: &str,
8686
expire_seconds: u64,
87-
max_generations: u32,
87+
max_versions: u32,
8888
versions: &[String],
8989
) -> Result<(Vec<String>, u64)> {
9090
let now = Self::current_timestamp();
@@ -110,9 +110,9 @@ impl Meta {
110110
});
111111
}
112112

113-
if max_generations != 0 {
113+
if max_versions != 0 {
114114
// `versions` is already scoped to the current storage directory, so every
115-
// non-hidden, non-active entry is a generation candidate.
115+
// non-hidden, non-active entry is a version candidate.
116116
let mut candidates = versions
117117
.iter()
118118
.filter(|version| version.as_str() != active_version && !version.starts_with(['_', '.']))
@@ -123,10 +123,8 @@ impl Meta {
123123
)
124124
})
125125
.collect::<Vec<_>>();
126-
let retained_inactive_generations = max_generations.saturating_sub(1) as usize;
127-
let remove_count = candidates
128-
.len()
129-
.saturating_sub(retained_inactive_generations);
126+
let retained_inactive_versions = max_versions.saturating_sub(1) as usize;
127+
let remove_count = candidates.len().saturating_sub(retained_inactive_versions);
130128
candidates.sort_unstable_by(|(version_a, timestamp_a), (version_b, timestamp_b)| {
131129
timestamp_a
132130
.cmp(timestamp_b)

crates/rspack_storage/src/filesystem/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async fn refresh_metadata(
1919
fs: ScopeFileSystem,
2020
version: String,
2121
expire: u64,
22-
max_generations: u32,
22+
max_versions: u32,
2323
next_meta_refresh_time: Arc<Mutex<u64>>,
2424
) {
2525
let now = Meta::current_timestamp();
@@ -37,7 +37,7 @@ async fn refresh_metadata(
3737
// sync with versions that still exist on disk.
3838
let versions = fs.list_child().await.unwrap_or_default();
3939
let Ok((removed_versions, next_refresh_time)) = meta
40-
.refresh(&version, expire, max_generations, &versions)
40+
.refresh(&version, expire, max_versions, &versions)
4141
.await
4242
else {
4343
return;
@@ -120,12 +120,12 @@ impl Storage for FileSystemStorage {
120120
let fs = self.fs.clone();
121121
let version = self.options.version.clone();
122122
let expire = self.options.expire;
123-
let max_generations = self.options.max_generations;
123+
let max_versions = self.options.max_versions;
124124
let next_meta_refresh_time = self.next_meta_refresh_time.clone();
125125

126126
self.task_queue.add_task(async move {
127127
if db.save(changes, max_pack_size).await {
128-
refresh_metadata(fs, version, expire, max_generations, next_meta_refresh_time).await;
128+
refresh_metadata(fs, version, expire, max_versions, next_meta_refresh_time).await;
129129
}
130130
});
131131
}

crates/rspack_storage/src/filesystem/options.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ pub struct FileSystemOptions {
1515
pub max_pack_size: usize,
1616
/// Data expiration time (seconds), 0 means never expire
1717
pub expire: u64,
18-
/// Maximum number of generations retained in the storage directory. 0 means disabled.
19-
pub max_generations: u32,
18+
/// Maximum number of versions retained in the storage directory. 0 means disabled.
19+
pub max_versions: u32,
2020
/// File system implementation
2121
pub fs: Arc<dyn IntermediateFileSystem>,
2222
}

packages/rspack/src/config/adapter.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,11 @@ function getRawCache(cache: CacheNormalized): RawOptions['cache'] {
128128
};
129129
return {
130130
...cache,
131+
maxAge: toRawStorageLimit('cache.maxAge', cache.maxAge!),
132+
maxVersions: toRawStorageLimit('cache.maxVersions', cache.maxVersions!),
131133
storage: {
132134
...cache.storage,
133135
directory: cache.storage.directory!,
134-
maxAge: toRawStorageLimit('cache.storage.maxAge', cache.storage.maxAge!),
135-
maxGenerations: toRawStorageLimit(
136-
'cache.storage.maxGenerations',
137-
cache.storage.maxGenerations!,
138-
),
139136
},
140137
snapshot: {
141138
immutablePaths: cache.snapshot.immutablePaths!,

packages/rspack/src/config/defaults.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,13 @@ const applyCacheDefaults = (
230230
break;
231231
case 'persistent':
232232
D(cache, 'version', '');
233+
D(cache, 'maxAge', DEFAULT_FILESYSTEM_CACHE_MAX_AGE_SECONDS);
234+
D(cache, 'maxVersions', 3);
233235
F(cache, 'buildDependencies', () => []);
234236
F(cache.snapshot, 'immutablePaths', () => []);
235237
F(cache.snapshot, 'unmanagedPaths', () => []);
236238
F(cache.snapshot, 'managedPaths', () => [/[\\/]node_modules[\\/][^.]/]);
237239
D(cache.storage, 'type', 'filesystem');
238-
D(cache.storage, 'maxAge', DEFAULT_FILESYSTEM_CACHE_MAX_AGE_SECONDS);
239-
D(cache.storage, 'maxGenerations', 3);
240240
F(cache.storage, 'directory', () => {
241241
const modeName = mode || 'production';
242242
const compilerName = name ? `${name}-${modeName}` : modeName;

0 commit comments

Comments
 (0)