You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

CachedRestClient.cs 4.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using Akavache;
  2. using Akavache.Sqlite3;
  3. using Discord.Net.Rest;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Net;
  8. using System.Reactive.Concurrency;
  9. using System.Reactive.Linq;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using Splat;
  13. namespace Discord.Net
  14. {
  15. internal class CachedRestClient : IRestClient
  16. {
  17. private readonly Dictionary<string, string> _headers;
  18. private IBlobCache _blobCache;
  19. private string _baseUrl;
  20. private CancellationTokenSource _cancelTokenSource;
  21. private CancellationToken _cancelToken, _parentToken;
  22. private bool _isDisposed;
  23. public CacheInfo Info { get; private set; }
  24. public CachedRestClient()
  25. {
  26. _headers = new Dictionary<string, string>();
  27. _cancelTokenSource = new CancellationTokenSource();
  28. _cancelToken = CancellationToken.None;
  29. _parentToken = CancellationToken.None;
  30. Locator.CurrentMutable.Register(() => Scheduler.Default, typeof(IScheduler), "Taskpool");
  31. Locator.CurrentMutable.Register(() => new FilesystemProvider(), typeof(IFilesystemProvider), null);
  32. Locator.CurrentMutable.Register(() => new HttpMixin(), typeof(IAkavacheHttpMixin), null);
  33. //new Akavache.Sqlite3.Registrations().Register(Locator.CurrentMutable);
  34. _blobCache = new SQLitePersistentBlobCache("cache.db");
  35. }
  36. private void Dispose(bool disposing)
  37. {
  38. if (!_isDisposed)
  39. {
  40. if (disposing)
  41. _blobCache.Dispose();
  42. _isDisposed = true;
  43. }
  44. }
  45. public void Dispose()
  46. {
  47. Dispose(true);
  48. }
  49. public void SetUrl(string url)
  50. {
  51. _baseUrl = url;
  52. }
  53. public void SetHeader(string key, string value)
  54. {
  55. _headers[key] = value;
  56. }
  57. public void SetCancelToken(CancellationToken cancelToken)
  58. {
  59. _parentToken = cancelToken;
  60. _cancelToken = CancellationTokenSource.CreateLinkedTokenSource(_parentToken, _cancelTokenSource.Token).Token;
  61. }
  62. public async Task<RestResponse> SendAsync(string method, string endpoint, CancellationToken cancelToken, bool headerOnly, string reason = null)
  63. {
  64. if (method != "GET")
  65. throw new InvalidOperationException("This RestClient only supports GET requests.");
  66. string uri = Path.Combine(_baseUrl, endpoint);
  67. var bytes = await _blobCache.DownloadUrl(uri, _headers);
  68. return new RestResponse(HttpStatusCode.OK, _headers, new MemoryStream(bytes));
  69. }
  70. public Task<RestResponse> SendAsync(string method, string endpoint, string json, CancellationToken cancelToken, bool headerOnly, string reason = null)
  71. {
  72. throw new InvalidOperationException("This RestClient does not support payloads.");
  73. }
  74. public Task<RestResponse> SendAsync(string method, string endpoint, IReadOnlyDictionary<string, object> multipartParams, CancellationToken cancelToken, bool headerOnly, string reason = null)
  75. {
  76. throw new InvalidOperationException("This RestClient does not support multipart requests.");
  77. }
  78. public async Task ClearAsync()
  79. {
  80. await _blobCache.InvalidateAll();
  81. }
  82. public async Task LoadInfoAsync(ulong guildId)
  83. {
  84. if (Info != null)
  85. return;
  86. bool needsReset = false;
  87. try
  88. {
  89. Info = await _blobCache.GetObject<CacheInfo>("info");
  90. if (Info.GuildId != guildId)
  91. needsReset = true;
  92. }
  93. catch (KeyNotFoundException)
  94. {
  95. needsReset = true;
  96. }
  97. if (needsReset)
  98. {
  99. Info = new CacheInfo() { GuildId = guildId, Version = 0 };
  100. await SaveInfoAsync().ConfigureAwait(false);
  101. }
  102. }
  103. public async Task SaveInfoAsync()
  104. {
  105. await ClearAsync().ConfigureAwait(false); //Version changed, invalidate cache
  106. await _blobCache.InsertObject<CacheInfo>("info", Info);
  107. }
  108. }
  109. }