Skip to content

Commit

Permalink
chore: add TTL from last access to paginators (#74)
Browse files Browse the repository at this point in the history
* chore: add TTL from last access to paginators

* chore: migrate all TimedCache usages to python commons

* feat: add ttl from last access to a few caches
  • Loading branch information
Skelmis authored Feb 4, 2024
1 parent ba699cc commit 806ba53
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 306 deletions.
8 changes: 8 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ aiodns==3.0.0
aiohttp==3.8.1
aiosignal==1.2.0
alaric==1.2.0
anyio==4.2.0
async-timeout==4.0.2
attrs==21.4.0
Bot-Base==1.7.1
Brotli==1.0.9
causar==0.2.0
cchardet==2.1.7
certifi==2023.11.17
cffi==1.15.0
charset-normalizer==2.0.12
disnake @ git+https://github.com/suggestionsbot/disnake.git@22a572afd139144c0e2de49c7692a73ab74d8a3d
disnake-ext-components @ git+https://github.com/suggestionsbot/disnake-ext-components.git@91689ed74ffee73f631453a39e548af9b824826d
dnspython==2.2.1
exceptiongroup==1.2.0
frozenlist==1.3.0
function-cooldowns==1.3.1
graphviz==0.20.1
h11==0.14.0
httpcore==1.0.2
httpx==0.26.0
humanize==4.2.0
idna==3.3
iniconfig==1.1.1
Expand All @@ -38,6 +44,8 @@ pytest==7.1.3
pytest-asyncio==0.19.0
python-dotenv==0.20.0
sentinels==1.0.0
skelmis-commons==1.1.0
sniffio==1.3.0
tomli==2.0.1
typing_extensions==4.3.0
websockets==10.4
Expand Down
5 changes: 1 addition & 4 deletions suggestions/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from bot_base import BotBase, BotContext, PrefixNotFound

from suggestions import State, Colors, Emojis, ErrorCode, Garven
from suggestions.clunk import Clunk
from suggestions.exceptions import (
BetaOnly,
MissingSuggestionsChannel,
Expand All @@ -49,7 +48,7 @@

class SuggestionsBot(commands.AutoShardedInteractionBot, BotBase):
def __init__(self, *args, **kwargs):
self.version: str = "Public Release 3.20"
self.version: str = "Public Release 3.21"
self.main_guild_id: int = 601219766258106399
self.legacy_beta_role_id: int = 995588041991274547
self.automated_beta_role_id: int = 998173237282361425
Expand All @@ -75,7 +74,6 @@ def __init__(self, *args, **kwargs):
self.state: State = State(self.db, self)
self.stats: Stats = Stats(self)
self.garven: Garven = Garven(self)
self.clunk: Clunk = Clunk(self.state)
self.suggestion_emojis: Emojis = Emojis(self)
self.old_prefixed_commands: set[str] = {
"changelog",
Expand Down Expand Up @@ -595,7 +593,6 @@ async def graceful_shutdown(self) -> None:
"""
log.debug("Attempting to shutdown")
self.state.notify_shutdown()
await self.clunk.kill_all()
await self.zonis.client.close()
await asyncio.gather(*self.state.background_tasks)
log.info("Shutting down")
Expand Down
3 changes: 0 additions & 3 deletions suggestions/clunk/__init__.py

This file was deleted.

25 changes: 0 additions & 25 deletions suggestions/clunk/cache.py

This file was deleted.

32 changes: 0 additions & 32 deletions suggestions/clunk/clunk.py

This file was deleted.

124 changes: 0 additions & 124 deletions suggestions/clunk/lock.py

This file was deleted.

7 changes: 4 additions & 3 deletions suggestions/cogs/suggestion_queue_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
from alaric.comparison import EQ
from alaric.logical import AND
from alaric.projections import Projection, SHOW
from bot_base import NonExistentEntry
from bot_base.caches import TimedCache
from commons.caching import NonExistentEntry, TimedCache
from disnake import Guild
from disnake.ext import commands, components

Expand All @@ -35,7 +34,9 @@ def __init__(self, bot):
self.state = self.bot.state
self.queued_suggestions_db: Document = self.bot.db.queued_suggestions
self.paginator_objects: TimedCache = TimedCache(
global_ttl=timedelta(minutes=15), lazy_eviction=False
global_ttl=timedelta(minutes=15),
lazy_eviction=False,
ttl_from_last_access=True,
)

async def get_paginator_for(
Expand Down
19 changes: 13 additions & 6 deletions suggestions/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
from alaric.logical import AND
from alaric.meta import Negate
from alaric.projections import PROJECTION, SHOW
from bot_base import NonExistentEntry
from bot_base.caches import TimedCache
from commons.caching import NonExistentEntry, TimedCache

from suggestions.objects import GuildConfig, UserConfig

Expand All @@ -42,17 +41,25 @@ def __init__(self, database: SuggestionsMongoManager, bot: SuggestionsBot):
)
self.guild_cache_ttl: timedelta = timedelta(minutes=15)
self.guild_cache: TimedCache[int, disnake.Guild] = TimedCache(
global_ttl=self.guild_cache_ttl, lazy_eviction=False
global_ttl=self.guild_cache_ttl,
lazy_eviction=False,
ttl_from_last_access=True,
)
self.view_voters_cache: TimedCache[int, list[str]] = TimedCache(
global_ttl=self.autocomplete_cache_ttl, lazy_eviction=False
global_ttl=self.autocomplete_cache_ttl,
lazy_eviction=False,
ttl_from_last_access=True,
)

self.guild_configs: TimedCache = TimedCache(
global_ttl=timedelta(minutes=30), lazy_eviction=False
global_ttl=timedelta(minutes=30),
lazy_eviction=False,
ttl_from_last_access=True,
)
self.user_configs: TimedCache = TimedCache(
global_ttl=timedelta(minutes=30), lazy_eviction=False
global_ttl=timedelta(minutes=30),
lazy_eviction=False,
ttl_from_last_access=True,
)

self.existing_error_ids: Set[str] = set()
Expand Down
6 changes: 2 additions & 4 deletions suggestions/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
import datetime
import logging
from enum import Enum
from typing import TYPE_CHECKING, Optional, Dict, Literal, Union, Type
from typing import TYPE_CHECKING, Optional, Type

import alaric
from alaric import Cursor, AQ
from alaric.comparison import EQ
from alaric.types import ObjectId
from bot_base.caches import TimedCache
from motor.motor_asyncio import AsyncIOMotorCollection
from commons.caching import TimedCache

from suggestions.objects.stats import MemberStats, MemberCommandStats

Expand Down
12 changes: 0 additions & 12 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import os
from unittest.mock import AsyncMock

import disnake
import pytest

from causar import Causar, InjectionMetadata

import suggestions
from suggestions.clunk import ClunkLock, Clunk
from tests.mocks import MockedSuggestionsMongoManager


Expand Down Expand Up @@ -43,13 +41,3 @@ async def injection_metadata(causar: Causar) -> InjectionMetadata:
return InjectionMetadata(
guild_id=881118111967883295, channel_id=causar.faker.generate_snowflake()
)


@pytest.fixture
async def clunk_lock(causar: Causar) -> ClunkLock:
return ClunkLock(causar.bot.state) # type: ignore


@pytest.fixture
async def clunk(causar: Causar) -> Clunk:
return Clunk(causar.bot.state) # type: ignore
Loading

0 comments on commit 806ba53

Please sign in to comment.