| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Threading; |
| | 5 | | using UnityEngine.Pool; |
| | 6 | |
|
| | 7 | | namespace DCLServices.Lambdas |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Caches paginated results until `Dispose` is called |
| | 11 | | /// assuming the results are immutable while iterating |
| | 12 | | /// </summary> |
| | 13 | | /// <typeparam name="T"></typeparam> |
| | 14 | | public class LambdaResponsePagePointer<T> : IDisposable where T : PaginatedResponse |
| | 15 | | { |
| | 16 | | private readonly int pageSize; |
| | 17 | | internal readonly Dictionary<int, (T page, DateTime retrievalTime)> cachedPages; |
| | 18 | | private readonly ILambdaServiceConsumer<T> serviceConsumer; |
| | 19 | | private readonly TimeSpan cacheExpiration; |
| | 20 | | private readonly CancellationToken cancellationToken; |
| | 21 | | private readonly string constEndPoint; |
| | 22 | |
|
| 2 | 23 | | internal bool isDisposed { get; private set; } |
| | 24 | |
|
| 0 | 25 | | internal IReadOnlyDictionary<int, (T page, DateTime retrievalTime)> CachedPages => cachedPages; |
| | 26 | |
|
| | 27 | | /// <param name="constEndpoint"></param> |
| | 28 | | /// <param name="pageSize"></param> |
| | 29 | | /// <param name="cancellationToken">Pass Cancellation Token so the pointer will be automatically disposed on can |
| | 30 | | /// <param name="consumer"></param> |
| | 31 | | public LambdaResponsePagePointer(string constEndpoint, int pageSize, CancellationToken cancellationToken, |
| 2 | 32 | | ILambdaServiceConsumer<T> consumer) : this(constEndpoint, pageSize, cancellationToken, consumer, TimeSpan.Fr |
| | 33 | | { |
| 2 | 34 | | } |
| | 35 | |
|
| | 36 | | /// <param name="constEndpoint"></param> |
| | 37 | | /// <param name="pageSize"></param> |
| | 38 | | /// <param name="cancellationToken">Pass Cancellation Token so the pointer will be automatically disposed on can |
| | 39 | | /// <param name="consumer"></param> |
| 2 | 40 | | public LambdaResponsePagePointer(string constEndpoint, int pageSize, CancellationToken cancellationToken, |
| | 41 | | ILambdaServiceConsumer<T> consumer, TimeSpan cacheExpiration ) |
| | 42 | | { |
| 2 | 43 | | this.pageSize = pageSize; |
| 2 | 44 | | this.cancellationToken = cancellationToken; |
| 2 | 45 | | this.constEndPoint = constEndpoint; |
| | 46 | |
|
| 2 | 47 | | cachedPages = DictionaryPool<int, (T page, DateTime retrievalTime)>.Get(); |
| 2 | 48 | | serviceConsumer = consumer; |
| 2 | 49 | | this.cacheExpiration = cacheExpiration; |
| | 50 | |
|
| 2 | 51 | | cancellationToken.Register(Dispose, false); |
| 2 | 52 | | } |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Retrieves a page from the endpoint or cache |
| | 56 | | /// </summary> |
| | 57 | | /// <exception cref="ObjectDisposedException"></exception> |
| | 58 | | /// <exception cref="OperationCanceledException"></exception> |
| | 59 | | public async UniTask<(T response, bool success)> GetPageAsync(int pageNum, CancellationToken localCancellationTo |
| | 60 | | { |
| 2 | 61 | | if (isDisposed) |
| 0 | 62 | | throw new ObjectDisposedException(nameof(LambdaResponsePagePointer<T>)); |
| | 63 | |
|
| | 64 | |
|
| 2 | 65 | | if (cachedPages.TryGetValue(pageNum, out var cachedPage)) |
| | 66 | | { |
| 0 | 67 | | if (DateTime.Now < cachedPage.retrievalTime + cacheExpiration) |
| 0 | 68 | | return (cachedPage.page, true); |
| | 69 | |
|
| | 70 | | // cache expired |
| 0 | 71 | | cachedPages.Remove(pageNum); |
| | 72 | | } |
| | 73 | |
|
| 2 | 74 | | var ct = this.cancellationToken; |
| | 75 | |
|
| 2 | 76 | | if (localCancellationToken != CancellationToken.None && !localCancellationToken.Equals(ct)) |
| 0 | 77 | | ct = CancellationTokenSource.CreateLinkedTokenSource(this.cancellationToken, localCancellationToken).Tok |
| | 78 | |
|
| 2 | 79 | | var res = await serviceConsumer.CreateRequest(constEndPoint, pageSize, pageNum, additionalData, ct); |
| | 80 | |
|
| 2 | 81 | | if (res.success) |
| | 82 | | { |
| 2 | 83 | | cachedPages[pageNum] = (res.response, DateTime.Now); |
| | 84 | | } |
| | 85 | |
|
| 2 | 86 | | return res; |
| 2 | 87 | | } |
| | 88 | |
|
| | 89 | | public void Dispose() |
| | 90 | | { |
| 0 | 91 | | if (isDisposed) |
| 0 | 92 | | return; |
| | 93 | |
|
| 0 | 94 | | DictionaryPool<int, (T, DateTime)>.Release(cachedPages); |
| 0 | 95 | | isDisposed = true; |
| 0 | 96 | | } |
| | 97 | | } |
| | 98 | | } |