| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Text.RegularExpressions; |
| | | 4 | | |
| | | 5 | | namespace SwitchBlade.Core |
| | | 6 | | { |
| | | 7 | | /// <summary> |
| | | 8 | | /// Thread-safe LRU cache for compiled regex patterns. |
| | | 9 | | /// Extracted from MainViewModel to follow Single Responsibility Principle. |
| | | 10 | | /// </summary> |
| | | 11 | | public class LruRegexCache : IRegexCache |
| | | 12 | | { |
| | 26 | 13 | | private readonly Dictionary<string, (Regex Regex, LinkedListNode<string> Node)> _cache = new(); |
| | 26 | 14 | | private readonly LinkedList<string> _lruList = new(); |
| | 26 | 15 | | private readonly object _lock = new(); |
| | | 16 | | private readonly int _maxSize; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Creates a new LRU regex cache with the specified capacity. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="maxSize">Maximum number of patterns to cache.</param> |
| | 26 | 22 | | public LruRegexCache(int maxSize = 50) |
| | 26 | 23 | | { |
| | 26 | 24 | | if (maxSize <= 0) |
| | 2 | 25 | | throw new ArgumentOutOfRangeException(nameof(maxSize), "Max size must be positive."); |
| | | 26 | | |
| | 24 | 27 | | _maxSize = maxSize; |
| | 24 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public Regex? GetOrCreate(string pattern) |
| | 21 | 32 | | { |
| | 21 | 33 | | if (string.IsNullOrEmpty(pattern)) |
| | 2 | 34 | | return null; |
| | | 35 | | |
| | 19 | 36 | | lock (_lock) |
| | 19 | 37 | | { |
| | 19 | 38 | | if (_cache.TryGetValue(pattern, out var entry)) |
| | 3 | 39 | | { |
| | | 40 | | // Move to front (LRU update) - O(1) |
| | 3 | 41 | | _lruList.Remove(entry.Node); |
| | 3 | 42 | | _lruList.AddFirst(entry.Node); |
| | 3 | 43 | | return entry.Regex; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | try |
| | 16 | 47 | | { |
| | | 48 | | // NonBacktracking prevents ReDoS for user-provided patterns |
| | | 49 | | // Available in .NET 7+ |
| | 16 | 50 | | var regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions |
| | | 51 | | |
| | | 52 | | // Add to cache - O(1) |
| | 14 | 53 | | var node = _lruList.AddFirst(pattern); |
| | 14 | 54 | | _cache[pattern] = (regex, node); |
| | | 55 | | |
| | | 56 | | // Evict if exceeded capacity |
| | 16 | 57 | | while (_cache.Count > _maxSize && _lruList.Count > 0) |
| | 2 | 58 | | { |
| | 2 | 59 | | var last = _lruList.Last; |
| | 2 | 60 | | if (last != null) |
| | 2 | 61 | | { |
| | 2 | 62 | | _cache.Remove(last.Value); |
| | 2 | 63 | | _lruList.RemoveLast(); |
| | 2 | 64 | | } |
| | 2 | 65 | | } |
| | | 66 | | |
| | 14 | 67 | | return regex; |
| | | 68 | | } |
| | 2 | 69 | | catch (ArgumentException) |
| | 2 | 70 | | { |
| | | 71 | | // Invalid regex pattern |
| | 2 | 72 | | return null; |
| | | 73 | | } |
| | | 74 | | } |
| | 21 | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <inheritdoc /> |
| | | 78 | | public void Clear() |
| | 1 | 79 | | { |
| | 1 | 80 | | lock (_lock) |
| | 1 | 81 | | { |
| | 1 | 82 | | _cache.Clear(); |
| | 1 | 83 | | _lruList.Clear(); |
| | 1 | 84 | | } |
| | 1 | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Gets the current number of cached patterns. |
| | | 89 | | /// </summary> |
| | | 90 | | public int Count |
| | | 91 | | { |
| | | 92 | | get |
| | 2 | 93 | | { |
| | 2 | 94 | | lock (_lock) |
| | 2 | 95 | | { |
| | 2 | 96 | | return _cache.Count; |
| | | 97 | | } |
| | 2 | 98 | | } |
| | | 99 | | } |
| | | 100 | | } |
| | | 101 | | } |