// MIT License - Copyright (c) 2026 wallstop // Full license text: https://github.com/wallstop/unity-helpers/blob/main/LICENSE namespace WallstopStudios.UnityHelpers.Core.DataStructure { /// /// Provides factory methods for creating pre-configured instances /// optimized for common game development scenarios. /// /// /// /// Each method returns a that can be further customized /// before calling or /// . /// /// /// These presets are designed to provide sensible defaults for typical use cases in Unity games, /// balancing memory usage, performance, and data freshness requirements. /// /// /// /// positionCache = CachePresets.ShortLived().Build(); /// /// // Create a high-throughput cache with custom eviction callback /// Cache pathCache = CachePresets.HighThroughput() /// .OnEviction((key, value, reason) => Debug.Log($"Path {key} evicted: {reason}")) /// .Build(); /// ]]> /// public static class CachePresets { /// /// Creates a cache builder configured for short-lived, frequently-changing data. /// /// /// /// This preset is ideal for frame-local computations, temporary lookups, and transient data /// that changes frequently and should not persist for long periods. /// /// /// Configuration: /// /// Maximum Size: 100 entries /// Expiration: 60 seconds after write /// Eviction Policy: LRU (Least Recently Used) /// /// /// /// The type of keys in the cache. /// The type of values in the cache. /// /// A configured for short-lived data that can be /// further customized before building. /// /// /// distanceCache = CachePresets.ShortLived<(int, int), float>().Build(); /// /// // Cache with custom loader for frame-local data /// Cache tempObjects = CachePresets.ShortLived() /// .Build(key => GameObject.Find(key)); /// ]]> /// public static CacheBuilder ShortLived() { CacheBuilder builder = CacheBuilder .NewBuilder() .MaximumSize(100) .ExpireAfterWrite(60f) .EvictionPolicy(EvictionPolicy.Lru); return builder; } /// /// Creates a cache builder configured for persistent data that rarely changes. /// /// /// /// This preset is ideal for asset references, prefab lookups, configuration data, and other /// long-lived content that should remain cached for the duration of the session. /// /// /// Configuration: /// /// Maximum Size: 1000 entries /// Expiration: None (entries persist until evicted by size) /// Eviction Policy: LRU (Least Recently Used) /// /// /// /// The type of keys in the cache. /// The type of values in the cache. /// /// A configured for long-lived data that can be /// further customized before building. /// /// /// prefabCache = CachePresets.LongLived() /// .Build(key => Resources.Load($"Prefabs/{key}")); /// /// // Cache for configuration lookups /// Cache enemyConfigs = CachePresets.LongLived().Build(); /// ]]> /// public static CacheBuilder LongLived() { CacheBuilder builder = CacheBuilder .NewBuilder() .MaximumSize(1000) .ExpireAfterWrite(-1f) .EvictionPolicy(EvictionPolicy.Lru); return builder; } /// /// Creates a cache builder configured for player session data with sliding expiration. /// /// /// /// This preset is ideal for player-specific data, gameplay state, inventory lookups, and /// other session-bound information that should expire based on access patterns rather than /// absolute time. /// /// /// Configuration: /// /// Maximum Size: 500 entries /// Expiration: 30 minutes (1800 seconds) after last access (sliding window) /// Eviction Policy: LRU (Least Recently Used) /// /// /// /// The type of keys in the cache. /// The type of values in the cache. /// /// A configured for session data that can be /// further customized before building. /// /// /// inventoryCache = CachePresets.SessionCache().Build(); /// /// // Cache for quest progress with custom maximum size /// Cache questCache = CachePresets.SessionCache() /// .MaximumSize(100) /// .Build(); /// ]]> /// public static CacheBuilder SessionCache() { CacheBuilder builder = CacheBuilder .NewBuilder() .MaximumSize(500) .ExpireAfterAccess(1800f) .EvictionPolicy(EvictionPolicy.Lru); return builder; } /// /// Creates a cache builder configured for high-frequency access scenarios. /// /// /// /// This preset is ideal for AI pathfinding results, physics query caching, spatial lookups, /// and other performance-critical systems that require high throughput and can benefit from /// dynamic growth under load. /// /// /// Configuration: /// /// Maximum Size: 2000 entries /// Expiration: 5 minutes (300 seconds) after write /// Eviction Policy: SLRU (Segmented Least Recently Used) for better hit rates /// Statistics: Enabled for monitoring performance /// Growth: Allows 1.5x growth up to 4000 entries when thrashing is detected /// /// /// /// The type of keys in the cache. /// The type of values in the cache. /// /// A configured for high-throughput scenarios that can be /// further customized before building. /// /// /// pathCache = CachePresets.HighThroughput<(Vector3, Vector3), NavMeshPath>() /// .Build(); /// /// // Cache for physics overlap queries with custom expiration /// Cache overlapCache = CachePresets.HighThroughput() /// .ExpireAfterWrite(60f) /// .Build(); /// ]]> /// public static CacheBuilder HighThroughput() { CacheBuilder builder = CacheBuilder .NewBuilder() .MaximumSize(2000) .ExpireAfterWrite(300f) .EvictionPolicy(EvictionPolicy.Slru) .RecordStatistics() .AllowGrowth(1.5f, 4000); return builder; } /// /// Creates a cache builder configured for render-related data. /// /// /// /// This preset is ideal for shader parameter caching, material property lookups, render state, /// and other graphics-related data that changes frequently but benefits from short-term caching. /// /// /// Configuration: /// /// Maximum Size: 200 entries /// Expiration: 30 seconds after write /// Eviction Policy: FIFO (First In First Out) for predictable eviction order /// /// /// /// The type of keys in the cache. /// The type of values in the cache. /// /// A configured for render data that can be /// further customized before building. /// /// /// propBlockCache = CachePresets.RenderCache() /// .Build(); /// /// // Cache for computed shader parameters /// Cache shaderParamCache = CachePresets.RenderCache() /// .MaximumSize(50) /// .Build(); /// ]]> /// public static CacheBuilder RenderCache() { CacheBuilder builder = CacheBuilder .NewBuilder() .MaximumSize(200) .ExpireAfterWrite(30f) .EvictionPolicy(EvictionPolicy.Fifo); return builder; } /// /// Creates a cache builder configured for network and API responses. /// /// /// /// This preset is ideal for REST API responses, server data, leaderboard entries, and other /// network-fetched content that should be cached temporarily to reduce server load and improve /// responsiveness. /// /// /// Configuration: /// /// Maximum Size: 100 entries /// Expiration: 2 minutes (120 seconds) after write /// Jitter: Up to 12 seconds to prevent thundering herd on cache refresh /// Eviction Policy: LRU (Least Recently Used) /// /// /// /// The type of keys in the cache. /// The type of values in the cache. /// /// A configured for network data that can be /// further customized before building. /// /// /// apiCache = CachePresets.NetworkCache() /// .Build(); /// /// // Cache for leaderboard data with longer expiration /// Cache leaderboardCache = CachePresets.NetworkCache() /// .ExpireAfterWrite(300f) /// .Build(); /// ]]> /// public static CacheBuilder NetworkCache() { CacheBuilder builder = CacheBuilder .NewBuilder() .MaximumSize(100) .ExpireAfterWrite(120f) .WithJitter(12f) .EvictionPolicy(EvictionPolicy.Lru); return builder; } } }