microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Common/Storage/LocalStorage.cs
118lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | namespace Microsoft.Teams.Common.Storage; |
| 5 | |
| 6 | /// <summary> |
| 7 | /// a local in-memory `IStorage` implementation |
| 8 | /// </summary> |
| 9 | /// <typeparam name="TValue">the value type</typeparam> |
| 10 | public class LocalStorage<TValue> : IStorage<string, TValue> |
| 11 | { |
| 12 | protected Dictionary<string, TValue> _store = []; |
| 13 | protected IList<string> _keys = []; |
| 14 | protected int? _max; |
| 15 | |
| 16 | /// <summary> |
| 17 | /// the number of items in the storage |
| 18 | /// </summary> |
| 19 | public int Size => _store.Count; |
| 20 | |
| 21 | /// <summary> |
| 22 | /// get the list of storage keys |
| 23 | /// </summary> |
| 24 | public IList<string> Keys => _store.Keys.ToList(); |
| 25 | |
| 26 | public LocalStorage(int? max = null) |
| 27 | { |
| 28 | _max = max; |
| 29 | } |
| 30 | |
| 31 | public LocalStorage(IDictionary<string, TValue> data, int? max) |
| 32 | { |
| 33 | _store = new Dictionary<string, TValue>(data); |
| 34 | _keys = data.Keys.ToList(); |
| 35 | _max = max; |
| 36 | } |
| 37 | |
| 38 | public bool Exists(string key) => _store.ContainsKey(key); |
| 39 | public Task<bool> ExistsAsync(string key) => Task.FromResult(Exists(key)); |
| 40 | |
| 41 | public TValue? Get(string key) |
| 42 | { |
| 43 | Hit(key); |
| 44 | return _store.TryGetValue(key, out var value) ? value : default; |
| 45 | } |
| 46 | |
| 47 | public T? Get<T>(string key) where T : TValue |
| 48 | { |
| 49 | var value = Get(key); |
| 50 | return (T?)value; |
| 51 | } |
| 52 | |
| 53 | public Task<TValue?> GetAsync(string key) |
| 54 | { |
| 55 | return Task.FromResult(Get(key)); |
| 56 | } |
| 57 | |
| 58 | public async Task<T?> GetAsync<T>(string key) where T : TValue |
| 59 | { |
| 60 | var value = await GetAsync(key); |
| 61 | return (T?)value; |
| 62 | } |
| 63 | |
| 64 | public void Set(string key, TValue value) |
| 65 | { |
| 66 | if (!Hit(key)) _keys.Add(key); |
| 67 | if (_max is not null) |
| 68 | { |
| 69 | if (_keys.Count > _max) |
| 70 | { |
| 71 | var toRemove = _keys.ElementAt(0); |
| 72 | _keys.RemoveAt(0); |
| 73 | _store.Remove(toRemove); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | _store[key] = value; |
| 78 | } |
| 79 | |
| 80 | public Task SetAsync(string key, TValue value) |
| 81 | { |
| 82 | return Task.Run(() => Set(key, value)); |
| 83 | } |
| 84 | |
| 85 | public void Delete(string key) |
| 86 | { |
| 87 | var index = _keys.IndexOf(key); |
| 88 | |
| 89 | if (index == -1) return; |
| 90 | |
| 91 | _keys.RemoveAt(index); |
| 92 | _store.Remove(key); |
| 93 | } |
| 94 | |
| 95 | public Task DeleteAsync(string key) |
| 96 | { |
| 97 | return Task.Run(() => Delete(key)); |
| 98 | } |
| 99 | |
| 100 | protected bool Hit(string key) |
| 101 | { |
| 102 | if (!Exists(key)) return false; |
| 103 | if (Keys.Last() == key) return true; |
| 104 | |
| 105 | var index = _keys.IndexOf(key); |
| 106 | |
| 107 | if (index < 0) return false; |
| 108 | |
| 109 | for (var i = index + 1; i < _keys.Count; i++) |
| 110 | { |
| 111 | var tmp = _keys[i - 1]; |
| 112 | _keys[i - 1] = _keys[i]; |
| 113 | _keys[i] = tmp; |
| 114 | } |
| 115 | |
| 116 | return true; |
| 117 | } |
| 118 | } |