Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for database caching #253

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

WaVEV
Copy link
Collaborator

@WaVEV WaVEV commented Feb 7, 2025

The MongoDB cache uses two different indexes:
Unique for the key and TTL index for the expiration date.

The Mongo's demon that deletes data from the collection is called every 60 seconds (if I am not mistaken) we have to deal with virtually deleted records (just filter if the record is expired)
So an extra exclude or include expired query are necessary.
Some scenarios to take in account:

  1. counting avoiding expired thing
  2. Do a set over an expired thing (must work as if the item isn't in the DB)
  3. When Cull (capping routine is called) remove by expiration date.

@WaVEV WaVEV requested review from timgraham and Jibola February 7, 2025 06:16
@WaVEV WaVEV force-pushed the add-support-for-database-caching branch from c7cbf04 to 1ee2a04 Compare February 7, 2025 16:07
self.assertEqual(caches["custom_key2"].get("answer2"), 42)

@override_settings(
CACHE_MIDDLEWARE_ALIAS=DEFAULT_CACHE_ALIAS,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use @modify_settings for appending to INSTALLED_APPS.

return pickle.loads(data, fix_imports=False) # noqa: S301


class Options:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be simpler to reuse (import) the Options and BaseDatabaseCache classes in the database cache backend. We can live with the fact that the attribute is called "table_name" rather than "collection name".

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I tried before, but I was struggling with Django test runner, it tries to create the cache table using the sql command. But I will try to find a workaround.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe my suggestion isn't clear or else I'm not understanding the issue. I don't see how importing createcachetable would affect the the test runner.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am researching to know why if I declare the same object in my test runs different if I import it. After this fix and some clean I will open it. It works well after all.

Copy link
Collaborator Author

@WaVEV WaVEV Feb 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When Django creates tables, it invokes createcachetable while running create_test_db.

To handle this, we could patch call_command to prevent this call or replace it with createcachecollection. Alternatively, we could overwrite the create_test_db method in DatabaseCreation.

Let me know if it is clear

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I can't do this, I wasn't able to add an installed app in this part of the flow, when Django test initializes the settings it put a default installed_apps in the settings. So, at that point I don't see a way to reach the new command. After the first init, I can overwrite the settings and add django_mongodb_backend in installed_apps.
If you have an idea how can I define a flow to reach django_mongodb_backend during this first initialization please let me know.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another idea is to call modify_settings in create_test_db, but I find it quite ugly. I don't think it's a good idea to set a global setting for the cache just for the test suite.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about putting the createcachecollection logic in a separate function or class so it can be called without call_command()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, like a test utils?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic could stay in this file.

@WaVEV WaVEV force-pushed the add-support-for-database-caching branch from 5f356db to 5be1867 Compare February 14, 2025 02:38
@WaVEV WaVEV marked this pull request as ready for review February 14, 2025 02:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants