|
| 1 | +# Cache Architecture |
| 2 | + |
| 3 | +## Flow Diagram |
| 4 | + |
| 5 | +``` |
| 6 | +┌─────────────────────────────────────────────────────────────┐ |
| 7 | +│ EvalAllWithResults │ |
| 8 | +│ │ |
| 9 | +│ ┌──────────────────────────────────────────────────────┐ │ |
| 10 | +│ │ For each Rule in Rules │ │ |
| 11 | +│ │ │ │ |
| 12 | +│ │ ┌───────────────────────────────────────────────┐ │ │ |
| 13 | +│ │ │ For each Input File matching Pattern │ │ │ |
| 14 | +│ │ │ │ │ │ |
| 15 | +│ │ │ 1. Generate Cache Key │ │ │ |
| 16 | +│ │ │ ├─ SHA256(rule content) │ │ │ |
| 17 | +│ │ │ └─ SHA256(input content) │ │ │ |
| 18 | +│ │ │ │ │ │ |
| 19 | +│ │ │ 2. Check Cache │ │ │ |
| 20 | +│ │ │ ├─ Hit? → Use cached result │ │ │ |
| 21 | +│ │ │ └─ Miss? → Evaluate + cache result │ │ │ |
| 22 | +│ │ │ │ │ │ |
| 23 | +│ │ └───────────────────────────────────────────────┘ │ │ |
| 24 | +│ └──────────────────────────────────────────────────────┘ │ |
| 25 | +└─────────────────────────────────────────────────────────────┘ |
| 26 | +
|
| 27 | +Cache Storage: |
| 28 | +~/.cache/mxlint/ |
| 29 | +├── {rule1-hash}-{input1-hash}.json |
| 30 | +├── {rule1-hash}-{input2-hash}.json |
| 31 | +├── {rule2-hash}-{input1-hash}.json |
| 32 | +└── ... |
| 33 | +``` |
| 34 | + |
| 35 | +## Cache Decision Flow |
| 36 | + |
| 37 | +``` |
| 38 | +┌─────────────────────────────────────┐ |
| 39 | +│ Start: Evaluate Rule on Input │ |
| 40 | +└──────────────┬──────────────────────┘ |
| 41 | + │ |
| 42 | + ▼ |
| 43 | +┌─────────────────────────────────────┐ |
| 44 | +│ Generate Cache Key │ |
| 45 | +│ - SHA256(rule content) │ |
| 46 | +│ - SHA256(input content) │ |
| 47 | +└──────────────┬──────────────────────┘ |
| 48 | + │ |
| 49 | + ▼ |
| 50 | + ┌─────────────┐ |
| 51 | + │ Cache Exists?│ |
| 52 | + └─────┬───┬────┘ |
| 53 | + │ │ |
| 54 | + Yes │ │ No |
| 55 | + │ │ |
| 56 | + ┌───────┘ └───────┐ |
| 57 | + │ │ |
| 58 | + ▼ ▼ |
| 59 | +┌──────────────┐ ┌──────────────┐ |
| 60 | +│ Cache Hit │ │ Cache Miss │ |
| 61 | +│ Load Result │ │ Evaluate Rule│ |
| 62 | +└──────┬───────┘ └──────┬───────┘ |
| 63 | + │ │ |
| 64 | + │ ▼ |
| 65 | + │ ┌──────────────┐ |
| 66 | + │ │ Save to Cache│ |
| 67 | + │ └──────┬───────┘ |
| 68 | + │ │ |
| 69 | + └───────┬───────────┘ |
| 70 | + │ |
| 71 | + ▼ |
| 72 | + ┌──────────────────────┐ |
| 73 | + │ Return Result │ |
| 74 | + └──────────────────────┘ |
| 75 | +``` |
| 76 | + |
| 77 | +## Performance Comparison |
| 78 | + |
| 79 | +### Scenario 1: First Run (Cold Cache) |
| 80 | +``` |
| 81 | +Time: 100% (baseline) |
| 82 | +Cache: 0 hits, N misses |
| 83 | +Result: All rules evaluated |
| 84 | +``` |
| 85 | + |
| 86 | +### Scenario 2: Second Run (Warm Cache, No Changes) |
| 87 | +``` |
| 88 | +Time: ~5-10% (90-95% faster) |
| 89 | +Cache: N hits, 0 misses |
| 90 | +Result: All results from cache |
| 91 | +``` |
| 92 | + |
| 93 | +### Scenario 3: Incremental Changes (1 file modified) |
| 94 | +``` |
| 95 | +Time: ~15-20% (80-85% faster) |
| 96 | +Cache: N-1 hits, 1 miss |
| 97 | +Result: 1 rule re-evaluated, rest from cache |
| 98 | +``` |
| 99 | + |
| 100 | +### Scenario 4: Rule Modified (All inputs need re-evaluation) |
| 101 | +``` |
| 102 | +Time: ~50-60% (if half the rules changed) |
| 103 | +Cache: M hits, N misses (where M = unchanged rules × inputs) |
| 104 | +Result: Changed rule re-evaluated against all inputs |
| 105 | +``` |
| 106 | + |
| 107 | +## Cache Key Generation |
| 108 | + |
| 109 | +```go |
| 110 | +// Pseudo-code |
| 111 | +func createCacheKey(rulePath, inputPath) CacheKey { |
| 112 | + ruleContent := readFile(rulePath) |
| 113 | + inputContent := readFile(inputPath) |
| 114 | + |
| 115 | + return CacheKey{ |
| 116 | + RuleHash: SHA256(ruleContent), |
| 117 | + InputHash: SHA256(inputContent), |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +## Cache File Structure |
| 123 | + |
| 124 | +Each cache file (`~/.cache/mxlint/{ruleHash}-{inputHash}.json`): |
| 125 | + |
| 126 | +```json |
| 127 | +{ |
| 128 | + "version": "v1", |
| 129 | + "cache_key": { |
| 130 | + "rule_hash": "abc123def456...", |
| 131 | + "input_hash": "789ghi012jkl..." |
| 132 | + }, |
| 133 | + "testcase": { |
| 134 | + "name": "path/to/input.yaml", |
| 135 | + "time": 0.123, |
| 136 | + "failure": null, |
| 137 | + "skipped": null |
| 138 | + } |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +## Cache Invalidation Strategy |
| 143 | + |
| 144 | +### Automatic Invalidation |
| 145 | +Cache is automatically invalidated when: |
| 146 | +- Rule file content changes (different SHA256 hash) |
| 147 | +- Input file content changes (different SHA256 hash) |
| 148 | + |
| 149 | +### Manual Invalidation |
| 150 | +Users can manually clear cache: |
| 151 | +```bash |
| 152 | +mxlint cache-clear |
| 153 | +``` |
| 154 | + |
| 155 | +### Version-based Invalidation |
| 156 | +Cache entries with different version numbers are ignored: |
| 157 | +- Current version: `v1` |
| 158 | +- Future versions will invalidate old cache entries |
| 159 | + |
| 160 | +## Cache Management Commands |
| 161 | + |
| 162 | +### View Statistics |
| 163 | +```bash |
| 164 | +mxlint cache-stats |
| 165 | + |
| 166 | +Output: |
| 167 | + Cache Statistics: |
| 168 | + Entries: 150 |
| 169 | + Total Size: 2.3 MB |
| 170 | +``` |
| 171 | + |
| 172 | +### Clear Cache |
| 173 | +```bash |
| 174 | +mxlint cache-clear |
| 175 | + |
| 176 | +Output: |
| 177 | + Cache cleared: ~/.cache/mxlint |
| 178 | +``` |
| 179 | + |
| 180 | +## Error Handling |
| 181 | + |
| 182 | +``` |
| 183 | +Cache Error → Log debug message → Continue with normal evaluation |
| 184 | +``` |
| 185 | + |
| 186 | +Cache errors never fail the lint operation: |
| 187 | +- Read error → Falls back to normal evaluation |
| 188 | +- Write error → Logs warning, continues |
| 189 | +- Invalid cache → Ignores entry, evaluates normally |
| 190 | + |
| 191 | +## Concurrency Safety |
| 192 | + |
| 193 | +The caching implementation is thread-safe: |
| 194 | +- Each goroutine handles its own cache operations |
| 195 | +- File system operations are atomic at the OS level |
| 196 | +- No shared state between goroutines |
| 197 | +- Cache misses may result in duplicate evaluations (acceptable) |
| 198 | + |
| 199 | +## Cache Location |
| 200 | + |
| 201 | +``` |
| 202 | +Default: ~/.cache/mxlint/ |
| 203 | +
|
| 204 | +Platform-specific: |
| 205 | +- Linux: ~/.cache/mxlint/ |
| 206 | +- macOS: ~/.cache/mxlint/ |
| 207 | +- Windows: %USERPROFILE%/.cache/mxlint/ |
| 208 | +``` |
| 209 | + |
| 210 | +## Scalability Considerations |
| 211 | + |
| 212 | +### Current Implementation |
| 213 | +- One file per cache entry |
| 214 | +- No size limits |
| 215 | +- No expiration policy |
| 216 | +- Simple file-based storage |
| 217 | + |
| 218 | +### Future Enhancements (if needed) |
| 219 | +- Maximum cache size limit |
| 220 | +- LRU eviction policy |
| 221 | +- Time-based expiration |
| 222 | +- Cache compression |
| 223 | +- Database backend for large caches |
| 224 | + |
0 commit comments