Sunday, December 21, 2014

Re: Raw access to cache table


On Sun, Dec 21, 2014 at 12:55 PM, Collin Anderson <cmawebsite@gmail.com> wrote:
Hi Erik,

If you want a nicer interface, I just ran manage.py inspecdb on one of my databases and got this for the cache table. Not sure why django still does it by hand.

Gather `round, children, and let Grandpa fill you in on the details... :-)

Although it isn't huge, Django's ORM does have an overhead. If you're looking for the general capability to query rows on a database, this overhead is a reasonable price to pay - you have a little overhead, but it's a lot easier to express complex queries. However, in the case of a cache table, you need to issue exactly 2 queries:

1) Get me the object with key X.

2) Set the value of key X.

These two queries are trivial to write -- so trivial in fact, that they don't even need to rewritten as they move across database backends. They're as "vanilla" as a query can get in SQL. 

So - this becomes a case study in when *not* to use the ORM. We know the 2 queries that need to be issued. That SQL isn't hard to write or maintain. And using the ORM would impose a non-trivial runtime overhead - and by definition, a cache backend is supposed to be as efficient as possible. Raw SQL is the "right" answer here.

(well.... not using a database backed cache backend is the *really* right answer, but I'll let that slide for the moment...)

If you *do* want to do complex queries on the database cache table, the approach suggested by Collin is as good an approach as any. A managed table will give you ORM operations over an arbitrary table - include Django's own internal tables.

That said, I'll also concur that the database cache backend is the wrong answer here. If you're writing a cron script, the approach I've always used is PIDfile based lock:


Yours,
Russ Magee %-)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJxq84-CzPsnoH4%3DT%2BfT9hEMw%2BobZpi5uHVh%3DintdcJjKMsY1Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment