1

I have a django project where I have some database tables.

One of the database tables is designed to store messages and their titles. This helps me to create/alter these messages from my django-admin.

Now I want to initialize a variable (as a dictionary) from this table as follows :

MY_MSGS = {record.name : {'title':record.title, 'message':record.message} for record in MyTable.objects.all()}

This must happen at the server startup for now. MY_MSGS must be accessible to the different view-files.

Later I would want to periodically update MY_MSGS by reading MyTable again.

So I want that My_MSGS behaves as a all global to all my view-files and should be initialized after the startup is complete.

FYI I have multiple view-files that are all imported from views.py. Also this is a very small table with just about maximum 15 messages and so I do not mind holding this data in memory

1
  • Given you initialize it at startup, if you edit a message, it will not update until you restart the webserver? Commented Feb 12 at 21:54

1 Answer 1

2

I think the main concern is that you should not run the query immediately, but after Django has initialized the models, etc.

We can do that by postponing the load procedure, and do it when we really need a message, with:

def get_message(name):
    cache = get_message.cache
    if cache is None:
        cache = get_message.cache = {
            record.name: {'title': record.title, 'message': record.message}
            for record in MyTable.objects.all()
        }
    return cache.get(name)


get_message.cache = None

and thus use this as a function, like:

get_message(my_record_name)

then it is of course still important to make sure you never call the function during initialization, so not pass this as a default=… [Django-doc], etc. of a method field for example.

An extra advantage is that as long as you don't need any message, you don't fetch these. If we do, we will not do it a second time.

But usually that might be a problem: typically you don't restart the Django server very often in production, so the messages can remain the old ones for months.

Sign up to request clarification or add additional context in comments.

2 Comments

...or they can use functools.cache, or a third party package such as cachetools.func.ttl_cache if they are worried about invalidating the cache after a set amount of time.
The functools.cache will cache the result of a call, but here we thus do the work once for all keys. So once the first key is requested, all are available.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.