-
Notifications
You must be signed in to change notification settings - Fork 5
/
AzureSynchronized.cs
56 lines (43 loc) · 1.51 KB
/
AzureSynchronized.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
// This is public domain Metalama sample code.
#if TEST_OPTIONS
// @DisableCompareProgramOutput -- the output is random
#endif
using Metalama.Patterns.Caching.Aspects;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
#if METALAMA
using Metalama.Patterns.Caching;
#endif
namespace Doc.AzureSynchronized
{
public record Product( string Id, decimal Price, string? Remarks = null );
public sealed class ProductCatalogue
{
// This instance is intentionally shared between both app instances to simulate
// a shared database.
private static readonly ConcurrentDictionary<string, Product> _dbSimulator
= new() { ["corn"] = new Product( "corn", 100, "Initial record." ) };
public int DbOperationCount { get; private set; }
[Cache]
public Product GetProduct( string productId )
{
Console.WriteLine( $"Getting the product of {productId} from database." );
this.DbOperationCount++;
return _dbSimulator[productId];
}
public void Update( Product product )
{
if ( !_dbSimulator.ContainsKey( product.Id ) )
{
throw new KeyNotFoundException();
}
Console.WriteLine( $"Updating the product {product.Id}." );
this.DbOperationCount++;
_dbSimulator[product.Id] = product;
#if METALAMA
this._cachingService.Invalidate( this.GetProduct, product.Id );
#endif
}
}
}