Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching MongoDB connections in Django

I'm using the standard (as opposed to NonRel) version of Django connected to PostgreSQL on top of Apache + mod_wsgi. This setup also connects to MongoDB (some data is saved externally). Right now I have to create a new MongoDB connection for each Django request, and pass it along throughout the call stack to all functions that require access to MongoDB. Is there a way to cache connections between requests?

Edit

At the risk of blasphemy, would a global variable work in this case?

like image 770
Assaf Lavie Avatar asked Jun 12 '12 08:06

Assaf Lavie


People also ask

Can we use MongoDB for caching?

Does MongoDB handle caching? Yes. MongoDB keeps most recently used data in RAM. If you have created indexes for your queries and your working data set fits in RAM, MongoDB serves all queries from memory.

Is it good to use MongoDB with Django?

Django, the most popular Python web framework, is an ideal tool to build secure and easy-to-maintain applications using MongoDB. Using MongoDB with Django is advantageous because: Every second, more and more unstructured data is generated from various sources like chats, real-time streams, feeds, and surveys.

Does MongoDB have in-memory cache?

The short answer is MongoDB relies on both its internal memory caches as well as the operating system's cache. The OS cache generally is seen as “Unallocated” by sysadmins, dba's, and devs. This means they steal memory from the OS and allocate it internally to MongoDB.


1 Answers

There are several ways explaining how pymongo can work (or fail) with mod_wsgi, suggested here: http://api.mongodb.org/python/current/faq.html?highlight=wsgi#does-pymongo-work-with-mod-wsgi

In addition you can use some kind of pooling solution, like described here: http://www.mongodb.org/display/DOCS/Notes+on+Pooling+for+Mongo+Drivers

One project that I know already to have pooling is MongoEngine, its a very simple ORM that uses pymongo behind the scenes. You might want to look into it together with the pymongo faq solutions above.

like image 162
Evgeny Avatar answered Sep 28 '22 04:09

Evgeny