|
| 1 | +# Migration Guide: v2.x to v3.0 |
| 2 | + |
| 3 | +Migration guide for **Neolution.Extensions.Caching v3.0**. |
| 4 | + |
| 5 | +## Breaking Changes |
| 6 | + |
| 7 | +### Options Pattern Correction |
| 8 | + |
| 9 | +Options classes now properly inherit from `DistributedCacheOptionsBase` instead of implementing `IOptions<T>`. |
| 10 | + |
| 11 | +**Impact**: Only affects code that directly instantiates options classes (rare). Service registration is unaffected. |
| 12 | + |
| 13 | +### Method Rename: AddMessagePackDistributedCache → AddSerializedDistributedCache |
| 14 | + |
| 15 | +The registration method has been renamed for clarity. |
| 16 | + |
| 17 | +**v2.x**: |
| 18 | +```csharp |
| 19 | +services.AddMessagePackDistributedCache(); |
| 20 | +``` |
| 21 | + |
| 22 | +**v3.0**: |
| 23 | +```csharp |
| 24 | +services.AddSerializedDistributedCache(); |
| 25 | +``` |
| 26 | + |
| 27 | +The old method still works but is marked `[Obsolete]` and will be removed in a later release. |
| 28 | + |
| 29 | +### Provider Registration Validation |
| 30 | + |
| 31 | +`AddSerializedDistributedCache()` now validates that a cache provider is registered **before** the wrapper. |
| 32 | + |
| 33 | +**v2.x** - Failed silently at runtime: |
| 34 | +```csharp |
| 35 | +services.AddMessagePackDistributedCache(); // No error until you use it |
| 36 | +``` |
| 37 | + |
| 38 | +**v3.0** - Fails immediately at registration: |
| 39 | +```csharp |
| 40 | +services.AddSerializedDistributedCache(); |
| 41 | +// InvalidOperationException: "An IDistributedCache provider must be registered..." |
| 42 | +``` |
| 43 | + |
| 44 | +**Required Order**: |
| 45 | +```csharp |
| 46 | +services.AddStackExchangeRedisCache(...); // Provider first |
| 47 | +services.AddSerializedDistributedCache(); // Wrapper second |
| 48 | +``` |
| 49 | + |
| 50 | +### Fluent API Return Types |
| 51 | + |
| 52 | +All extension methods now return `IServiceCollection` for chaining. |
| 53 | + |
| 54 | +```csharp |
| 55 | +// v3.0 enables chaining |
| 56 | +services.AddStackExchangeRedisCache(options => { ... }) |
| 57 | + .AddSerializedDistributedCache() |
| 58 | + .AddLogging(); |
| 59 | +``` |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +## New Configuration Options |
| 64 | + |
| 65 | +Version 3.0 exposes configuration properties that were previously hardcoded or inaccessible: |
| 66 | + |
| 67 | +### EnableKeyEncoding (Distributed Caches Only) |
| 68 | + |
| 69 | +Controls URL encoding of optional keys. **Default: `true`** |
| 70 | + |
| 71 | +```csharp |
| 72 | +services.AddSerializedDistributedCache(options => |
| 73 | +{ |
| 74 | + options.EnableKeyEncoding = false; // Disable if you have legacy keys |
| 75 | +}); |
| 76 | +``` |
| 77 | + |
| 78 | +- **true**: `"user:123"` → `"user%3A123"` (safe for all backends) |
| 79 | +- **false**: `"user:123"` → `"user:123"` (v2.x behavior) |
| 80 | + |
| 81 | +### EnableKeyLengthValidation (Distributed Caches Only) |
| 82 | + |
| 83 | +Validates keys don't exceed 250 bytes. **Default: `true`** |
| 84 | + |
| 85 | +```csharp |
| 86 | +services.AddSerializedDistributedCache(options => |
| 87 | +{ |
| 88 | + options.EnableKeyLengthValidation = false; // If your backend supports long keys |
| 89 | +}); |
| 90 | +``` |
| 91 | + |
| 92 | +Prevents issues with Memcached and other backends with key length limits. |
| 93 | + |
| 94 | +--- |
| 95 | + |
| 96 | +## Migration Checklist |
| 97 | + |
| 98 | +### Step 1: Update Packages |
| 99 | + |
| 100 | +```bash |
| 101 | +dotnet add package Neolution.Extensions.Caching.InMemory --version 3.0.0 |
| 102 | +dotnet add package Neolution.Extensions.Caching.Distributed --version 3.0.0 |
| 103 | +dotnet add package Neolution.Extensions.Caching.RedisHybrid --version 3.0.0 |
| 104 | +``` |
| 105 | + |
| 106 | +### Step 2: Update Service Registration |
| 107 | + |
| 108 | +Replace deprecated method name (or ignore warning): |
| 109 | + |
| 110 | +```csharp |
| 111 | +// Old (still works, but obsolete) |
| 112 | +services.AddMessagePackDistributedCache(); |
| 113 | + |
| 114 | +// New (recommended) |
| 115 | +services.AddSerializedDistributedCache(); |
| 116 | +``` |
| 117 | + |
| 118 | +### Step 3: Verify Provider Registration Order |
| 119 | + |
| 120 | +Ensure provider comes **before** wrapper: |
| 121 | + |
| 122 | +```csharp |
| 123 | +// Correct |
| 124 | +services.AddStackExchangeRedisCache(options => { ... }); |
| 125 | +services.AddSerializedDistributedCache(); |
| 126 | + |
| 127 | +// Wrong - throws InvalidOperationException |
| 128 | +services.AddSerializedDistributedCache(); |
| 129 | +services.AddStackExchangeRedisCache(options => { ... }); |
| 130 | +``` |
| 131 | + |
| 132 | +### Step 4: Test Your Application |
| 133 | + |
| 134 | +- Verify cache operations work as expected |
| 135 | +- Check logs for obsolete warnings |
| 136 | +- Monitor cache hit rates |
| 137 | + |
| 138 | +--- |
| 139 | + |
| 140 | +## Optional: Leverage New Features |
| 141 | + |
| 142 | +### Cache Key Versioning |
| 143 | + |
| 144 | +Invalidate all cache entries by incrementing version: |
| 145 | + |
| 146 | +```csharp |
| 147 | +services.AddSerializedDistributedCache(options => |
| 148 | +{ |
| 149 | + options.Version = 1; // Keys: "MyCacheId:v1:UserProfile" |
| 150 | +}); |
| 151 | + |
| 152 | +// Later: increment to invalidate all entries |
| 153 | +options.Version = 2; // Keys: "MyCacheId:v2:UserProfile" |
| 154 | +``` |
| 155 | + |
| 156 | +**Default**: `null` (no version) - maintains v2.x compatibility. |
| 157 | + |
| 158 | +### Environment Isolation |
| 159 | + |
| 160 | +Prevent cache collisions when sharing backends: |
| 161 | + |
| 162 | +```csharp |
| 163 | +services.AddSerializedDistributedCache(options => |
| 164 | +{ |
| 165 | + options.EnvironmentPrefix = "staging"; // Keys: "staging:MyCacheId:UserProfile" |
| 166 | +}); |
| 167 | +``` |
| 168 | + |
| 169 | +**Default**: `null` (no prefix) - maintains v2.x compatibility. |
| 170 | + |
| 171 | +### Refactor-Safe Keys |
| 172 | + |
| 173 | +Protect distributed cache keys from enum renames: |
| 174 | + |
| 175 | +```csharp |
| 176 | +public enum MyCacheId |
| 177 | +{ |
| 178 | + [CacheKey("user-profile")] // Can rename enum without breaking cache |
| 179 | + UserProfile = 0, |
| 180 | + |
| 181 | + ProductCatalog = 1 // Renaming breaks cache keys |
| 182 | +} |
| 183 | +``` |
| 184 | + |
| 185 | +**Note**: `[CacheKey]` only applies to distributed caches. In-memory cache ignores it. |
| 186 | + |
| 187 | +## FAQ |
| 188 | + |
| 189 | +### Will my existing cache entries still work? |
| 190 | + |
| 191 | +**Yes**, if you: |
| 192 | +- Don't set `Version` (default) |
| 193 | +- Don't set `EnvironmentPrefix` (default) |
| 194 | +- Keep `EnableKeyEncoding = true` (default) |
| 195 | + |
| 196 | +Cache key format remains compatible with v2.x by default. |
| 197 | + |
| 198 | +### Will this affect performance? |
| 199 | + |
| 200 | +Minimal impact: |
| 201 | +- URL encoding: Only on optional keys (negligible) |
| 202 | +- Length validation: Simple string length check (negligible) |
| 203 | +- Version/prefix: String concatenation (negligible) |
| 204 | + |
| 205 | +## Summary |
| 206 | + |
| 207 | +v3.0 improves configuration and error handling for distributed cache: |
| 208 | + |
| 209 | +- Better error messages (early validation) |
| 210 | +- Exposed configuration properties |
| 211 | +- Fluent API support |
| 212 | +- Refactor-safe cache keys |
| 213 | + |
| 214 | +New features are opt-in. Existing cache entries remain compatible with default settings (no `Version`, no `EnvironmentPrefix`, `EnableKeyEncoding = true`). |
| 215 | + |
| 216 | +**Need Help?** Open an issue on GitHub. |
0 commit comments