Rspack's caching mechanism is one of the key factors in its high performance. Through intelligent caching strategies, build speed can be significantly improved. Here's a detailed explanation of Rspack's caching mechanism:
Cache Types
Rspack provides multiple cache types suitable for different scenarios:
1. Memory Cache
Stores build results in memory, fastest speed but lost after restart:
javascriptmodule.exports = { cache: { type: 'memory' } }
Characteristics:
- Fastest cache speed
- Cache lost after restart
- Suitable for development environment
- Uses memory resources
2. Filesystem Cache
Stores build results in filesystem, persisted:
javascriptmodule.exports = { cache: { type: 'filesystem', cacheDirectory: path.resolve(__dirname, '.rspack-cache'), cacheLocation: path.resolve(__dirname, '.rspack-cache'), name: 'rspack-cache', buildDependencies: { config: [__filename] } } }
Characteristics:
- Persistent cache
- Maintained across builds
- Suitable for all environments
- Requires disk space
Cache Configuration Options
Basic Configuration
javascriptmodule.exports = { cache: { type: 'filesystem', cacheDirectory: path.resolve(__dirname, '.cache'), cacheLocation: path.resolve(__dirname, '.cache/rspack'), name: 'my-cache', version: '1.0.0' } }
Cache Dependencies
javascriptmodule.exports = { cache: { type: 'filesystem', buildDependencies: { config: [__filename], tsconfig: ['./tsconfig.json'] } } }
Cache Compression
javascriptmodule.exports = { cache: { type: 'filesystem', compression: 'gzip', maxAge: 1000 * 60 * 60 * 24 * 7 // 7 days } }
Caching Strategies
1. Module Caching
Cache module compilation results:
javascriptmodule.exports = { module: { rules: [ { test: /\.js$/, use: { loader: 'babel-loader', options: { cacheDirectory: true, cacheCompression: false } } } ] } }
2. Resolution Caching
Cache module resolution results:
javascriptmodule.exports = { cache: { type: 'filesystem' }, resolve: { cacheWithContext: true, cachePredicate: (module) => { return !/[\\/]node_modules[\\/]/.test(module.resource); } } }
3. Build Caching
Cache entire build process:
javascriptmodule.exports = { cache: { type: 'filesystem', cacheDirectory: path.resolve(__dirname, '.rspack-cache'), managedPaths: [path.resolve(__dirname, 'node_modules')], immutablePaths: [] } }
Cache Invalidation Strategies
1. Content-based Invalidation
javascriptmodule.exports = { cache: { type: 'filesystem', hashAlgorithm: 'md4' } }
2. Time-based Invalidation
javascriptmodule.exports = { cache: { type: 'filesystem', maxAge: 1000 * 60 * 60 * 24 * 7 // 7 days } }
3. Manual Cache Clearing
javascriptconst { rmSync } = require('fs'); // Clear cache directory rmSync(path.resolve(__dirname, '.rspack-cache'), { recursive: true, force: true });
Cache Optimization
1. Cache Size Limit
javascriptmodule.exports = { cache: { type: 'filesystem', maxMemoryGenerations: 1, maxAge: 1000 * 60 * 60 * 24 * 7, size: 500 * 1024 * 1024 // 500MB } }
2. Cache Compression
javascriptmodule.exports = { cache: { type: 'filesystem', compression: 'gzip', compressionLevel: 9 } }
3. Parallel Caching
javascriptmodule.exports = { cache: { type: 'filesystem', parallelism: 4 } }
Cache Monitoring
1. Cache Statistics
javascriptmodule.exports = { cache: { type: 'filesystem', profile: true } }
2. Cache Logging
javascriptmodule.exports = { stats: { cached: true, cachedAssets: true } }
Environment-specific Configuration
Development Environment
javascriptmodule.exports = { mode: 'development', cache: { type: 'filesystem', cacheDirectory: path.resolve(__dirname, '.cache/dev') } }
Production Environment
javascriptmodule.exports = { mode: 'production', cache: { type: 'filesystem', cacheDirectory: path.resolve(__dirname, '.cache/prod'), maxAge: 1000 * 60 * 60 * 24 * 30 // 30 days } }
CI/CD Caching
1. Persistent Caching
javascriptmodule.exports = { cache: { type: 'filesystem', cacheDirectory: path.resolve(__dirname, '.cache/ci'), buildDependencies: { config: [__filename] } } }
2. Cache Restoration
yaml# GitHub Actions example - name: Restore cache uses: actions/cache@v3 with: path: .cache key: ${{ runner.os }}-rspack-${{ hashFiles('**/package-lock.json') }}
Best Practices
-
Cache Strategy Selection:
- Development environment: Use filesystem cache
- Production environment: Use persistent cache
- CI/CD: Configure cache restoration
-
Cache Management:
- Regularly clean expired cache
- Monitor cache size
- Optimize cache configuration
-
Performance Optimization:
- Enable cache compression
- Reasonably set cache size
- Use parallel caching
-
Cache Validation:
- Validate cache effectiveness
- Handle cache invalidation
- Provide cache clearing mechanism
-
Team Collaboration:
- Unify cache configuration
- Document caching strategies
- Share cache best practices
Rspack's caching mechanism through intelligent caching strategies can significantly improve build speed, especially in large projects and CI/CD environments, the role of caching is more obvious.