Compare commits
7 Commits
a54ef39e53
...
v0.2.0-rc.
Author | SHA1 | Date | |
---|---|---|---|
|
25f2595cf7 | ||
a4967e5b77 | |||
|
0228983d52 | ||
39a90d39fd | |||
9e9b6bc7dc
|
|||
d19a7381f3 | |||
cda570eb37
|
28
README.md
28
README.md
@@ -73,6 +73,34 @@ Mandatory keys to modify:
|
|||||||
|
|
||||||
After all of that you're good to go! Happy using :)
|
After all of that you're good to go! Happy using :)
|
||||||
|
|
||||||
|
## Caching
|
||||||
|
|
||||||
|
Although general database access speed is fast, caching might become helpful for
|
||||||
|
bigger servers with many bot interactions. Currently, Redis and Memcached are supported.
|
||||||
|
|
||||||
|
Configuration happens through the config key `caching`.
|
||||||
|
|
||||||
|
Set `caching.type` to the service of you choice ("redis" or "memcached") and then update
|
||||||
|
the URI to access the service. It's Redis' default URI format for Redis and "address:port"
|
||||||
|
for Memcached.
|
||||||
|
|
||||||
|
Which one should I choose?
|
||||||
|
|
||||||
|
| Service | Read/write speed | Config flexibility |
|
||||||
|
|-----------|------------------|--------------------|
|
||||||
|
| Redis | High | Very flexible |
|
||||||
|
| Memcached | Very high | Basic |
|
||||||
|
|
||||||
|
> Performance difference between Redis and Memcached is generally quite low, so your setup
|
||||||
|
> should normally depend more on the configuration flexibility than on raw speed.
|
||||||
|
|
||||||
|
## Debugging
|
||||||
|
|
||||||
|
There's a config key `debug` that can be set to `true` for debugging purposes.
|
||||||
|
|
||||||
|
It should be set to `false` in production, otherwise log becomes very verbose and might
|
||||||
|
contain data that shouldn't normally be logged.
|
||||||
|
|
||||||
## Docker [Experimental]
|
## Docker [Experimental]
|
||||||
|
|
||||||
As an experiment, Docker deployment option has been added.
|
As an experiment, Docker deployment option has been added.
|
||||||
|
6
classes/cache/holo_cache_redis.py
vendored
6
classes/cache/holo_cache_redis.py
vendored
@@ -1,6 +1,6 @@
|
|||||||
import logging
|
import logging
|
||||||
from logging import Logger
|
from logging import Logger
|
||||||
from typing import Dict, Any, List
|
from typing import Dict, Any
|
||||||
|
|
||||||
from redis import Redis
|
from redis import Redis
|
||||||
|
|
||||||
@@ -25,9 +25,7 @@ class HoloCacheRedis(HoloCache):
|
|||||||
"Cache configuration is invalid. Please check if all keys are set (engine: memcached)"
|
"Cache configuration is invalid. Please check if all keys are set (engine: memcached)"
|
||||||
)
|
)
|
||||||
|
|
||||||
uri_split: List[str] = engine_config["uri"].split(":")
|
return cls(Redis.from_url(engine_config["uri"]))
|
||||||
|
|
||||||
return cls(Redis(host=uri_split[0], port=int(uri_split[1])))
|
|
||||||
|
|
||||||
def get_json(self, key: str) -> Any | None:
|
def get_json(self, key: str) -> Any | None:
|
||||||
try:
|
try:
|
||||||
|
@@ -5,7 +5,7 @@ from libbot.pycord.classes import PycordBot
|
|||||||
|
|
||||||
from classes.cache.holo_cache_memcached import HoloCacheMemcached
|
from classes.cache.holo_cache_memcached import HoloCacheMemcached
|
||||||
from classes.cache.holo_cache_redis import HoloCacheRedis
|
from classes.cache.holo_cache_redis import HoloCacheRedis
|
||||||
from modules.cache_utils import create_cache_client
|
from modules.cache_manager import create_cache_client
|
||||||
|
|
||||||
logger: Logger = logging.getLogger(__name__)
|
logger: Logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@@ -28,7 +28,7 @@
|
|||||||
"uri": "127.0.0.1:11211"
|
"uri": "127.0.0.1:11211"
|
||||||
},
|
},
|
||||||
"redis": {
|
"redis": {
|
||||||
"uri": "127.0.0.1:6379"
|
"uri": "redis://127.0.0.1:6379/0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"logging": {
|
"logging": {
|
||||||
|
@@ -15,7 +15,7 @@ class Migration(BaseMigration):
|
|||||||
{
|
{
|
||||||
"type": None,
|
"type": None,
|
||||||
"memcached": {"uri": "127.0.0.1:11211"},
|
"memcached": {"uri": "127.0.0.1:11211"},
|
||||||
"redis": {"uri": "127.0.0.1:6379"},
|
"redis": {"uri": "redis://127.0.0.1:6379/0"},
|
||||||
},
|
},
|
||||||
*[],
|
*[],
|
||||||
)
|
)
|
||||||
|
29
modules/cache_manager.py
Normal file
29
modules/cache_manager.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
from typing import Dict, Any, Literal
|
||||||
|
|
||||||
|
from classes.cache.holo_cache_memcached import HoloCacheMemcached
|
||||||
|
from classes.cache.holo_cache_redis import HoloCacheRedis
|
||||||
|
|
||||||
|
|
||||||
|
def create_cache_client(
|
||||||
|
config: Dict[str, Any],
|
||||||
|
engine: Literal["memcached", "redis"] | None = None,
|
||||||
|
) -> HoloCacheMemcached | HoloCacheRedis:
|
||||||
|
if engine not in ["memcached", "redis"] or engine is None:
|
||||||
|
raise KeyError(
|
||||||
|
f"Incorrect cache engine provided. Expected 'memcached' or 'redis', got '{engine}'"
|
||||||
|
)
|
||||||
|
|
||||||
|
if "cache" not in config or engine not in config["cache"]:
|
||||||
|
raise KeyError(
|
||||||
|
f"Cache configuration is invalid. Please check if all keys are set (engine: '{engine}')"
|
||||||
|
)
|
||||||
|
|
||||||
|
match engine:
|
||||||
|
case "memcached":
|
||||||
|
return HoloCacheMemcached.from_config(config["cache"][engine])
|
||||||
|
case "redis":
|
||||||
|
return HoloCacheRedis.from_config(config["cache"][engine])
|
||||||
|
case _:
|
||||||
|
raise KeyError(
|
||||||
|
f"Cache implementation for the engine '{engine}' is not present."
|
||||||
|
)
|
@@ -1,37 +1,9 @@
|
|||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from typing import Dict, Any, Literal
|
from typing import Any
|
||||||
|
|
||||||
from bson import ObjectId
|
from bson import ObjectId
|
||||||
from ujson import dumps, loads
|
from ujson import dumps, loads
|
||||||
|
|
||||||
from classes.cache.holo_cache_memcached import HoloCacheMemcached
|
|
||||||
from classes.cache.holo_cache_redis import HoloCacheRedis
|
|
||||||
|
|
||||||
|
|
||||||
def create_cache_client(
|
|
||||||
config: Dict[str, Any],
|
|
||||||
engine: Literal["memcached", "redis"] | None = None,
|
|
||||||
) -> HoloCacheMemcached | HoloCacheRedis:
|
|
||||||
if engine not in ["memcached", "redis"] or engine is None:
|
|
||||||
raise KeyError(
|
|
||||||
f"Incorrect cache engine provided. Expected 'memcached' or 'redis', got '{engine}'"
|
|
||||||
)
|
|
||||||
|
|
||||||
if "cache" not in config or engine not in config["cache"]:
|
|
||||||
raise KeyError(
|
|
||||||
f"Cache configuration is invalid. Please check if all keys are set (engine: '{engine}')"
|
|
||||||
)
|
|
||||||
|
|
||||||
match engine:
|
|
||||||
case "memcached":
|
|
||||||
return HoloCacheMemcached.from_config(config["cache"][engine])
|
|
||||||
case "redis":
|
|
||||||
return HoloCacheRedis.from_config(config["cache"][engine])
|
|
||||||
case _:
|
|
||||||
raise KeyError(
|
|
||||||
f"Cache implementation for the engine '{engine}' is not present."
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def json_to_string(json_object: Any) -> str:
|
def json_to_string(json_object: Any) -> str:
|
||||||
json_object_copy: Any = deepcopy(json_object)
|
json_object_copy: Any = deepcopy(json_object)
|
||||||
|
Reference in New Issue
Block a user