New Backends
Three more backends are introduced:
- locmem: - simple thread-safe in-memory cache. Good for small web sites.
- file: - file-based multi-process/thread-safe cache.
- Example: file:///tmp/django.cache/lazutkin.com/ - creates directory /tmp/django.cache/lazutkin.com and stores cached items as individual files.
- sql: - sql-based multi-process/thread-safe cache.
- Example: sql://cache/ --- uses table cache in current database to store cached items.
All three backends accept arguments described in Django's cache framework. Examples: sql://cache/?timeout=60, locmem:///?max_entries=10&cull_frequency=2.
Additionally simple: cache backend's bug is fixed.
Notes:
- All backends use pickle (cPickle) as means to save the object. I decided against marshal or custom solutions. For now.
- locmem:
- Requires reader-writer lock, which is implemented in synch.py (attached). This file should be placed into django/utils directory.
- Implements simplified (a-la simple:) cache culling strategy, when each cull_frequency item is removed to make room.
- file:
- Implements simplified (a-la simple:) cache culling strategy, when each cull_frequency item is removed to make room.
- sql:
- When it is time to cull cache, sql: removes expired items first, then removes oldest items by access time to satisfy cull_frequency. This strategy has a drawback: every time cached item is accessed, its access time filed is updated.
- It can be speeded up with stored procedures.
Missing django-admin Support
The missing part is django-admin support for:
- creation of cache table for sql: backend
- creation of cache indices for sql:
- cleaning up sql: and file: caches
Pseudo model for cache table is:
class Cache(meta.Model):
# id is assumed
keyx = meta.CharField(maxlength=255, unique=True),
expiration = meta.DateTimeField(db_index=True),
accessed = meta.DateTimeField(db_index=True),
valx = meta.TextField(), # should be big enough to hold an item
For TextField on MySQL see #489 --- when you create cache table manually make sure to use longtext instead of text.
If you want to use MySQL, use patch from #463. Otherwise you may have strange random errors.
Testing
Notes:
- I ran all new backends for several days monitoring cache entries.
- sql: was tested locally using MySQL and SQLite. I use MySQL/MyISAM-based cache on my web site. I didn't try it with PostgreSQL nor MySQL/InnoDB.