-
Notifications
You must be signed in to change notification settings - Fork 5
/
InvalidateAspect.cs
58 lines (45 loc) · 1.75 KB
/
InvalidateAspect.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// This is public domain Metalama sample code.
using Metalama.Patterns.Caching.Aspects;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Doc.InvalidateAspect
{
public sealed class ProductCatalogue
{
private readonly Dictionary<string, decimal> _dbSimulator = new() { ["corn"] = 100 };
public int DbOperationCount { get; private set; }
[Cache]
public string[] GetProducts()
{
Console.WriteLine( "Getting the product list from database." );
this.DbOperationCount++;
return this._dbSimulator.Keys.ToArray();
}
[Cache] /*<Cache>*/
public decimal GetPrice( string productId ) /*</Cache>*/
{
Console.WriteLine( $"Getting the price of {productId} from database." );
this.DbOperationCount++;
return this._dbSimulator[productId];
}
[InvalidateCache( nameof(GetProducts) )]
public void AddProduct( string productId, decimal price )
{
Console.WriteLine( $"Adding the product {productId}." );
this.DbOperationCount++;
this._dbSimulator.Add( productId, price );
}
[InvalidateCache( nameof(GetPrice) )] /*<InvalidateCache>*/
public void UpdatePrice( string productId, decimal price ) /*</InvalidateCache>*/
{
if ( !this._dbSimulator.ContainsKey( productId ) )
{
throw new KeyNotFoundException();
}
Console.WriteLine( $"Updating the price of {productId}." );
this.DbOperationCount++;
this._dbSimulator[productId] = price;
}
}
}