Cache Choices
Internally all data is ‘cached’ using an implementation
which implements antispam.abc.Cache
- In the standard package you have the following choices:
antispam.caches.MemoryCache(Default)antispam.caches.mongo.MongoCacheantispam.caches.redis.RedisCache
In order to use a cache other then the default one,
simply pass in an instance of the cache you wish to
use with the cache kwarg when initialising your
AntiSpamHandler.
Alternately, use the AntiSpamHandler.set_cache method.
Once a cache is registered like so, there is nothing else you need to do. The package will simply use that caching mechanism.
Also note, AntiSpamHandler will call antispam.abc.Cache.initialize()
before any cache operations are undertaken.
Redis Cache
Here is an example, note RedisCache needs a extra argument.
1import discord
2from discord.ext import commands
3from redis import asyncio as aioredis
4
5from antispam import AntiSpamHandler
6from antispam.caches.redis import RedisCache
7
8bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())
9bot.handler = AntiSpamHandler(bot)
10
11redis = aioredis.from_url("redis://localhost")
12redis_cache: RedisCache = RedisCache(bot.handler, redis)
13bot.handler.set_cache(redis_cache)
MongoDB Cache
Here is an example, note MongoCache needs a extra argument.
1import discord
2from discord.ext import commands
3
4from antispam import AntiSpamHandler
5from antispam.caches.mongo import MongoCache
6
7bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())
8bot.handler = AntiSpamHandler(bot)
9
10my_cache = MongoCache(bot.handler, "Mongo connection url")
11bot.handler.set_cache(my_cache)