GSOC 2023: Improving the databse cache backend

Hi there @carltongibson , my name is Chiso and im looking to contribut to django’s improvement this summer, and I just want to know if this sounds like a good proposalto begin.

Proposal: Improve the Database Cache Backend : Abstract
Introduction

The Database Cache Backend is an essential component of Django that enables caching in a shared environment. However, the current implementation has limitations that make it less efficient than it could be. This proposal aims to improve the Database Cache Backend by adapting the Django-Mysql DB cache backend implementation for other supported databases.
Problem Statement

The current implementation of the Database Cache Backend uses a locmemcache that is per process, which is not ideal for shared environments. This makes it less efficient and limits its usefulness for third-party packages. To address this limitation, we propose to improve the Database Cache Backend by using the more efficient and widely used shared cache backend.
Proposed Solution

The proposed solution is to adapt the Django-Mysql DB cache backend implementation for other supported databases. This implementation has been proven to be much more efficient than the current Database Cache Backend and has been in use for the past six years.

The solution involves translating the SQL used in the Django-Mysql DB cache backend implementation to different dialects for other supported databases. This will enable the Database Cache Backend to use the same efficient caching mechanism as the Django-Mysql DB cache backend implementation.

Here is a snippet of implementation code that demonstrates the changes required to adapt the Django-Mysql DB cache backend implementation for other supported databases:

python

from django.db import models

class Cache(models.Model):
    key = models.CharField(max_length=255, unique=True)
    value = models.BinaryField()
    expires = models.DateTimeField()

    class Meta:
        db_table = 'cache_table'
        indexes = [
            models.Index(fields=['expires'], name='cache_expires'),
        ]

This code defines a new model that represents the cache table in the database. The model includes a key field to store the cache key, a value field to store the serialized cache value, and an expires field to store the cache expiration time. The model also includes an index on the expires field to make it easier to query for expired cache entries.
Implementation Details

The proposed solution requires changes to the codebase to enable the translation of SQL to different dialects. We will modify the current Database Cache Backend code to incorporate the changes required for the efficient caching mechanism.

We will also provide code examples to demonstrate how the new implementation will work and how it can be tested to ensure compatibility with other parts of Django. Testing will involve running unit tests to ensure that the implementation works as expected and does not break other parts of the Django codebase.
Expected Outcome

The expected outcome of this project is a significant improvement in the performance and efficiency of the Database Cache Backend. This will make it more useful for third-party packages and enhance the overall performance of Django. The new implementation will be more widely adopted and easier to maintain, making it a significant improvement over the current implementation.

The timeline for this project will be approximately 350 hours, with the expected release of the improvement by the end of the summer .
Stretch Goals

As a stretch goal, we propose hooking the database cache tables into migrations to make the process more efficient. Currently, this is achieved through a hacky duck typing approach, which is not ideal.

The goal is to provide a more efficient and effective approach to migrations. This will require modifying the current codebase to hook the database cache tables into migrations effectively.
Key Skills

The key skills required for this project include Cache Framework and SQL expertise. We will also require familiarity with the Django codebase and unit testing to ensure compatibility with other parts of Django.

p.s: I know this is a late contribution period, but i have been caught up with school and work and did not know the proposal period had began .