Skip to content

Commit

Permalink
Speed-up startup and fix clear cache (#1045)
Browse files Browse the repository at this point in the history
Speed-up startup
  • Loading branch information
marcelveldt authored Jan 29, 2024
1 parent a652572 commit 52ca551
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
28 changes: 12 additions & 16 deletions music_assistant/server/controllers/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,11 @@ async def get_config_entries(
label="Clear cache",
description="Reset/clear all items in the cache. ",
),
# ConfigEntry(
# key=CONF_BIND_IP,
# type=ConfigEntryType.STRING,
# default_value=default_ip,
# label="Bind to IP/interface",
# description="Start the streamserver on this specific interface. \n"
# "This IP address is communicated to players where to find this server. "
# "Override the default in advanced scenarios, such as multi NIC configurations. \n"
# "Make sure that this server can be reached "
# "on the given IP and TCP port by players on the local network. \n"
# "This is an advanced setting that should normally "
# "not be adjusted in regular setups.",
# advanced=True,
# ),
)

async def setup(self, config: CoreConfig) -> None: # noqa: ARG002
"""Async initialize of cache module."""
self.logger.info("Initializing cache controller...")
await self._setup_database()
self.__schedule_cleanup_task()

Expand Down Expand Up @@ -154,18 +141,29 @@ async def delete(self, cache_key):
async def clear(self, key_filter: str | None = None) -> None:
"""Clear all/partial items from cache."""
self._mem_cache = {}
self.logger.info("Clearing database...")
query = f"key LIKE '%{key_filter}%'" if key_filter else None
await self.database.delete(DB_TABLE_CACHE, query=query)
await self.database.vacuum()
self.logger.info("Clearing database DONE")

async def auto_cleanup(self):
"""Sceduled auto cleanup task."""
self.logger.info("Running automatic cleanup...")
# for now we simply reset the memory cache
self._mem_cache = {}
cur_timestamp = int(time.time())
cleaned_records = 0
for db_row in await self.database.get_rows(DB_TABLE_CACHE):
# clean up db cache object only if expired
if db_row["expires"] < cur_timestamp:
await self.delete(db_row["key"])
cleaned_records += 1
if cleaned_records > 50:
self.logger.debug("Compacting database...")
await self.database.vacuum()
self.logger.debug("Compacting database done")
self.logger.info("Automatic cleanup finished (cleaned up %s records)", cleaned_records)

async def _setup_database(self):
"""Initialize database."""
Expand Down Expand Up @@ -202,8 +200,6 @@ async def _setup_database(self):
DB_TABLE_SETTINGS,
{"key": "version", "value": str(DB_SCHEMA_VERSION), "type": "str"},
)
# compact db
await self.database.execute("VACUUM")

async def __create_database_tables(self) -> None:
"""Create database table(s)."""
Expand Down
4 changes: 3 additions & 1 deletion music_assistant/server/controllers/music.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,9 @@ async def _setup_database(self):
# create indexes if needed
await self.__create_database_indexes()
# compact db
await self.database.execute("VACUUM")
self.logger.debug("Compacting database...")
await self.database.vacuum()
self.logger.debug("Compacting database done")

async def __create_database_tables(self) -> None:
"""Create database tables."""
Expand Down
5 changes: 5 additions & 0 deletions music_assistant/server/helpers/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,8 @@ async def iter_items(
if len(next_items) < limit:
break
offset += limit

async def vacuum(self) -> None:
"""Run vacuum command on database."""
await self._db.execute("VACUUM")
await self._db.commit()

0 comments on commit 52ca551

Please sign in to comment.