|
| 1 | +using System.Linq.Expressions; |
| 2 | +using BitFaster.Caching.Lru.Builder; |
| 3 | + |
| 4 | +namespace BitFaster.Caching.Lru |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Factory class for creating ConcurrentLru variants. |
| 8 | + /// </summary> |
| 9 | + internal static class LruFactory<K, V> |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// Creates a ConcurrentLru instance based on the provided LruInfo. |
| 13 | + /// </summary> |
| 14 | + /// <param name="info">The LruInfo</param> |
| 15 | + /// <returns>A ConcurrentLru</returns> |
| 16 | + internal static ICache<K, V> CreateConcurrent(LruInfo<K> info) |
| 17 | + { |
| 18 | + if (info.TimeToExpireAfterWrite.HasValue && info.TimeToExpireAfterAccess.HasValue) |
| 19 | + Throw.InvalidOp("Specifying both ExpireAfterWrite and ExpireAfterAccess is not supported."); |
| 20 | + |
| 21 | + return (info.WithMetrics, info.TimeToExpireAfterWrite.HasValue, info.TimeToExpireAfterAccess.HasValue) switch |
| 22 | + { |
| 23 | + (true, false, false) => new ConcurrentLru<K, V>(info.ConcurrencyLevel, info.Capacity, info.KeyComparer), |
| 24 | + (true, true, false) => new ConcurrentTLru<K, V>(info.ConcurrencyLevel, info.Capacity, info.KeyComparer, info.TimeToExpireAfterWrite.Value), |
| 25 | + (false, true, false) => new FastConcurrentTLru<K, V>(info.ConcurrencyLevel, info.Capacity, info.KeyComparer, info.TimeToExpireAfterWrite.Value), |
| 26 | + (true, false, true) => CreateExpireAfterAccess<TelemetryPolicy<K, V>>(info), |
| 27 | + (false, false, true) => CreateExpireAfterAccess<NoTelemetryPolicy<K, V>>(info), |
| 28 | + _ => new FastConcurrentLru<K, V>(info.ConcurrencyLevel, info.Capacity, info.KeyComparer), |
| 29 | + }; |
| 30 | + } |
| 31 | + |
| 32 | + private static ICache<K, V> CreateExpireAfterAccess<TP>(LruInfo<K> info) where TP : struct, ITelemetryPolicy<K, V> |
| 33 | + { |
| 34 | + return new ConcurrentLruCore<K, V, LongTickCountLruItem<K, V>, AfterAccessLongTicksPolicy<K, V>, TP>( |
| 35 | + info.ConcurrencyLevel, info.Capacity, info.KeyComparer, new AfterAccessLongTicksPolicy<K, V>(info.TimeToExpireAfterAccess.Value), default); |
| 36 | + } |
| 37 | + } |
| 38 | +} |
0 commit comments