1

Possible Duplicate:
Django cache.set() causing duplicate key error

I ran into this problem using django core's database cache:

ERROR:  duplicate key value violates unique constraint "cache_pkey"
STATEMENT:  INSERT INTO "cache" (cache_key, value, expires) VALUES (E':1:cms-menu_nodes_en-us_1', E'gAJdcQEoY21lbnVzLmJhc2UKTmF2aW
LOG:  server process (PID 8453) was terminated by signal 9: Killed
LOG:  terminating any other active server processes
LOG:  all server processes terminated; reinitializing
FATAL:  could not create shared memory segment: Cannot allocate memory
DETAIL:  Failed system call was shmget(key=5432001, size=29278208, 03600).

I looked in the table and sure enough, there is an entry for the key ':1:cms-menu_nodes_en-us_1'. I found a similar issue here, but was unable to exactly understand what the issue is.

Anyone have any ideas or suggestions? Sounds like a bug in django core, since if a key exist, it should update the record.

edit: I should have clarified that the DB was PostgreSQL 8.4.7. Thanks lazerscience.

edit @ Jack M: I haven't been able to replicate this error, but believe the code is in django.core.cache.backends.db.DatabaseCache in a method called set() that calls _base_set().

2
  • With what database backend did you try it? Try it with others as well... Commented Jul 12, 2011 at 16:39
  • What is the code which is adding this entry to the cache? Commented Jul 12, 2011 at 20:12

1 Answer 1

1

Sounds like a bug in django core, since if a key exist, it should update the record.

Indeed, but I'd suggest that said bug is related to a concurrency issue, in which case it could be fixed at the app level. As in two neighboring calls to the same asset/page/whatever run an exist() statement, find no row, and proceed to insert as a result -- without issuing a lock of any kind, and without wrapping the thing in a transaction to discard the offending call and (since it's just a cache) continuing.

It also begs one question: are you sure that you should be caching in your database in the first place? The database typically is a bottleneck in a web application (especially when using an ORM), and the whole point of caching is to avoid that bottleneck. Shouldn't you be using memcache instead?

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

1 Comment

My issue is that I'm memory constrained, so adding memcache is not an option. The webpage makes an in-ordinate number of sql calls so instead of optimizing in code, a single database call significantly sped things up. That's assuming the cache works....

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.