Browse Source

add basic redis

Karathan 6 years ago
parent
commit
9120e83bbe

+ 1
- 0
AuctionTracker/AuctionTracker.csproj View File

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

+ 25
- 0
AuctionTracker/Program.cs View File

13
 using Microsoft.Extensions.Configuration;
13
 using Microsoft.Extensions.Configuration;
14
 using Microsoft.Extensions.Configuration.FileExtensions;
14
 using Microsoft.Extensions.Configuration.FileExtensions;
15
 using Microsoft.Extensions.Configuration.Json;
15
 using Microsoft.Extensions.Configuration.Json;
16
+using ServiceStack.Redis;
17
+using ServiceStack.Redis.Generic;
16
 
18
 
17
 namespace AuctionTracker
19
 namespace AuctionTracker
18
 {
20
 {
25
         private static IConfiguration _configuration;
27
         private static IConfiguration _configuration;
26
         private static ILog _logger;
28
         private static ILog _logger;
27
 
29
 
30
+        private static RedisManagerPool _redisPool;
28
         private static string _character;
31
         private static string _character;
29
         private static string _locale;
32
         private static string _locale;
30
         private static string _realm;
33
         private static string _realm;
80
                 }
83
                 }
81
             }
84
             }
82
             lastAuctions = auctions;
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
         static async Task Main(string[] args)
107
         static async Task Main(string[] args)
85
         {
108
         {
99
             _logger.Info("Creating Clients");
122
             _logger.Info("Creating Clients");
100
             _client = new BlizzSharpClient($"{_configuration["blizzardApiKey"]}");
123
             _client = new BlizzSharpClient($"{_configuration["blizzardApiKey"]}");
101
             _pushClient = new PushClient($"{_configuration["pushoverSecretKey"]}", $"{_configuration["pushoverUserKey"]}");
124
             _pushClient = new PushClient($"{_configuration["pushoverSecretKey"]}", $"{_configuration["pushoverUserKey"]}");
125
+            _logger.Info("Initializing redis pool");
126
+            _redisPool = new RedisManagerPool("localhost:6379");
102
             _logger.Info("AuctionTracker started, entering loop");
127
             _logger.Info("AuctionTracker started, entering loop");
103
 
128
 
104
             while(true)
129
             while(true)

+ 29
- 0
AuctionTracker/RedisAuctionSnapshot.cs View File

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
+}