Modules/Framework/Gardener.Cache/CacheExtensions.cs

(开头部分) 2KB

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

// -----------------------------------------------------------------------------
// 园丁,是个很简单的管理系统
//  gitee:https://gitee.com/hgflydream/Gardener 
//  issues:https://gitee.com/hgflydream/Gardener/issues 
// -----------------------------------------------------------------------------

using Furion;
using Gardener.Cache;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;
using System;

namespace Microsoft.Extensions.DependencyInjection
{
    /// <summary>
    /// 安全服务
    /// </summary>
    public static class CacheExtensions
    {
        /// <summary>
        /// 添加服务
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddCache(this IServiceCollection services)
        {
            services.AddConfigurableOptions<CacheOptions>();
            using var serviceProvider = services.BuildServiceProvider();
            if(serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }
            var cacheOptionProvider = serviceProvider.GetService<IOptions<CacheOptions>>();
            if (cacheOptionProvider == null)
            {
                throw new ArgumentNullException(nameof(CacheOptions));
            }
            var cacheOptions = cacheOptionProvider.Value;
            if (cacheOptions.Type.Equals("Memory"))
            {
                services.AddDistributedMemoryCache();
            }else if (cacheOptions.Type.Equals("SqlServer"))
            {
                if (cacheOptions.SqlServer == null) 
                {
                    throw new OptionsValidationException("Cache:SqlServer",typeof(SqlServerCacheOptions),new[] { "请配置SqlServer缓存配置" });
                }
                string? connectionString = App.Configuration[cacheOptions.SqlServer.ConnectionString];
                services.AddDistributedSqlServerCache(options => { 
                    options.ConnectionString = connectionString;
                    options.SchemaName = cacheOptions.SqlServer.SchemaName; 
                    options.TableName = cacheOptions.SqlServer.TableName; }
                );
            }else if (cacheOptions.Type.Equals("Redis"))
            {
                if (cacheOptions.Redis == null) 
                {
                    throw new OptionsValidationException("Cache:Redis", typeof(SqlServerCacheOptions),new[] { "请配置Redis缓存配置" });
                }
                services.AddStackExchangeRedisCache(options => {    
后面还有 9 行代码,解锁后查看完整代码

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

AI 解读

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

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