4

How can I load resources from mysql database when Django starts up and put it in memory (Redis) to use by all the applications.

I have seen this [https://docs.djangoproject.com/en/dev/ref/applications/#django.apps.AppConfig.ready]

class MyAppConfig(AppConfig):
        def ready(self):

But they mention not use db connections inside ready function. How can do it when my Website starts.?

And Can I also set cache value inside ready ?

from django.core.cache import cache
cache.set()
Karesh A
  • 1,731
  • 3
  • 22
  • 47

1 Answers1

2

Since you are only loading into redis rather than creating instances of models that are held in memory and shared by all apps in your website, perhaps the best way is to use a custom management command.

A second solution is to create a Django CLI, as posted by e4c5 on the ex-documentation:

Supposing you have setup a django project, and the settings file is in an app named main, this is how you initialize your code

import os, sys

# Setup environ
sys.path.append(os.getcwd())
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "main.settings")

# Setup django
import django
django.setup()

# rest of your imports go here

from main.models import MyModel

# normal python code that makes use of Django models go here

for obj in MyModel.objects.all():
    print obj

The above can be executed as

python main/cli.py

Since you are using redis, do you really need to store things in memcache as well? But if you need to that too is something that can be done from a CLI

Community
  • 1
  • 1
e4c5
  • 52,766
  • 11
  • 101
  • 134
  • Is there any way we can store the values to application context. And access by all the users. ? Even if we have, when we deploy to apache, the wsgi instance may restart. So I guess I need to store in memcahe for frequently changing realtime data for all the users. – Karesh A Sep 16 '16 at 05:10
  • So your command/CLI we have to run separately? How can we integrate everything into one web project and start when Apache starts. – Karesh A Sep 16 '16 at 05:13
  • See the whole point is that this is not at all related to apache restarts. If you are expiring an item from the cache you should do it from where ever in the code where the item is modified and not wait for an apache restart. – e4c5 Sep 16 '16 at 05:15
  • On good installations, apache can run for months if not years without having to be restarted so your command should really be independent of that. – e4c5 Sep 16 '16 at 05:15
  • ok. is there any way to create one global object inside django and access it in all the apps.? Something like inside settings.py in Django 1.10. – Karesh A Sep 16 '16 at 05:38
  • yes that's quite possible but that's not what your question says – e4c5 Sep 16 '16 at 05:39
  • But this answers saying not to include anything like that. http://stackoverflow.com/questions/2680902/python-django-global-variables – Karesh A Sep 16 '16 at 05:52
  • 1
    you asked me whether it's possible and I said it is, but didn't discuss the pros and cons of it. Why don't you post another question on that topic? – e4c5 Sep 16 '16 at 05:59