コード例 #1
0
        public async Task <T> GetHttpWithSpool(string key, string url, int howLongToStoreSec,
                                               Func <string, string, T> decoder)
        {
            T data = null;

            try
            {
                SpoolItem <T> item = TrySpool(key, url, howLongToStoreSec);
                if (item.Data != null)
                {
                    return(item.Data);
                }
                string jsonBody = await Client.GetHttpStringAsync(url);



                item.Data = decoder(key, jsonBody);

                return(item.Data);
            }
            catch (Exception ex)
            {
                Log.LogError(ex.StackTrace);
                return(null);
            }
            return(null);
        }
コード例 #2
0
        private SpoolItem <T> TrySpool(string key, string url, int howLongToStoreSec)
        {
            SpoolItem <T> item = DictLazy.Value.AddOrUpdate(key,
                                                            (key) =>
            {
                // Create New Instance for Spooler
                return(new SpoolItem <T>()
                {
                    Key = key,
                    ActualUntil = DateTime.Now.AddSeconds(howLongToStoreSec),
                    Data = null
                });
            },
                                                            (_url, _item) =>
            {
                // Test of Existing Instance for Time Existance
                if (_item.ActualUntil < DateTime.Now)
                {
                    _item.Data = null;
                    Log.LogInformation($"Old tem {url} removed");
                }
                return(_item);
            }
                                                            );

            return(item);
        }
コード例 #3
0
        public T TryGetValue(string key)
        {
            SpoolItem <T> item = null;

            if (Dict.TryGetValue(key, out item))
            {
                if (item.ActualUntil <= DateTime.Now)
                {
                    Dict.TryRemove(key, out item);
                    item = null;
                }
            }

            return((item != null) ? item.Data : null);
        }
コード例 #4
0
        public T TryAddValue(string key, T p)
        {
            SpoolItem <T> item = Dict.AddOrUpdate(key, item => ToSpoolItem(p), (item, old) => ToSpoolItem(p));

            return(item.Data);
        }