/
DataService.cs
102 lines (88 loc) · 3.98 KB
/
DataService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using DevWeek.Services.Downloader;
using MongoDB.Driver;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DevWeek.Services
{
/// <summary>
/// Manage Download Data
/// </summary>
public class DataService
{
private readonly StackExchange.Redis.ConnectionMultiplexer redis;
private readonly DistributedLockService distributedLockService;
private readonly MongoClient mongoClient;
private readonly string redisDownloadListKey;
private readonly string mongoDownloadCollectionName;
public DataService(StackExchange.Redis.ConnectionMultiplexer redis, DistributedLockService distributedLockService, MongoClient mongoClient, string redisDownloadListKey, string mongoDownloadCollectionName)
{
this.redis = redis;
this.distributedLockService = distributedLockService;
this.mongoClient = mongoClient;
this.redisDownloadListKey = redisDownloadListKey;
this.mongoDownloadCollectionName = mongoDownloadCollectionName;
}
public async Task Update(string id, Func<UpdateDefinitionBuilder<Download>, UpdateDefinition<Download>> updateFunction)
{
var downloadItemOnDB = this.GetDownloadElementById(id);
if (downloadItemOnDB != null)
{
var downloadCollection = this.GetDownloadCollection();
var update = updateFunction(Builders<Download>.Update);
downloadCollection.FindOneAndUpdate(it => it.Id == id, update);
await this.RebuildCache(downloadCollection);
}
else
{
throw new InvalidOperationException($"Download with id '{id}' can not found! #invalidId");
}
}
public async Task Insert(Download downloadToInsert)
{
var downloadItemOnDB = this.GetDownloadElementByUrl(downloadToInsert.OriginalMediaUrl);
if (downloadItemOnDB == null)
{
var downloadCollection = this.GetDownloadCollection();
await this.GetDownloadCollection().InsertOneAsync(downloadToInsert);
await this.RebuildCache(downloadCollection);
}
else
{
downloadToInsert.Id = downloadItemOnDB.Id;
}
}
public async Task RebuildCache(IMongoCollection<Download> downloadCollection = null)
{
downloadCollection = downloadCollection ?? this.GetDownloadCollection();
using (this.distributedLockService.Acquire(0, $"{this.redisDownloadListKey}-key", TimeSpan.FromSeconds(15)))
{
var redisDB = this.redis.GetDatabase(0);
redisDB.KeyDelete(this.redisDownloadListKey);
var list = (await downloadCollection.Find(Builders<Download>.Filter.Empty).ToListAsync()).Select(it =>
{
return (RedisValue)Newtonsoft.Json.JsonConvert.SerializeObject(it);
}).ToArray();
redisDB.ListLeftPush(this.redisDownloadListKey, list);
};
}
private IMongoCollection<Download> GetDownloadCollection()
{
var downloadCollection = this.mongoClient.GetDatabase("admin").GetCollection<Download>(this.mongoDownloadCollectionName);
return downloadCollection;
}
private Download GetDownloadElementByUrl(string url)
{
var downloadItemOnDB = this.GetDownloadCollection().AsQueryable().Where(it => it.OriginalMediaUrl.ToLowerInvariant() == url.ToLowerInvariant()).SingleOrDefault();
return downloadItemOnDB;
}
private Download GetDownloadElementById(string id)
{
var downloadItemOnDB = this.GetDownloadCollection().AsQueryable().Where(it => it.Id == id).SingleOrDefault();
return downloadItemOnDB;
}
}
}