/// <summary>
        ///   Looks for a query parameter value in a <see cref="Uri"/>.
        /// </summary>
        /// <param name="uri">
        ///   The <seealso cref="Uri"/> containing the query.
        /// </param>
        /// <param name="key">
        ///   A query property name to look for.
        /// </param>
        /// <returns>
        ///   A <seealso cref="BoolValue{T}"/> indicating whether the specified value
        ///   was present in the query. On success this object will also carry the
        ///   requested query property value (<seealso cref="BoolValue{T}.Value"/>).
        /// </returns>
        public static BoolValue <string> TryGetQueryValue(this Uri uri, string key)
        {
            lock (s_cachedQueries)
            {
                if (!s_cachedQueries.TryGetValue(uri, out var dict))
                {
                    dict = new Dictionary <string, string>();
                    var kvps = uri.Query.Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
                    for (var i = 0; i < kvps.Length; i++)
                    {
                        var kvp = kvps[i].Split('=');
                        dict[kvp[0].TrimStart('?')] = kvp[1];
                    }

                    s_cachedQueries.Add(uri, dict);
                }

                return(dict.TryGetValue(key, out var value)
                    ? BoolValue <string> .Success(value)
                    : BoolValue <string> .Fail(""));
            }
        }
示例#2
0
        public override async Task <BoolValue <T> > TryGetAsync(string key = null)
        {
            var isLoaded = await base.TryGetAsync(key);

            if (isLoaded)
            {
                return(isLoaded);
            }

            var path = Path.Combine(_directory.FullName, $"{key}{_fileSuffix}");

            if (!File.Exists(path))
            {
                return(BoolValue <T> .Fail());
            }

            var json       = File.ReadAllText(path);
            var cachedItem = json.FromJson <T>();
            await base.AddAsync(cachedItem.Key, cachedItem.Value, true, cachedItem.Expires);

            return(BoolValue <T> .Success(cachedItem.Value));
        }
        /// <inheritdoc/>
        public virtual Task <BoolValue <T> > TryGetAsync(string key = null)
        {
            key ??= DefaultKey;
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            lock (_syncRoot)
            {
                if (_items.TryGetValue(key, out CachedItem <T> item) && !item.IsExpired)
                {
                    return(Task.FromResult(BoolValue <T> .Success(item.Value)));
                }
            }

            return(Task.FromResult(BoolValue <T> .Fail()));
        }