forked from MarcelRaschke/PostSharp.Samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAccountServices.cs
41 lines (34 loc) · 1.2 KB
/
AccountServices.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
using PostSharp.Patterns.Caching;
using System;
using System.Collections.Generic;
using System.Threading;
namespace PostSharp.Samples.Caching
{
[CacheConfiguration(ProfileName = "Account")] // See Program.cs for the configuration of the "Account" caching profile.
internal class AccountServices
{
[Cache]
public static Account GetAccount(int id)
{
Console.WriteLine($">> Retrieving the account {id} from database...");
Thread.Sleep(1000);
var account = new Account { AccountId = id };
CachingServices.CurrentContext.AddDependency(account);
return account;
}
[Cache]
public static IEnumerable<Account> GetAccountsOfCustomer(int customerId)
{
// Dependencies of GetAccount are automatically added to GetAccountsOfCustomer.
yield return GetAccount(1);
yield return GetAccount(2);
}
public static void UpdateAccount(Account account)
{
Console.WriteLine($">> Updating the account {account.AccountId} in database...");
Thread.Sleep(1000);
// This will invalidate both GetAccount and GetAccountsOfCustomer.
CachingServices.Invalidation.Invalidate(account);
}
}
}