Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Cache routes on get_route.
Browse files Browse the repository at this point in the history
  • Loading branch information
shazow committed Oct 31, 2011
1 parent ce8ece5 commit eebdc16
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions composer/index.py
Expand Up @@ -85,6 +85,8 @@ def __init__(self, base_path='', base_url='/'):
self._register_default_filters()
self._register_filters()

self._route_cache = None

def _register_default_filters(self):
for filter_id, filter_cls in default_filters.iteritems():
try:
Expand Down Expand Up @@ -186,13 +188,16 @@ def _generate_static(self):
"""
pass

def _refresh_route_cache(self):
self._route_cache = {}
for route in self.routes:
self._route_cache[route.url.lstrip('/')] = route

def get_route(self, url):
url = url.lstrip('/')
if self._route_cache is None:
self._refresh_route_cache()

# TODO: Should we pre-index this?
for route in self.routes:
if route.url.lstrip('/') == url:
return route
return self._route_cache.get(url.lstrip('/'))

@property
def routes(self):
Expand Down

0 comments on commit eebdc16

Please sign in to comment.