Exemplo n.º 1
0
        public static T GetKeyT <TId>(TId id, int decaysec, Func <TId, T> factory)
        {
            // Get object from cache and Load/Create it if needed.
            // NOTE: await Task<T> cant be done inside lock !???
            // TId = int or string.
            // like IMemoryCache GetOrCreate

            string cacheKey = string.Concat(typeof(T).Name, CacheData.kSep, id);

            lock (_WeakRefs)
            {
                // Find in the hard ref cache first.
                T obj = (T)CacheData.Get(cacheKey);
                if (obj != null)
                {
                    return(obj);  // got it.
                }

                // Fallback to the weak ref cache next. e.g. someone has a ref that existed longer than the cache? get it.
                WeakReference wr;
                if (_WeakRefs.TryGetValue(cacheKey, out wr))
                {
                    if (wr.IsAlive)
                    {
                        // someone still has a ref to this . so continue to use it. we should reload this from the db though.
                        obj = (T)wr.Target;
                    }
                }

                // lastly load it if we cant find it otherwise.
                if (factory != null)
                {
                    // NOTE: await Task<T> cant be done inside lock !
                    T objLoad = factory.Invoke(id);
                    ValidState.ThrowIfNull(objLoad, nameof(objLoad));

                    if (obj == null)
                    {
                        obj = objLoad;  // Fresh load.
                        if (wr != null)
                        {
                            _WeakRefs.Remove(cacheKey);
                        }
                        _WeakRefs.Add(cacheKey, new WeakReference(obj));
                    }
                    else
                    {
                        PropertyUtil.InjectProperties <T>(obj, objLoad); // refresh props in old weak ref.
                    }
                }

                if (obj != null)
                {
                    // store it in cache. refresh decaySec.
                    CacheData.Set(cacheKey, obj, decaysec);
                }

                return(obj);
            }
        }
Exemplo n.º 2
0
        public static T Get(string id)
        {
            // Find the object in the cache if possible.
            if (id == null)       // shortcut.
            {
                return(null);
            }
            ValidState.ThrowIfWhiteSpace(id, nameof(id));       // must have id. else use GetSingleton
            string cacheKey = MakeKey(id);
            object o        = CacheData.Get(cacheKey);

            return((T)o);
        }
Exemplo n.º 3
0
 public static T GetSingleton()
 {
     // Get singleton. Instance()
     return((T)CacheData.Get(typeof(T).Name));
 }