1
- using System . Threading . Tasks ;
1
+ using EasyCaching . Serialization . MemoryPack ;
2
2
3
3
namespace EasyCaching . Demo . ConsoleApp
4
4
{
5
5
using EasyCaching . Core ;
6
6
using EasyCaching . SQLite ;
7
+ using MemoryPack ;
7
8
using Microsoft . Extensions . DependencyInjection ;
8
9
using System ;
9
10
@@ -16,15 +17,18 @@ static void Main(string[] args)
16
17
IServiceCollection services = new ServiceCollection ( ) ;
17
18
services . AddEasyCaching ( option =>
18
19
{
20
+ option . WithMemoryPack ( configure =>
21
+ {
22
+ } , "mempack" ) ;
23
+
19
24
option . UseInMemory ( "m1" ) ;
20
25
21
- // option.UseRedis(config =>
22
- // {
23
- // config.DBConfig = new Redis.RedisDBOptions { Configuration = "localhost" };
24
- // config.SerializerName = "json";
25
- // }, "r1");
26
- //
27
-
26
+ option . UseRedis ( ( options ) =>
27
+ {
28
+ options . SerializerName = "mempack" ;
29
+ options . DBConfig . Endpoints . Add ( new Core . Configurations . ServerEndPoint ( "localhost" , 6388 ) ) ;
30
+ } , "r1" ) ;
31
+
28
32
option . UseSQLite ( c =>
29
33
{
30
34
c . DBConfig = new SQLiteDBOptions
@@ -43,7 +47,7 @@ static void Main(string[] args)
43
47
44
48
IServiceProvider serviceProvider = services . BuildServiceProvider ( ) ;
45
49
var factory = serviceProvider . GetService < IEasyCachingProviderFactory > ( ) ;
46
-
50
+
47
51
// var redisCache = factory.GetCachingProvider("r1");
48
52
//
49
53
// redisCache.Set<Product>("rkey", new Product() { Name = "test" }, TimeSpan.FromSeconds(20));
@@ -55,41 +59,63 @@ static void Main(string[] args)
55
59
// Console.WriteLine($"redis cache get value, {redisVal.HasValue} {redisVal.IsNull} {redisVal.Value}");
56
60
57
61
58
-
59
- var mCache = factory . GetCachingProvider ( "m1" ) ;
60
-
61
- mCache . Set < Product > ( "mkey1" , new Product ( ) { Name = "test" } , TimeSpan . FromSeconds ( 20 ) ) ;
62
62
63
- var mVal1 = mCache . Get < Product > ( "mkey1" ) ;
63
+ var rCache = factory . GetCachingProvider ( "r1" ) ;
64
+
65
+ var prod = new Product ( )
66
+ {
67
+ Name = "Name1" ,
68
+ Lastname = "Lastname1" ,
69
+ Inner = new ( )
70
+ {
71
+ Name = "Name2" ,
72
+ Lastname = "Lastname2"
73
+ }
74
+ } ;
75
+
76
+ prod . Inner . Inner = prod ;
77
+ rCache . Set < Product > ( "mkey1" , prod , TimeSpan . FromSeconds ( 20 ) ) ;
78
+
79
+ var mVal1 = rCache . Get < Product > ( "mkey1" ) ;
80
+
81
+ rCache . Set < string > ( "mkey" , "mvalue" , TimeSpan . FromSeconds ( 20 ) ) ;
82
+
83
+ var mVal = rCache . Get < string > ( "mkey" ) ;
84
+
85
+ var mAllKey = rCache . GetAllKeysByPrefix ( "mk" ) ;
64
86
65
- mCache . Set < string > ( "mkey" , "mvalue" , TimeSpan . FromSeconds ( 20 ) ) ;
66
-
67
- var mVal = mCache . Get < string > ( "mkey" ) ;
68
-
69
- var mAllKey = mCache . GetAllKeysByPrefix ( "mk" ) ;
70
-
71
87
Console . WriteLine ( $ "in-memory cache get value, { mVal . HasValue } { mVal . IsNull } { mVal . Value } ") ;
72
88
73
-
89
+
74
90
var sCache = factory . GetCachingProvider ( "s1" ) ;
75
91
76
-
92
+
77
93
sCache . Set < string > ( "skey" , "svalue" , TimeSpan . FromSeconds ( 20 ) ) ;
78
94
79
-
95
+
80
96
var sVal = sCache . Get < string > ( "skey" ) ;
81
97
82
-
98
+
83
99
Console . WriteLine ( $ "sqlite cache get value, { sVal . HasValue } { sVal . IsNull } { sVal . Value } ") ;
84
100
85
101
Console . ReadKey ( ) ;
86
102
}
87
103
}
88
104
89
- public class Product
105
+ [ MemoryPackable ( GenerateType . CircularReference ) ]
106
+ public partial class Product
90
107
{
91
108
109
+ [ MemoryPackOrder ( 0 ) ]
92
110
public string Name { get ; set ; }
111
+
112
+ [ MemoryPackOrder ( 1 ) ]
113
+
114
+ public string Lastname { get ; set ; }
115
+
116
+ [ MemoryPackOrder ( 2 ) ]
117
+
118
+ public Product Inner { set ; get ; }
93
119
}
94
120
}
95
121
0 commit comments