2
2
3
3
HybridCachingProvider will combine local caching and distributed caching together.
4
4
5
+ And the most important problem is to keep the newest local cached value.
6
+
7
+ When we modify a cached value, the provider will send a message to ` EasyCaching Bus ` so that we can notify other Apps to remove the old value.
8
+
9
+ The following image shows how it runs.
10
+
11
+ ![ ] ( https://raw.githubusercontent.com/dotnetcore/EasyCaching/master/media/hybrid_details.png )
12
+
5
13
# How to use ?
6
14
7
15
## 1. Install the packages via Nuget
8
16
9
17
```
10
18
Install-Package EasyCaching.HybridCache
11
-
12
19
Install-Package EasyCaching.InMemory
13
-
14
20
Install-Package EasyCaching.Redis
21
+ Install-Package EasyCaching.Bus.Redis
15
22
```
16
23
17
24
## 2. Config in Startup class
@@ -25,28 +32,34 @@ public class Startup
25
32
{
26
33
services .AddMvc ();
27
34
28
- // Important step for using Hybrid Cache
29
- // 1. Local Cache
30
- services .AddDefaultInMemoryCache (x =>
35
+ services .AddEasyCaching (option =>
31
36
{
32
- x .Order = 1 ;
37
+ // local
38
+ option .UseInMemory (" m1" );
39
+ // distributed
40
+ option .UseRedis (config =>
41
+ {
42
+ config .DBConfig .Endpoints .Add (new Core .Configurations .ServerEndPoint (" 127.0.0.1" , 6379 ));
43
+ config .DBConfig .Database = 5 ;
44
+ }, " myredis" );
45
+
46
+ // combine local and distributed
47
+ option .UseHybrid (config =>
48
+ {
49
+ config .TopicName = " test-topic" ;
50
+ config .EnableLogging = false ;
51
+ })
52
+ // use redis bus
53
+ .WithRedisBus (busConf =>
54
+ {
55
+ busConf .Endpoints .Add (new ServerEndPoint (" 127.0.0.1" , 6380 ));
56
+ });
33
57
});
34
- // 2. Distributed Cache
35
- services .AddDefaultRedisCache (option =>
36
- {
37
- option .Endpoints .Add (new ServerEndPoint (" 127.0.0.1" , 6379 ));
38
- option .Password = " " ;
39
- }, x =>
40
- {
41
- x .Order = 2 ;// this value should greater than local caching provider
42
- });
43
- // 3. Hybrid
44
- services .AddDefaultHybridCache ();
45
58
}
46
59
}
47
60
```
48
61
49
- ### 3. Call the EasyCachingProvider
62
+ ### 3. Call ` IHybridCachingProvider `
50
63
51
64
The following code show how to use EasyCachingProvider in ASP.NET Core Web API.
52
65
@@ -64,73 +77,11 @@ public class ValuesController : Controller
64
77
[HttpGet ]
65
78
public string Get ()
66
79
{
67
- // Remove
68
- _provider .Remove (" demo" );
69
-
70
80
// Set
71
81
_provider .Set (" demo" , " 123" , TimeSpan .FromMinutes (1 ));
72
-
73
- // Get
74
- var res = _provider .Get (" demo" , () => " 456" , TimeSpan .FromMinutes (1 ));
75
-
76
- // Get without data retriever
77
- var res = _provider .Get <string >(" demo" );
78
-
79
- // Remove Async
80
- await _provider .RemoveAsync (" demo" );
81
-
82
- // Set Async
83
- await _provider .SetAsync (" demo" , " 123" , TimeSpan .FromMinutes (1 ));
84
-
85
- // Get Async
86
- var res = await _provider .GetAsync (" demo" ,async () => await Task .FromResult (" 456" ), TimeSpan .FromMinutes (1 ));
87
-
88
- // Get without data retriever Async
89
- var res = await _provider .GetAsync <string >(" demo" );
90
-
91
- // Refresh
92
- _provider .Refresh (" key" , " 123" , TimeSpan .FromMinutes (1 ));
93
-
94
- // Refresh Async
95
- await _provider .RefreshAsync (" key" , " 123" , TimeSpan .FromMinutes (1 ));
96
-
97
- // RemoveByPrefix
98
- _provider .RemoveByPrefix (" prefix" );
99
-
100
- // RemoveByPrefix async
101
- await _provider .RemoveByPrefixAsync (" prefix" );
102
82
103
- // SetAll
104
- _provider .SetAll (new Dictionary <string , string >()
105
- {
106
- {" key:1" ," value1" },
107
- {" key:2" ," value2" }
108
- }, TimeSpan .FromMinutes (1 ));
109
-
110
- // SetAllAsync
111
- await _provider .SetAllAsync (new Dictionary <string , string >()
112
- {
113
- {" key:1" ," value1" },
114
- {" key:2" ," value2" }
115
- }, TimeSpan .FromMinutes (1 ));
116
-
117
- // GetAll
118
- var res = _provider .GetAll (new List <string > { " key:1" , " key:2" });
119
-
120
- // GetAllAsync
121
- var res = await _provider .GetAllAsync (new List <string > { " key:1" , " key:2" });
122
-
123
- // GetByPrefix
124
- var res = _provider .GetByPrefix <T >(" prefix" );
125
-
126
- // GetByPrefixAsync
127
- var res = await _provider .GetByPrefixAsync <T >(" prefix" );
128
-
129
- // RemoveAll
130
- _provider .RemoveAll (new List <string > { " key:1" , " key:2" });
131
-
132
- // RemoveAllAsync
133
- awiat _provider .RemoveAllAsync (new List <string > { " key:1" , " key:2" });
83
+ // others
84
+ // ...
134
85
}
135
86
}
136
87
```
0 commit comments