1- using System . Threading . Tasks ;
1+ using EasyCaching . Serialization . MemoryPack ;
22
33namespace EasyCaching . Demo . ConsoleApp
44{
55 using EasyCaching . Core ;
66 using EasyCaching . SQLite ;
7+ using MemoryPack ;
78 using Microsoft . Extensions . DependencyInjection ;
89 using System ;
910
@@ -16,15 +17,18 @@ static void Main(string[] args)
1617 IServiceCollection services = new ServiceCollection ( ) ;
1718 services . AddEasyCaching ( option =>
1819 {
20+ option . WithMemoryPack ( configure =>
21+ {
22+ } , "mempack" ) ;
23+
1924 option . UseInMemory ( "m1" ) ;
2025
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+
2832 option . UseSQLite ( c =>
2933 {
3034 c . DBConfig = new SQLiteDBOptions
@@ -43,7 +47,7 @@ static void Main(string[] args)
4347
4448 IServiceProvider serviceProvider = services . BuildServiceProvider ( ) ;
4549 var factory = serviceProvider . GetService < IEasyCachingProviderFactory > ( ) ;
46-
50+
4751 // var redisCache = factory.GetCachingProvider("r1");
4852 //
4953 // redisCache.Set<Product>("rkey", new Product() { Name = "test" }, TimeSpan.FromSeconds(20));
@@ -55,41 +59,63 @@ static void Main(string[] args)
5559 // Console.WriteLine($"redis cache get value, {redisVal.HasValue} {redisVal.IsNull} {redisVal.Value}");
5660
5761
58-
59- var mCache = factory . GetCachingProvider ( "m1" ) ;
60-
61- mCache . Set < Product > ( "mkey1" , new Product ( ) { Name = "test" } , TimeSpan . FromSeconds ( 20 ) ) ;
6262
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" ) ;
6486
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-
7187 Console . WriteLine ( $ "in-memory cache get value, { mVal . HasValue } { mVal . IsNull } { mVal . Value } ") ;
7288
73-
89+
7490 var sCache = factory . GetCachingProvider ( "s1" ) ;
7591
76-
92+
7793 sCache . Set < string > ( "skey" , "svalue" , TimeSpan . FromSeconds ( 20 ) ) ;
7894
79-
95+
8096 var sVal = sCache . Get < string > ( "skey" ) ;
8197
82-
98+
8399 Console . WriteLine ( $ "sqlite cache get value, { sVal . HasValue } { sVal . IsNull } { sVal . Value } ") ;
84100
85101 Console . ReadKey ( ) ;
86102 }
87103 }
88104
89- public class Product
105+ [ MemoryPackable ( GenerateType . CircularReference ) ]
106+ public partial class Product
90107 {
91108
109+ [ MemoryPackOrder ( 0 ) ]
92110 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 ; }
93119 }
94120}
95121
0 commit comments