src/YL.Utils/Cache/DistributedCacheExt.cs

(开头部分) 30KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using Microsoft.Extensions.Caching.Distributed;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using YL.Utils.Check;
using YL.Utils.Extensions;

namespace YL.Utils.Cache
{
    public static class DistributedCacheExt
    {
        /// <summary>
        /// Gets the <see cref="bool"/> value with the specified key from the cache asynchronously or returns
        /// <c>null</c> if the key was not found.
        /// </summary>
        /// <param name="cache">The distributed cache.</param>
        /// <param name="key">The cache item key.</param>
        /// <returns>The <see cref="bool"/> value or <c>null</c> if the key was not found.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="cache"/> or <paramref name="key"/> is <c>null</c>.</exception>
        public static async Task<bool?> GetBooleanAsync(this IDistributedCache cache, string key)
        {
            CheckNull.ArgumentIsNullException(cache, nameof(cache));
            CheckNull.ArgumentIsNullException(cache, nameof(key));
            var bytes = await cache.GetAsync(key).ConfigureAwait(false);
            if (bytes == null)
            {
                return null;
            }
            return bytes.GetBinaryReader().ReadBoolean();
        }

        /// <summary>
        /// Gets the <see cref="char"/> value with the specified key from the cache asynchronously or returns
        /// <c>null</c> if the key was not found.
        /// </summary>
        /// <param name="cache">The distributed cache.</param>
        /// <param name="key">The cache item key.</param>
        /// <returns>The <see cref="char"/> value or <c>null</c> if the key was not found.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="cache"/> or <paramref name="key"/> is <c>null</c>.</exception>
        public static async Task<char?> GetCharAsync(this IDistributedCache cache, string key)
        {
            if (cache == null)
            {
                throw new ArgumentNullException(nameof(cache));
            }

            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            var bytes = await cache.GetAsync(key).ConfigureAwait(false);
            if (bytes == null)
            {
                return null;
            }

            using (var memoryStream = new MemoryStream(bytes))
后面还有 725 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。