happy-geeks_geeks-core-library/GeeksCoreLibrary/Modules/Redirect/Services/CachedRedirectService.cs
using System;
using System.Threading.Tasks;
using GeeksCoreLibrary.Core.Enums;
using GeeksCoreLibrary.Core.Interfaces;
using GeeksCoreLibrary.Core.Models;
using GeeksCoreLibrary.Modules.Branches.Interfaces;
using GeeksCoreLibrary.Modules.Redirect.Interfaces;
using GeeksCoreLibrary.Modules.Redirect.Models;
using LazyCache;
using Microsoft.Extensions.Options;

namespace GeeksCoreLibrary.Modules.Redirect.Services
{
    public class CachedRedirectService : IRedirectService
    {
        private readonly IAppCache cache;
        private readonly IRedirectService redirectService;
        private readonly ICacheService cacheService;
        private readonly GclSettings gclSettings;
        private readonly IBranchesService branchesService;

        public CachedRedirectService(IAppCache cache, IRedirectService redirectService, IOptions<GclSettings> gclSettings, ICacheService cacheService, IBranchesService branchesService)
        {
            this.cache = cache;
            this.redirectService = redirectService;
            this.cacheService = cacheService;
            this.gclSettings = gclSettings.Value;
            this.branchesService = branchesService;
        }

        /// <inheritdoc />
        public async Task<RedirectModel> GetRedirectAsync(Uri uri)
        {
            var cacheName = $"Redirect_{uri}_{branchesService.GetDatabaseNameFromCookie()}";
            return await cache.GetOrAddAsync(cacheName,
                 async cacheEntry =>
                 {
                     cacheEntry.AbsoluteExpirationRelativeToNow = gclSettings.DefaultRedirectModuleCacheDuration;
                     return await redirectService.GetRedirectAsync(uri);
                 }, cacheService.CreateMemoryCacheEntryOptions(CacheAreas.Redirects));
        }

        /// <inheritdoc />
        public Task<bool> RedirectModuleIsEnabledAsync()
        {
            return redirectService.RedirectModuleIsEnabledAsync();
        }

        /// <inheritdoc />
        public  Task<string> GetMainDomainForRedirectAsync()
        {
            return redirectService.GetMainDomainForRedirectAsync();
        }

        /// <inheritdoc />
        public Task<bool> ShouldRedirectToUrlWithTrailingSlashAsync()
        {
            return redirectService.ShouldRedirectToUrlWithTrailingSlashAsync();
        }

        /// <inheritdoc />
        public Task<bool> ShouldRedirectToLowerCaseUrlAsync()
        {
            return redirectService.ShouldRedirectToLowerCaseUrlAsync();
        }

        /// <inheritdoc />
        public Task<bool> ShouldRedirectToHttpsAsync()
        {
            return redirectService.ShouldRedirectToHttpsAsync();
        }
    }
}