/// <inheritdoc />
        public override async Task <Result <IEmoji> > GetGuildEmojiAsync
        (
            Snowflake guildID,
            Snowflake emojiID,
            CancellationToken ct = default
        )
        {
            var key = KeyHelpers.CreateEmojiCacheKey(guildID, emojiID);

            if (_cacheService.TryGetValue <IEmoji>(key, out var cachedInstance))
            {
                return(Result <IEmoji> .FromSuccess(cachedInstance));
            }

            var getResult = await base.GetGuildEmojiAsync(guildID, emojiID, ct);

            if (!getResult.IsSuccess)
            {
                return(getResult);
            }

            var emoji = getResult.Entity;

            _cacheService.Cache(key, emoji);

            return(getResult);
        }
        /// <inheritdoc />
        public override async Task <IRetrieveRestEntityResult <IUser> > GetUserAsync
        (
            Snowflake userID,
            CancellationToken ct = default
        )
        {
            var key = KeyHelpers.CreateUserCacheKey(userID);

            if (_cacheService.TryGetValue <IUser>(key, out var cachedInstance))
            {
                return(RetrieveRestEntityResult <IUser> .FromSuccess(cachedInstance));
            }

            var getUser = await base.GetUserAsync(userID, ct);

            if (!getUser.IsSuccess)
            {
                return(getUser);
            }

            var user = getUser.Entity;

            _cacheService.Cache(key, user);

            return(getUser);
        }
Esempio n. 3
0
        /// <inheritdoc />
        public override async Task <Result <IReadOnlyList <IVoiceRegion> > > ListVoiceRegionsAsync
        (
            CancellationToken ct = default
        )
        {
            var key = KeyHelpers.CreateVoiceRegionsCacheKey();

            if (_cacheService.TryGetValue <IReadOnlyList <IVoiceRegion> >(key, out var cachedInstance))
            {
                return(Result <IReadOnlyList <IVoiceRegion> > .FromSuccess(cachedInstance));
            }

            var listRegions = await base.ListVoiceRegionsAsync(ct);

            if (!listRegions.IsSuccess)
            {
                return(listRegions);
            }

            var regions = listRegions.Entity;

            _cacheService.Cache(key, regions);

            foreach (var voiceRegion in regions)
            {
                var regionKey = KeyHelpers.CreateVoiceRegionCacheKey(voiceRegion.ID);
                _cacheService.Cache(regionKey, voiceRegion);
            }

            return(listRegions);
        }
Esempio n. 4
0
        public void Should_provides_the_cached_database()
        {
            var db = new CachedDatabase
            {
                Document = new XDocument()
            };

            _service.Cache(db);

            Assert.Same(db, _service.Database);
        }
Esempio n. 5
0
        /// <inheritdoc />
        public override async Task <IRetrieveRestEntityResult <IChannel> > GetChannelAsync
        (
            Snowflake channelID,
            CancellationToken ct = default
        )
        {
            var key = KeyHelpers.CreateChannelCacheKey(channelID);

            if (_cacheService.TryGetValue <IChannel>(key, out var cachedInstance))
            {
                return(RetrieveRestEntityResult <IChannel> .FromSuccess(cachedInstance));
            }

            var getChannel = await base.GetChannelAsync(channelID, ct);

            if (!getChannel.IsSuccess)
            {
                return(getChannel);
            }

            var channel = getChannel.Entity;

            _cacheService.Cache(key, channel);

            return(getChannel);
        }
        /// <inheritdoc/>
        public Task <Result> RespondAsync(IChannelCreate gatewayEvent, CancellationToken ct = default)
        {
            var key = KeyHelpers.CreateChannelCacheKey(gatewayEvent.ID);

            _cacheService.Cache <IChannel>(key, gatewayEvent);

            return(Task.FromResult(Result.FromSuccess()));
        }
Esempio n. 7
0
        /// <inheritdoc />
        public override async Task <Result <IApplication> > GetCurrentBotApplicationInformationAsync
        (
            CancellationToken ct = default
        )
        {
            var key = KeyHelpers.CreateCurrentApplicationCacheKey();

            if (_cacheService.TryGetValue <IApplication>(key, out var cachedInstance))
            {
                return(Result <IApplication> .FromSuccess(cachedInstance));
            }

            var getCurrent = await base.GetCurrentBotApplicationInformationAsync(ct);

            if (!getCurrent.IsSuccess)
            {
                return(getCurrent);
            }

            var application = getCurrent.Entity;

            _cacheService.Cache(key, application);

            return(getCurrent);
        }
Esempio n. 8
0
        /// <inheritdoc />
        public override async Task <Result <IInvite> > GetInviteAsync
        (
            string inviteCode,
            Optional <bool> withCounts     = default,
            Optional <bool> withExpiration = default,
            CancellationToken ct           = default
        )
        {
            var key = KeyHelpers.CreateInviteCacheKey(inviteCode);

            if (_cacheService.TryGetValue <IInvite>(key, out var cachedInstance))
            {
                return(Result <IInvite> .FromSuccess(cachedInstance));
            }

            var getInvite = await base.GetInviteAsync(inviteCode, withCounts, withExpiration, ct);

            if (!getInvite.IsSuccess)
            {
                return(getInvite);
            }

            var invite = getInvite.Entity;

            _cacheService.Cache(key, invite);

            return(getInvite);
        }
Esempio n. 9
0
        public CacheServiceTests()
        {
            _service = new CacheService();

            _entry = new XElement("Entry",
                                  new XElement("UUID", "e12"));
            _group = new XElement("Group",
                                  new XElement("UUID", "g02"),
                                  new XElement("Entry",
                                               new XElement("UUID", "e21")),
                                  new XElement("Entry",
                                               new XElement("UUID", "e22")));

            _doc = new XDocument(
                new XElement("KeePassFile",
                             new XElement("Root",
                                          new XElement("Group",
                                                       new XElement("UUID", "g01"),
                                                       new XElement("Entry",
                                                                    new XElement("UUID", "e11")),
                                                       _entry),
                                          _group)));

            _service.Cache(new CachedDatabase
            {
                Document = _doc,
            });
        }
Esempio n. 10
0
        /// <inheritdoc />
        public override async Task <Result <ITemplate> > GetTemplateAsync
        (
            string templateCode,
            CancellationToken ct = default
        )
        {
            var key = KeyHelpers.CreateTemplateCacheKey(templateCode);

            if (_cacheService.TryGetValue <ITemplate>(key, out var cachedInstance))
            {
                return(Result <ITemplate> .FromSuccess(cachedInstance));
            }

            var getTemplate = await base.GetTemplateAsync(templateCode, ct);

            if (!getTemplate.IsSuccess)
            {
                return(getTemplate);
            }

            var template = getTemplate.Entity;

            _cacheService.Cache(key, template);

            return(getTemplate);
        }
        /// <inheritdoc />
        public override async Task <Result <IWebhook> > CreateWebhookAsync
        (
            Snowflake channelID,
            string name,
            Optional <Stream?> avatar,
            CancellationToken ct = default
        )
        {
            var createWebhook = await base.CreateWebhookAsync(channelID, name, avatar, ct);

            if (!createWebhook.IsSuccess)
            {
                return(createWebhook);
            }

            var webhook = createWebhook.Entity;
            var key     = KeyHelpers.CreateWebhookCacheKey(webhook.ID);

            _cacheService.Cache(key, webhook);

            return(createWebhook);
        }
Esempio n. 12
0
        public void ItShouldMatchARegisteredLocalizerNullCulture()
        {
            var localizer = Mock.Of <IStringLocalizer>();

            var service = new CacheService();

            service.Cache(Assembly, BaseName, null, localizer);

            var cacheEntry = service.Match(Assembly, BaseName, null);

            Assert.NotNull(cacheEntry);
            Assert.Same(localizer, cacheEntry);
        }
Esempio n. 13
0
        public void ItShouldValidateTheArgumentsOnCache(Type type, string baseName, bool withLocalizer)
        {
            var service   = new CacheService();
            var localizer = withLocalizer ? Mock.Of <IStringLocalizer>() : null;

            Assert.Throws <ArgumentNullException>(() =>
            {
                service.Cache(
                    type?.Assembly,
                    baseName,
                    CultureInfo,
                    localizer);
            });
        }
Esempio n. 14
0
        public void ItShouldResetARegisteredLocalizer()
        {
            var localizer = Mock.Of <IStringLocalizer>();

            var service = new CacheService();

            service.Cache(Assembly, BaseName, CultureInfo, localizer);

            var cacheEntry = service.Match(Assembly, BaseName, CultureInfo);

            Assert.NotNull(cacheEntry);
            Assert.Same(localizer, cacheEntry);

            Assert.True(service.Reset(Assembly, BaseName, CultureInfo));

            cacheEntry = service.Match(Assembly, BaseName, CultureInfo);

            Assert.Null(cacheEntry);
        }
Esempio n. 15
0
        /// <inheritdoc />
        public override async Task <Result <IGuild> > CreateGuildAsync
        (
            string name,
            Optional <string> region = default,
            Optional <Stream> icon   = default,
            Optional <VerificationLevel> verificationLevel = default,
            Optional <MessageNotificationLevel> defaultMessageNotifications = default,
            Optional <ExplicitContentFilterLevel> explicitContentFilter     = default,
            Optional <IReadOnlyList <IRole> > roles = default,
            Optional <IReadOnlyList <IPartialChannel> > channels = default,
            Optional <Snowflake> afkChannelID    = default,
            Optional <TimeSpan> afkTimeout       = default,
            Optional <Snowflake> systemChannelID = default,
            CancellationToken ct = default
        )
        {
            var createResult = await base.CreateGuildAsync
                               (
                name,
                region,
                icon,
                verificationLevel,
                defaultMessageNotifications,
                explicitContentFilter,
                roles,
                channels,
                afkChannelID,
                afkTimeout,
                systemChannelID,
                ct
                               );

            if (!createResult.IsSuccess)
            {
                return(createResult);
            }

            var guild = createResult.Entity;
            var key   = KeyHelpers.CreateGuildCacheKey(guild.ID);

            _cacheService.Cache(key, guild);

            return(createResult);
        }