ntminer 5 лет назад
Родитель
Сommit
bcaaff71af

+ 13 - 0
src/WebApiServer/Core/Redis/ISpeedDataRedis.cs

@@ -0,0 +1,13 @@
+using NTMiner.Report;
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+
+namespace NTMiner.Core.Redis {
+    public interface ISpeedDataRedis {
+        Task<List<SpeedData>> GetAllAsync();
+        Task<SpeedData> GetByClientIdAsync(Guid clientId);
+        Task SetAsync(SpeedData data);
+        Task DeleteByClientIdAsync(Guid clientId);
+    }
+}

+ 63 - 0
src/WebApiServer/Core/Redis/Impl/SpeedDataRedis.cs

@@ -0,0 +1,63 @@
+using NTMiner.Report;
+using StackExchange.Redis;
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+
+namespace NTMiner.Core.Redis.Impl {
+    public class SpeedDataRedis : ISpeedDataRedis {
+        protected const string _redisKeySpeedDataByClientId = "speedDatas.SpeedDataByClientId";// 根据ClientId索引SpeedData对象的json
+
+        protected readonly ConnectionMultiplexer _connection;
+        public SpeedDataRedis(ConnectionMultiplexer connection) {
+            _connection = connection;
+        }
+
+        public Task<List<SpeedData>> GetAllAsync() {
+            var db = _connection.GetDatabase();
+            return db.HashGetAllAsync(_redisKeySpeedDataByClientId).ContinueWith(t => {
+                List<SpeedData> list = new List<SpeedData>();
+                foreach (var item in t.Result) {
+                    if (item.Value.HasValue) {
+                        SpeedData data = VirtualRoot.JsonSerializer.Deserialize<SpeedData>(item.Value);
+                        if (data != null) {
+                            list.Add(data);
+                        }
+                    }
+                }
+                return list;
+            });
+        }
+
+        public Task<SpeedData> GetByClientIdAsync(Guid clientId) {
+            if (clientId == Guid.Empty) {
+                return Task.FromResult<SpeedData>(null);
+            }
+            var db = _connection.GetDatabase();
+            return db.HashGetAsync(_redisKeySpeedDataByClientId, clientId.ToString()).ContinueWith(t => {
+                if (t.Result.HasValue) {
+                    return VirtualRoot.JsonSerializer.Deserialize<SpeedData>(t.Result);
+                }
+                else {
+                    return null;
+                }
+            });
+        }
+
+        public Task SetAsync(SpeedData data) {
+            if (data == null || data.ClientId == Guid.Empty) {
+                return TaskEx.CompletedTask;
+            }
+            var db = _connection.GetDatabase();
+            return db.HashSetAsync(_redisKeySpeedDataByClientId, data.ClientId.ToString(), VirtualRoot.JsonSerializer.Serialize(data));
+        }
+
+        public Task DeleteByClientIdAsync(Guid clientId) {
+            if (clientId == Guid.Empty) {
+                return TaskEx.CompletedTask;
+            }
+            var db = _connection.GetDatabase();
+            return db.HashDeleteAsync(_redisKeySpeedDataByClientId, clientId.ToString());
+        }
+    }
+}

+ 2 - 0
src/WebApiServer/WebApiServer.csproj

@@ -151,7 +151,9 @@
     <Compile Include="Core\Redis\IMinerRedis.cs" />
     <Compile Include="Core\Redis\Impl\CaptchaRedis.cs" />
     <Compile Include="Core\Redis\Impl\MinerRedis.cs" />
+    <Compile Include="Core\Redis\Impl\SpeedDataRedis.cs" />
     <Compile Include="Core\Redis\Impl\UserRedis.cs" />
+    <Compile Include="Core\Redis\ISpeedDataRedis.cs" />
     <Compile Include="Core\Redis\IUserRedis.cs" />
     <Compile Include="UserDataExtensions.cs" />
     <Compile Include="WebApiRoot.cs" />