Permalink
Browse files

abstract into a managers.py

  • Loading branch information...
coffindragger committed Jul 12, 2012
1 parent b332adf commit c3d90908c98045b31cfefcd0e4ecb582c5e65a35
Showing with 52 additions and 50 deletions.
  1. +50 −0 sky_redirects/managers.py
  2. +2 −50 sky_redirects/models.py
View
@@ -0,0 +1,50 @@
+from django.db import models
+from django.core.cache import cache
+
+class IndexedCachedTableManager(models.Manager):
+ def __init__(self, indexed_field, *args, **kwargs):
+ super(IndexedCachedTableManager, self).__init__(*args, **kwargs)
+ self.indexed_field = indexed_field
+
+ def _cache_key(self):
+ return '%s:indexed_cached_table' % (self.model.__class__.__name__,)
+
+ def cached_index(self):
+ """Returned the cached index, generate it if not present."""
+ index = cache.get(self._cache_key())
+ if index is None:
+ index = self.rebuild_cache()
+ return index
+
+ def rebuild_cache(self):
+ """Generate the indexed dictionary and publish it to the django cache"""
+ index = {}
+ for obj in self.all().select_related():
+ key = getattr(obj, self.indexed_field, None)
+ if key is not None:
+ index[key] = obj
+ cache.set(self._cache_key(), index, 365*86400) # cache forever
+ return index
+
+class OrderedCachedTableManager(models.Manager):
+ def __init__(self, ordering_field, *args, **kwargs):
+ super(OrderedCachedTableManager, self).__init__(*args, **kwargs)
+ self.ordering_field = ordering_field
+
+ def _cache_key(self):
+ return '%s:ordered_cached_table' % (self.model.__class__.__name__,)
+
+ def cached_index(self):
+ """Returned the cached index, generate it if not present."""
+ index = cache.get(self._cache_key())
+ if index is None:
+ index = self.rebuild_cache()
+ return index
+
+ def rebuild_cache(self):
+ index = []
+ for obj in self.all().order_by(self.ordering_field):
+ index.append(obj)
+ cache.set(self._cache_key(), index, 365*86400) # cache forever
+ return index
+
View
@@ -1,8 +1,9 @@
from django.db import models
from django.contrib.sites.models import Site
-from django.core.cache import cache
import re
+from sky_redirects.managers import IndexedCachedTableManager, OrderedCachedTableManager
+
class RedirectType(object):
PERMANENT='301'
@@ -13,55 +14,6 @@ class RedirectType(object):
)
-class IndexedCachedTableManager(models.Manager):
- def __init__(self, indexed_field, *args, **kwargs):
- super(IndexedCachedTableManager, self).__init__(*args, **kwargs)
- self.indexed_field = indexed_field
-
- def _cache_key(self):
- return '%s:indexed_cached_table' % (self.model.__class__.__name__,)
-
- def cached_index(self):
- """Returned the cached index, generate it if not present."""
- index = cache.get(self._cache_key())
- if index is None:
- index = self.rebuild_cache()
- return index
-
- def rebuild_cache(self):
- """Generate the indexed dictionary and publish it to the django cache"""
- index = {}
- for obj in self.all().select_related():
- key = getattr(obj, self.indexed_field, None)
- if key is not None:
- index[key] = obj
- cache.set(self._cache_key(), index, 365*86400) # cache forever
- return index
-
-class OrderedCachedTableManager(models.Manager):
- def __init__(self, ordering_field, *args, **kwargs):
- super(OrderedCachedTableManager, self).__init__(*args, **kwargs)
- self.ordering_field = ordering_field
-
- def _cache_key(self):
- return '%s:ordered_cached_table' % (self.model.__class__.__name__,)
-
- def cached_index(self):
- """Returned the cached index, generate it if not present."""
- index = cache.get(self._cache_key())
- if index is None:
- index = self.rebuild_cache()
- return index
-
- def rebuild_cache(self):
- index = []
- for obj in self.all().order_by(self.ordering_field):
- index.append(obj)
- cache.set(self._cache_key(), index, 365*86400) # cache forever
- return index
-
-
-
class CachedTable(models.Model):
class Meta:
abstract = True

0 comments on commit c3d9090

Please sign in to comment.