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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. {
  42. _blobCache.Dispose();
  43. _cancelTokenSource?.Dispose();
  44. }
  45. _isDisposed = true;
  46. }
  47. }
  48. public void Dispose()
  49. {
  50. Dispose(true);
  51. }
  52. public void SetUrl(string url)
  53. {
  54. _baseUrl = url;
  55. }
  56. public void SetHeader(string key, string value)
  57. {
  58. _headers[key] = value;
  59. }
  60. public void SetCancelToken(CancellationToken cancelToken)
  61. {
  62. _parentToken = cancelToken;
  63. _cancelToken = CancellationTokenSource.CreateLinkedTokenSource(_parentToken, _cancelTokenSource.Token).Token;
  64. }
  65. public async Task<RestResponse> SendAsync(string method, string endpoint, CancellationToken cancelToken, bool headerOnly, string reason = null)
  66. {
  67. if (method != "GET")
  68. throw new InvalidOperationException("This RestClient only supports GET requests.");
  69. string uri = Path.Combine(_baseUrl, endpoint);
  70. var bytes = await _blobCache.DownloadUrl(uri, _headers);
  71. return new RestResponse(HttpStatusCode.OK, _headers, new MemoryStream(bytes));
  72. }
  73. public Task<RestResponse> SendAsync(string method, string endpoint, string json, CancellationToken cancelToken, bool headerOnly, string reason = null)
  74. {
  75. throw new InvalidOperationException("This RestClient does not support payloads.");
  76. }
  77. public Task<RestResponse> SendAsync(string method, string endpoint, IReadOnlyDictionary<string, object> multipartParams, CancellationToken cancelToken, bool headerOnly, string reason = null)
  78. {
  79. throw new InvalidOperationException("This RestClient does not support multipart requests.");
  80. }
  81. public async Task ClearAsync()
  82. {
  83. await _blobCache.InvalidateAll();
  84. }
  85. public async Task LoadInfoAsync(ulong guildId)
  86. {
  87. if (Info != null)
  88. return;
  89. bool needsReset = false;
  90. try
  91. {
  92. Info = await _blobCache.GetObject<CacheInfo>("info");
  93. if (Info.GuildId != guildId)
  94. needsReset = true;
  95. }
  96. catch (KeyNotFoundException)
  97. {
  98. needsReset = true;
  99. }
  100. if (needsReset)
  101. {
  102. Info = new CacheInfo() { GuildId = guildId, Version = 0 };
  103. await SaveInfoAsync().ConfigureAwait(false);
  104. }
  105. }
  106. public async Task SaveInfoAsync()
  107. {
  108. await ClearAsync().ConfigureAwait(false); //Version changed, invalidate cache
  109. await _blobCache.InsertObject<CacheInfo>("info", Info);
  110. }
  111. }
  112. }