Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 42 lines (36 sloc) 1.49 kB
9966d47 @chrisglass Source encoding changes for menus app
chrisglass authored
1 # -*- coding: utf-8 -*-
f193c4d @chrisglass - First try to have a process-safe caching mechanism for menu trees
chrisglass authored
2 from django.db import models
3
63253b5 @chrisglass Added a db table purge on exception
chrisglass authored
4
f193c4d @chrisglass - First try to have a process-safe caching mechanism for menu trees
chrisglass authored
5 class CacheKeyManager(models.Manager):
6 def get_keys(self, site_id=None, language=None):
7 ret = self.none()
8 if not site_id and not language:
9 # Both site and language are None - return everything
10 ret = self.all()
11 elif not site_id:
12 ret = self.filter(language=language)
13 elif not language:
14 ret = self.filter(site=site_id)
15 else:
16 # Filter by site_id *and* by language.
17 ret = self.filter(site=site_id).filter(language=language)
18 return ret
19
2e0dbd7 @chrisglass Forgot to add ** to the method signature...
chrisglass authored
20 def get_or_create(self, **kwargs):
63253b5 @chrisglass Added a db table purge on exception
chrisglass authored
21 try:
22 return super(CacheKeyManager, self).get_or_create(**kwargs)
23 except CacheKey.MultipleObjectsReturned:
24 # Truncate the table, we don't want a funny cache object to cause
25 # mayhem!
17e618b @chrisglass Fixed typo, added test
chrisglass authored
26 CacheKey.objects.all().delete()
63253b5 @chrisglass Added a db table purge on exception
chrisglass authored
27 return super(CacheKeyManager, self).get_or_create(**kwargs)
28
29
f193c4d @chrisglass - First try to have a process-safe caching mechanism for menu trees
chrisglass authored
30 class CacheKey(models.Model):
31 '''
63253b5 @chrisglass Added a db table purge on exception
chrisglass authored
32 This is to store a "set" of cache keys in a fashion where it's accessible
f193c4d @chrisglass - First try to have a process-safe caching mechanism for menu trees
chrisglass authored
33 by multiple processes / machines.
34 Multiple Django instances will then share the keys.
63253b5 @chrisglass Added a db table purge on exception
chrisglass authored
35 This allows for selective invalidation of the menu trees (per site, per
f193c4d @chrisglass - First try to have a process-safe caching mechanism for menu trees
chrisglass authored
36 language), in the cache.
37 '''
38 language = models.CharField(max_length=255)
39 site = models.PositiveIntegerField()
40 key = models.CharField(max_length=255)
63253b5 @chrisglass Added a db table purge on exception
chrisglass authored
41 objects = CacheKeyManager()
Something went wrong with that request. Please try again.