Skip to content

Commit 89d4a66

Browse files
authored
📝 Update Hybrid After Refactoring.
1 parent 40db227 commit 89d4a66

File tree

1 file changed

+33
-82
lines changed

1 file changed

+33
-82
lines changed

docs/Hybrid.md

Lines changed: 33 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@
22

33
HybridCachingProvider will combine local caching and distributed caching together.
44

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+
513
# How to use ?
614

715
## 1. Install the packages via Nuget
816

917
```
1018
Install-Package EasyCaching.HybridCache
11-
1219
Install-Package EasyCaching.InMemory
13-
1420
Install-Package EasyCaching.Redis
21+
Install-Package EasyCaching.Bus.Redis
1522
```
1623

1724
## 2. Config in Startup class
@@ -25,28 +32,34 @@ public class Startup
2532
{
2633
services.AddMvc();
2734

28-
//Important step for using Hybrid Cache
29-
//1. Local Cache
30-
services.AddDefaultInMemoryCache(x=>
35+
services.AddEasyCaching(option =>
3136
{
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+
});
3357
});
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();
4558
}
4659
}
4760
```
4861

49-
### 3. Call the EasyCachingProvider
62+
### 3. Call `IHybridCachingProvider`
5063

5164
The following code show how to use EasyCachingProvider in ASP.NET Core Web API.
5265

@@ -64,73 +77,11 @@ public class ValuesController : Controller
6477
[HttpGet]
6578
public string Get()
6679
{
67-
//Remove
68-
_provider.Remove("demo");
69-
7080
//Set
7181
_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");
10282

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+
//...
13485
}
13586
}
13687
```

0 commit comments

Comments
 (0)