< Summary

Class:DataCache[T]
Assembly:DataCache
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/DataCache/DataCache.cs
Covered lines:20
Uncovered lines:11
Coverable lines:31
Total lines:77
Line coverage:64.5% (20 of 31)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
DataCache()0%110100%
Add(...)0%2.032080%
TryGet(...)0%2.312057.14%
Forget(...)0%6200%
Dispose()0%220100%
RemoveCache()0%8.744033.33%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/DataCache/DataCache.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using UnityEngine;
 4
 5public class DataCache<T> : IDataCache<T>
 6{
 7    private class CacheType
 8    {
 9        public T value;
 10        public float lastUpdate;
 11        public float maxAge;
 12        public Coroutine expireCacheRoutine;
 13    }
 14
 134215    private readonly Dictionary<string, CacheType> cache = new Dictionary<string, CacheType>();
 16
 17    public void Add(string key, T value, float maxAge)
 18    {
 419        if (cache.TryGetValue(key, out CacheType storedCache))
 20        {
 021            CoroutineStarter.Stop(storedCache.expireCacheRoutine);
 022        }
 23        else
 24        {
 425            storedCache = new CacheType();
 426            cache[key] = storedCache;
 27        }
 28
 429        storedCache.value = value;
 430        storedCache.maxAge = maxAge;
 431        storedCache.lastUpdate = Time.unscaledTime;
 432        storedCache.expireCacheRoutine = CoroutineStarter.Start(RemoveCache(key, storedCache.maxAge));
 433    }
 34
 35    public bool TryGet(string key, out T value, out float lastUpdate)
 36    {
 437        value = default(T);
 438        lastUpdate = 0;
 39
 440        if (cache.TryGetValue(key, out CacheType storedCache))
 41        {
 042            value = storedCache.value;
 043            lastUpdate = storedCache.lastUpdate;
 044            return true;
 45        }
 46
 447        return false;
 48    }
 49
 50    public void Forget(string key)
 51    {
 052        if (cache.TryGetValue(key, out CacheType storedCache))
 53        {
 054            CoroutineStarter.Stop(storedCache.expireCacheRoutine);
 055            cache.Remove(key);
 56        }
 057    }
 58
 59    public void Dispose()
 60    {
 138461        using (var iterator = cache.GetEnumerator())
 62        {
 138563            while (iterator.MoveNext())
 64            {
 165                CoroutineStarter.Stop(iterator.Current.Value.expireCacheRoutine);
 66            }
 138467        }
 68
 138469        cache.Clear();
 138470    }
 71
 72    private IEnumerator RemoveCache(string key, float delay)
 73    {
 474        yield return WaitForSecondsCache.Get(delay);
 075        cache?.Remove(key);
 076    }
 77}