public static IServiceCollection AddCacheTower(this IServiceCollection services)
    {
        var cacheTowerPath   = "Storage/Cache-Tower/".EnsureDirectory();
        var serviceProvider  = services.BuildServiceProvider();
        var cacheConfig      = serviceProvider.GetRequiredService <IOptions <CacheConfig> >().Value;
        var lastError        = ErrorUtil.ParseErrorTextAsync().Result;
        var shouldInvalidate = lastError.FullText.Contains("CacheTower");

        if (cacheConfig.InvalidateOnStart || shouldInvalidate)
        {
            cacheTowerPath.DeleteDirectory().EnsureDirectory();
        }

        if (shouldInvalidate)
        {
            "No Error".SaveErrorToText().WaitAndUnwrapException();
        }

        services.AddCacheStack(
            builder => {
            var jsonSerializerSettings = new JsonSerializerSettings()
            {
                Formatting = Formatting.Indented
            };

            if (cacheConfig.EnableInMemoryCache)
            {
                builder.AddMemoryCacheLayer();
            }

            if (cacheConfig.EnableJsonCache)
            {
                builder.AddFileCacheLayer(
                    new FileCacheLayerOptions(
                        directoryPath: cacheTowerPath,
                        serializer: new NewtonsoftJsonCacheSerializer(jsonSerializerSettings),
                        manifestSaveInterval: TimeSpan.FromSeconds(5)
                        )
                    );
            }

            if (cacheConfig.EnableRedisCache)
            {
                builder.AddRedisCacheLayer(
                    connection: ConnectionMultiplexer.Connect(cacheConfig.RedisConnection),
                    options: new RedisCacheLayerOptions(new NewtonsoftJsonCacheSerializer(jsonSerializerSettings))
                    );
            }
        }
            );

        return(services);
    }
Exemplo n.º 2
0
    public static IApplicationBuilder ResetHangfireStorageIfRequired(this IApplicationBuilder app)
    {
        var serviceProvider = app.GetServiceProvider();

        var storageService = serviceProvider.GetRequiredService <IStorageService>();
        var parseError     = ErrorUtil.ParseErrorTextAsync().Result;
        var lastFtlError   = parseError.FullText;

        if (lastFtlError.Contains("hangfire", StringComparison.CurrentCultureIgnoreCase))
        {
            if (lastFtlError.Contains("Storage", StringComparison.CurrentCultureIgnoreCase))
            {
                Log.Warning("Last error about Hangfire, seem need to Reset MySql Storage");
                storageService.ResetHangfireMySqlStorage().WaitAndUnwrapException();

                "".SaveErrorToText().WaitAndUnwrapException();
            }
        }

        return(app);
    }