|
| 1 | + |
| 2 | +Hello! |
| 3 | + |
| 4 | +It's Fedja from Metalama again. In my previous emails, I described Metalama as a ,meta-programming _framework_ that allows you to generate and validate code as you type. |
| 5 | + |
| 6 | +If you looked at our API and documentation, you may have got the impression that the framework is deep and complex. |
| 7 | + |
| 8 | +Good news is, **you don't need to write your own aspects**. You can find open-source and professionally supported aspects on [Metalama Marketplace](https://www.postsharp.net/metalama/marketplace), including code contracts, caching, observability (INotifyPropertyChanged), WPF commands and dependency properties, and much more. |
| 9 | + |
| 10 | +Let's look at caching today. |
| 11 | + |
| 12 | +## Adding Caching to Your App |
| 13 | + |
| 14 | +Metalama supports caching with and without Dependency Injection (DI). In our first example, we will explore its usage in a project that employs DI. |
| 15 | + |
| 16 | +You can add caching to your app in just three steps: |
| 17 | + |
| 18 | +1. Add the [Metalama.Patterns.Caching.Aspects](https://www.nuget.org/packages/Metalama.Patterns.Caching.Aspects/) package to your project. |
| 19 | +2. Navigate to all methods that need caching and add the `[Cache]` custom attribute. |
| 20 | + |
| 21 | + |
| 22 | + ```c# |
| 23 | + using Metalama.Patterns.Caching.Aspects; |
| 24 | + |
| 25 | + namespace CreatingAspects.Caching |
| 26 | + { |
| 27 | + public sealed class CloudCalculator |
| 28 | + { |
| 29 | + |
| 30 | + [Cache] |
| 31 | + public int Add(int a, int b) |
| 32 | + { |
| 33 | + Console.WriteLine("Doing some very hard work."); |
| 34 | + |
| 35 | + this.OperationCount++; |
| 36 | + |
| 37 | + Console.WriteLine("Finished doing some very hard work."); |
| 38 | + |
| 39 | + return a + b; |
| 40 | + } |
| 41 | + |
| 42 | + public int OperationCount { get; private set; } |
| 43 | + } |
| 44 | + } |
| 45 | + ``` |
| 46 | + |
| 47 | +3. Go to the application startup code and call `AddMetalamaCaching`. This adds the `ICachingService` interface to your `IServiceCollection`, enabling the `[Cache]` aspect to be used on all objects instantiated by the DI container. |
| 48 | + |
| 49 | + ```c# |
| 50 | + builder.Services.AddMetalamaCaching(); |
| 51 | + ``` |
| 52 | + |
| 53 | + This will use the `MemoryCache` by default. If you want to [use Redis](https://doc.postsharp.net/metalama/preview/patterns/caching/redis), do this: |
| 54 | +
|
| 55 | + ```c# |
| 56 | + builder.Services.AddMetalamaCaching( caching => caching.WithBackend( backend => backend.Redis() ) ); |
| 57 | + ``` |
| 58 | + |
| 59 | + Want a `MemoryCache` _in front_ of your Redis cache? No problem. The service will listen to Redis notifications to invalidate the L1 cache. |
| 60 | + |
| 61 | + ```c# |
| 62 | + builder.Services.AddMetalamaCaching( |
| 63 | + caching => caching.WithBackend( |
| 64 | + backend => backend.Redis().WithL1() ) ); |
| 65 | + |
| 66 | + ``` |
| 67 | + |
| 68 | +## What Metalama does for you |
| 69 | + |
| 70 | +At build time, Metalama transforms your code on-the-fly: |
| 71 | +* It pulls the dependency to `ICachingService`. |
| 72 | +* It generates the [cache key](https://doc.postsharp.net/metalama/preview/patterns/caching/caching-keys). |
| 73 | +* It wraps your cached method into a delegate before calling `ICachingService.GetOrAdd`. |
| 74 | + |
| 75 | +Other features of this open-source caching library include: |
| 76 | + |
| 77 | +* Serialization. |
| 78 | +* Robust [invalidation](https://doc.postsharp.net/metalama/preview/patterns/caching/invalidation). Say goodbye to the cache key hell. |
| 79 | +* Multi-node [synchronization](https://doc.postsharp.net/metalama/preview/patterns/caching/pubsub) over Redis or Azure Service Bus. |
| 80 | +* Transparent handling of weird types like `IEnumerable` or `Stream`. |
| 81 | +* [Locking](https://doc.postsharp.net/metalama/preview/patterns/caching/locking). |
| 82 | +* Compatible with .NET Aspire. |
| 83 | + |
| 84 | +## Conclusion |
| 85 | + |
| 86 | +Applying caching to an application can dramatically improve performance, but implementing the pattern is not straightforward. Metalama does all the heavy lifting for you and provides several flexible implementations that you can customize to meet your specific requirements. |
| 87 | + |
| 88 | +By leveraging Metalama, you'll find that implementing caching is both simpler and more efficient than creating a bespoke solution. |
| 89 | + |
| 90 | +Caching is just one of the open-source aspects built by our team. For more, check [Metalama Marketplace](https://www.postsharp.net/metalama/marketplace). |
0 commit comments