-
Notifications
You must be signed in to change notification settings - Fork 10
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
base: main
Are you sure you want to change the base?
Conversation
c7cbf04
to
1ee2a04
Compare
self.assertEqual(caches["custom_key2"].get("answer2"), 42) | ||
|
||
@override_settings( | ||
CACHE_MIDDLEWARE_ALIAS=DEFAULT_CACHE_ALIAS, |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()
?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
5f356db
to
5be1867
Compare
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: