1 module viva.collections.cache; 2 3 import std.typecons : Nullable; 4 5 import viva.collections.table; 6 7 /++ 8 + 9 +/ 10 struct Cache(T) 11 { 12 private HashTable cache = HashTable(5); 13 14 /++ 15 + 16 +/ 17 //public T get(string key) 18 public Nullable!EntryValue get(string key) 19 { 20 // TODO: Find a way to make it spit out the direct value of type `T` 21 22 //return cache.get(key); 23 24 //Nullable!EntryValue result = cache.get(key); 25 //if (result.isNull) throw new Exception(""); 26 //return cast(int) result.get.integer; 27 28 return cache.get(key); 29 } 30 31 /++ 32 + 33 +/ 34 //public T[] getAll() 35 public EntryValue[] getAll() 36 { 37 return cache.getAll(); 38 } 39 40 /++ 41 + 42 +/ 43 public void add(string key, T value) 44 { 45 cache.set(key, value); 46 } 47 48 /++ 49 + 50 +/ 51 public void update(string key, T value) 52 { 53 cache.set(key, value); 54 } 55 56 /++ 57 + 58 +/ 59 public void remove(string key) 60 { 61 cache.remove(key); 62 } 63 }