Skip to content

Commit

Permalink
more doc (read the stuff in base.py thanks :P)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcramer committed Jan 24, 2008
1 parent f4991b4 commit bd21c4c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
31 changes: 30 additions & 1 deletion cache/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,36 @@

DEFAULT_CACHE_TIME = 60*60*60 # the maximum an item should be in the cache

# Signals rundown:
# .cache(expire_on=['create', 'update', 'delete'])
# use namespaces possible so the cache key becomes key_name:expire_namespace(not always present):hash

# for example, a call with no expires:
# db_table:hash

# a call with a delete expires
# db_table:0,0,0:hash

# the numbers represent our current namespace level for the 3 expiration methods
# in order to do this, we'd have to actually store another cache key per model
# and to support threading, query that cache key everytime we do any cache queryset
# hit
# e.g. cache.get('ns:db_table') = 0,0,0

# when a new row is created, we'd set that to 1,0,0
# which would invalidate anything that had a create expiration set because the key is
# now invalid, because the namespace changed.

# We can also add a table namespace, which says "delete everything" so our
# cache key now becomes db_table:ns_count:0,0,0:hash
# where the 0,0,0: is optional

# ns_count would be stored in the same ns:db_table key and starts at 0
# this would most likely only be incremented if you did a push to your site
# and needed to say wipe all articles because the dataset changed.

class CachedModelBase(ModelBase):
# TODO: find a way to not overwrite __new__ like this
def __new__(cls, name, bases, attrs):
# If this isn't a subclass of CachedModel, don't do anything special.
try:
Expand Down Expand Up @@ -90,7 +119,7 @@ class CachedModel(Model):
# Maybe this would work?
@classmethod
def _prepare(cls):
# How do we extend the parent classes classmethod properly?
# TODO: How do we extend the parent classes classmethod properly?
# super(CachedModel, cls)._prepare() errors
opts = cls._meta
opts._prepare(cls)
Expand Down
11 changes: 8 additions & 3 deletions cache/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,18 @@ def _clone(self, klass=None, **kwargs):
return c

def _get_sorted_clause_key(self):
if self.cache_key_name is not None:
return self.cache_key_name
return (isinstance(i, basestring) and i.lower().replace('`', '').replace("'", '') or str(tuple(sorted(i))) for i in self._get_sql_clause())

def _get_cache_key(self, extra=''):
# TODO: Need to figure out if this is the best use.
# Maybe we should use extra for cache_key_name, extra was planned for use
# in things like .count() as it's a different cache key than the normal queryset,
# but that also doesn't make sense because theoretically count() is already different
# sql so the sorted_sql_clause should have figured that out.
if self.cache_key_name is not None:
return '%s:%s' % (self.cache_key_prefix, self.cache_key_name)
if extra not in self._cache_keys:
self._cache_keys[extra] = '%s%s%s' % (self.cache_key_prefix, str(hash(''.join(self._get_sorted_clause_key()))), extra)
self._cache_keys[extra] = '%s:%s:%s' % (self.cache_key_prefix, str(hash(''.join(self._get_sorted_clause_key()))), extra)
return self._cache_keys[extra]

def _prepare_queryset_for_cache(self, queryset):
Expand Down

0 comments on commit bd21c4c

Please sign in to comment.