Parcourir la source

add basic redis

Karathan il y a 6 ans
Parent
révision
9120e83bbe

+ 1
- 0
AuctionTracker/AuctionTracker.csproj Voir le fichier

@@ -15,6 +15,7 @@
15 15
     <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" />
16 16
     <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.1.1" />
17 17
     <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
18
+    <PackageReference Include="ServiceStack.Redis.Core" Version="5.1.0" />
18 19
   </ItemGroup>
19 20
 
20 21
 </Project>

+ 25
- 0
AuctionTracker/Program.cs Voir le fichier

@@ -13,6 +13,8 @@ using log4net.Config;
13 13
 using Microsoft.Extensions.Configuration;
14 14
 using Microsoft.Extensions.Configuration.FileExtensions;
15 15
 using Microsoft.Extensions.Configuration.Json;
16
+using ServiceStack.Redis;
17
+using ServiceStack.Redis.Generic;
16 18
 
17 19
 namespace AuctionTracker
18 20
 {
@@ -25,6 +27,7 @@ namespace AuctionTracker
25 27
         private static IConfiguration _configuration;
26 28
         private static ILog _logger;
27 29
 
30
+        private static RedisManagerPool _redisPool;
28 31
         private static string _character;
29 32
         private static string _locale;
30 33
         private static string _realm;
@@ -80,6 +83,26 @@ namespace AuctionTracker
80 83
                 }
81 84
             }
82 85
             lastAuctions = auctions;
86
+            //update redis database
87
+            using (IRedisClient client = _redisPool.GetClient())
88
+            {
89
+                var auctionClient = client.As<RedisAuctionSnapshot>();
90
+                foreach(Auction auction in auctions)
91
+                {
92
+                    RedisAuctionSnapshot snapshot = await RedisAuctionSnapshot.FromAuctionClientAsync(auction, _client, _locale);
93
+                    auctionClient.Store(snapshot, TimeSpan.FromMinutes(20d));
94
+                }
95
+            }
96
+            //retrieving stuff
97
+            _logger.Info("Retrieving stored values");
98
+            using (IRedisClient client = _redisPool.GetClient())
99
+            {
100
+                var auctionClient = client.As<RedisAuctionSnapshot>();
101
+                foreach(RedisAuctionSnapshot item in auctionClient.GetAll())
102
+                {
103
+                    _logger.Info(item.Name);
104
+                }
105
+            }
83 106
         }
84 107
         static async Task Main(string[] args)
85 108
         {
@@ -99,6 +122,8 @@ namespace AuctionTracker
99 122
             _logger.Info("Creating Clients");
100 123
             _client = new BlizzSharpClient($"{_configuration["blizzardApiKey"]}");
101 124
             _pushClient = new PushClient($"{_configuration["pushoverSecretKey"]}", $"{_configuration["pushoverUserKey"]}");
125
+            _logger.Info("Initializing redis pool");
126
+            _redisPool = new RedisManagerPool("localhost:6379");
102 127
             _logger.Info("AuctionTracker started, entering loop");
103 128
 
104 129
             while(true)

+ 29
- 0
AuctionTracker/RedisAuctionSnapshot.cs Voir le fichier

@@ -0,0 +1,29 @@
1
+using System;
2
+using System.Threading.Tasks;
3
+
4
+namespace AuctionTracker
5
+{
6
+    public class RedisAuctionSnapshot
7
+    {
8
+        public long Gold {get;set;}
9
+        public long Silver {get;set;}
10
+        public long Copper {get;set;}
11
+        public string Name {get;set;}
12
+
13
+        public long Id {get;set;}
14
+        private RedisAuctionSnapshot(BlizzSharp.Data.Auction auction, BlizzSharp.Data.Item item)
15
+        {
16
+            Gold = auction.Buyout / 10000;
17
+            Silver = (auction.Buyout - (Gold*10000)) / 100;
18
+            Copper = (auction.Buyout - (Gold*10000) - (Silver * 100));
19
+            Name = item.Name;
20
+            Id = auction.Id;
21
+        }
22
+
23
+        public static async Task<RedisAuctionSnapshot> FromAuctionClientAsync(BlizzSharp.Data.Auction auction, BlizzSharp.BlizzSharpClient client, string locale)
24
+        {
25
+            return new RedisAuctionSnapshot(auction, await client.GetItemInfoAsync(auction.Item, locale));
26
+        }
27
+    }
28
+
29
+}