Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
version 0.3 and ability to record all cached URLs
- Loading branch information
Showing
16 changed files
with
491 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
__version__ = '0.2' | ||
__version__ = '0.3' | ||
from cache_page import cache_page |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
__doc__ = """ | ||
If you enable `FANCY_REMEMBER_ALL_URLS` then every URL take is turned | ||
into a cache key for cache_page() to remember is recorded. | ||
You can use this to do statistics or to do invalidation by URL. | ||
To use: simply add the URL patterns after like this:: | ||
$ ./manage.py %(this_file)s /path1.html /path3/*/*.json | ||
To show all cached URLs simply run it with no pattern like this:: | ||
$ ./manage.py %(this_file)s | ||
Equally the ``--purge`` switch can always be added. For example, | ||
running this will purge all cached URLs:: | ||
$ ./manage.py %(this_file)s --purge | ||
If you enable `FANCY_REMEMBER_STATS_ALL_URLS` you can get a tally for each | ||
URL how many cache HITS and MISSES it has had. | ||
""" % dict(this_file=__file__) | ||
|
||
from optparse import make_option | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
from fancy_cache.memory import find_urls | ||
|
||
|
||
class Command(BaseCommand): | ||
help = __doc__.strip() | ||
|
||
option_list = BaseCommand.option_list + ( | ||
make_option( | ||
'-p', '--purge', dest='purge', action='store_true', | ||
help='Purge found URLs' | ||
), | ||
) | ||
args = 'urls' | ||
|
||
def handle(self, *urls, **options): | ||
verbose = int(options['verbosity']) > 1 | ||
|
||
_count = 0 | ||
for url, cache_key, stats in find_urls(urls, purge=options['purge']): | ||
_count += 1 | ||
if stats: | ||
print url[:70].ljust(65), | ||
print "HITS", str(stats['hits']).ljust(5), | ||
print "MISSES", str(stats['misses']).ljust(5) | ||
|
||
else: | ||
print url | ||
|
||
if verbose: | ||
print "-- %s URLs cached --" % _count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import re | ||
|
||
from django.core.cache import cache | ||
|
||
from fancy_cache.middleware import REMEMBERED_URLS_KEY, LONG_TIME | ||
|
||
__all__ = ('find_urls',) | ||
|
||
|
||
def _match(url, regexes): | ||
if not regexes: | ||
return url | ||
for regex in regexes: | ||
if regex.match(url): | ||
return True | ||
return False | ||
|
||
|
||
def _urls_to_regexes(urls): | ||
regexes = [] | ||
for each in urls: | ||
parts = each.split('*') | ||
if len(parts) == 1: | ||
regexes.append(re.compile(re.escape(parts[0]))) | ||
else: | ||
_re = '.*'.join(re.escape(x) for x in parts) | ||
regexes.append(re.compile(_re)) | ||
return regexes | ||
|
||
|
||
def find_urls(urls, purge=False): | ||
remembered_urls = cache.get(REMEMBERED_URLS_KEY, {}) | ||
_del_keys = [] | ||
regexes = _urls_to_regexes(urls) | ||
|
||
for url in remembered_urls: | ||
if _match(url, regexes): | ||
cache_key = remembered_urls[url] | ||
if not cache.get(cache_key): | ||
continue | ||
if purge: | ||
cache.delete(cache_key) | ||
_del_keys.append(url) | ||
misses_cache_key = '%s__misses' % url | ||
hits_cache_key = '%s__hits' % url | ||
misses = cache.get(misses_cache_key) | ||
hits = cache.get(hits_cache_key) | ||
if misses is None and hits is None: | ||
stats = None | ||
else: | ||
stats = { | ||
'hits': hits or 0, | ||
'misses': misses or 0 | ||
} | ||
yield (url, cache_key, stats) | ||
|
||
if _del_keys: | ||
# means something was changed | ||
for url in _del_keys: | ||
remembered_urls.pop(url) | ||
misses_cache_key = '%s__misses' % url | ||
hits_cache_key = '%s__hits' % url | ||
cache.delete(misses_cache_key) | ||
cache.delete(hits_cache_key) | ||
|
||
cache.set( | ||
REMEMBERED_URLS_KEY, | ||
remembered_urls, | ||
LONG_TIME | ||
) |
Oops, something went wrong.