What is the Difference Between LRU and ARC Cache Strategies in TEngine?
TEngine provides two distinct cache eviction policies—LRU (Least Recently Used) and ARC (Adaptive Replacement Cache)—that determine how the Resource Module retains or removes assets when memory limits are reached, with LRU prioritizing recent access history and ARC dynamically balancing both recency and frequency of use.
TEngine is a Unity game framework built on top of YooAsset, inheriting its sophisticated resource management capabilities. The framework allows developers to switch between LRU and ARC cache strategies without modifying loading code, enabling optimization for different asset access patterns directly through configuration.
How LRU and ARC Cache Strategies Work
TEngine's Resource Module delegates cache management to YooAsset's DefaultCacheFileSystem, which implements both eviction algorithms in UnityProject/Packages/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs. The core difference lies in how each policy decides which assets to evict when the cache reaches its capacity limit.
LRU (Least Recently Used)
The LRU strategy maintains a simple recency-based ordering of cached assets. When the cache is full and a new asset requires space, the system evicts the asset that has remained unaccessed for the longest period. This approach assumes that recently used assets are likely to be needed again soon, making it ideal for workloads with strong temporal locality such as UI textures or level-specific assets that persist during specific game phases.
ARC (Adaptive Replacement Cache)
The ARC strategy maintains two separate LRU lists: one tracking recent accesses and another tracking frequent accesses. Unlike standard LRU, ARC dynamically adjusts the balance between these lists based on runtime usage patterns. If the workload shows repeated access to specific assets, ARC shifts capacity toward the frequency list; if access patterns favor new assets, it favors the recency list. This adaptive behavior excels in mixed-usage scenarios where some assets (like common UI sprites) are accessed repeatedly while others (like occasional character models) are loaded briefly and discarded.
Configuring Cache Strategies in TEngine
You can switch between LRU and ARC without changing asset loading code. The framework automatically applies the selected eviction algorithm when the cache requires pruning.
Editor Configuration
To change the cache strategy through the Unity Editor interface:
- Open TEngineSetting from the top menu bar.
- Navigate to the 资源模块 (Resource Module) section.
- Locate the 缓存策略 (Cache Strategy) dropdown.
- Select LRU or ARC.
- Clear the existing cache to ensure the new policy starts fresh using the "ClearCache" button.
Runtime Configuration via Script
For dynamic strategy switching at runtime, use the YooAssetSettings class exposed in UnityProject/Packages/YooAsset/Runtime/Settings/YooAssetSettings.cs:
using YooAsset;
// Switch to ARC cache strategy
YooAssetSettings.SetDefaultCacheFileSystem(CacheFileSystemType.ARC);
// Clear existing cache to apply the new policy
GameModule.Resource.ClearCache();
When loading assets, the system automatically applies the configured cache policy without additional parameters:
// Load a sprite - cache retention is handled automatically
Sprite icon = await GameModule.Resource.LoadAssetAsync<Sprite>("UI/Icons/ItemIcon");
When to Use LRU vs ARC
Select your cache strategy based on your game's specific asset access patterns:
-
Choose LRU when your game loads assets in distinct phases where recent usage strongly predicts future needs, such as level-based games where once a player leaves a level, its assets are unlikely to be needed immediately.
-
Choose ARC when your game mixes frequently accessed core assets (main character textures, common UI elements) with transient content (temporary effects, situational audio), as ARC's dual-list approach prevents frequently used assets from being evicted by one-time loads.
According to the documentation in Books/3-1-资源模块.md, TEngine explicitly supports both strategies to accommodate these different architectural needs without requiring custom cache implementations.
Summary
- LRU evicts the least recently accessed asset when space is needed, optimizing for recency-based access patterns.
- ARC maintains separate lists for recent and frequent accesses, dynamically balancing between them to handle mixed workloads.
- Configuration occurs through
YooAssetSettings.SetDefaultCacheFileSystem()or the TEngineSettings editor UI. - Strategy changes require cache clearing via
GameModule.Resource.ClearCache()to take full effect. - Both strategies operate transparently during asset loading via
GameModule.Resource.LoadAssetAsync().
Frequently Asked Questions
How do I know which cache strategy is currently active in my TEngine project?
Check the 缓存策略 setting in the TEngineSettings editor window, or query the current configuration programmatically through the YooAsset settings API. The active strategy determines eviction behavior in DefaultCacheFileSystem.cs without requiring changes to your asset loading code.
Will changing the cache strategy delete my currently cached assets?
Changing the strategy does not automatically clear existing cached files from disk, but it changes how future evictions are decided. To ensure consistent behavior, call GameModule.Resource.ClearCache() after switching strategies to start with a clean cache state.
Can I use different cache strategies for different asset types?
No, TEngine (via YooAsset) applies a single global cache strategy for the entire DefaultCacheFileSystem. All assets managed through GameModule.Resource follow the same eviction policy. For specialized caching needs, you would need to implement custom file system logic outside the default resource module.
Does ARC consume more memory than LRU?
ARC maintains additional metadata to track both recent and frequent access lists, resulting in slightly higher memory overhead for cache management compared to LRU. However, this overhead is typically negligible compared to the asset data itself, and the improved hit rate in mixed workloads often results in better overall memory efficiency by retaining the right assets longer.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →