Skip to content

Commit

Permalink
Cache the remaining pages that should be cached
Browse files Browse the repository at this point in the history
Add caching to all the pages that formerly had been cached in
spline-pokedex.

Also drop the template argument from cache_content, since unlike spline
it isn't responsible for rendering the template. We still need something
to use as the cache key, so use the do_work function name instead, same
as beaker does for decorated functions.

Updates veekun#128
  • Loading branch information
magical committed Jul 16, 2019
1 parent 6404c80 commit 4ed803e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 43 deletions.
13 changes: 11 additions & 2 deletions splinext/pokedex/views/abilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from .. import db
from .. import helpers
from . import viewlib

def ability_list(request):
c = request.tmpl_context
Expand Down Expand Up @@ -46,6 +47,16 @@ def ability_view(request):
filters=[t.Ability.is_main_series],
)

viewlib.cache_content(
request=request,
key=c.ability.identifier,
do_work=_do_ability,
)

return {}

def _do_ability(request, cache_key):
c = request.tmpl_context

# Eagerload
db.pokedex_session.query(t.Ability) \
Expand Down Expand Up @@ -103,5 +114,3 @@ def ability_view(request):
subqueryload('damage_class')
) \
.all()

return {}
18 changes: 11 additions & 7 deletions splinext/pokedex/views/moves.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from .. import db
from .. import helpers
from . import viewlib

# XXX(pyramid): move these to a shared module
from .pokemon import _move_tutor_version_groups, _collapse_pokemon_move_columns, _pokemon_move_method_sort_key
Expand Down Expand Up @@ -59,11 +60,16 @@ def move_view(request):
)

# XXX(pyramid)
#return self.cache_content(
# key=c.move.identifier,
# template='/pokedex/move.mako',
# do_work=self._do_moves,
#)
viewlib.cache_content(
request=request,
key=c.move.identifier,
do_work=_do_move,
)

return {}

def _do_move(request, cache_key):
c = request.tmpl_context

# Eagerload
db.pokedex_session.query(t.Move) \
Expand Down Expand Up @@ -251,5 +257,3 @@ def move_view(request):
.subquery()
)
).value(func.count(t.Pokemon.id))

return {}
44 changes: 20 additions & 24 deletions splinext/pokedex/views/pokemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,10 @@ def pokemon_view(request):
# Some Javascript
c.javascripts.append(('pokedex', 'pokemon'))

### TODO: Let's cache this bitch
#return self.cache_content(
# key=c.pokemon.identifier,
# template='/pokedex/pokemon.mako',
# do_work=self._do_pokemon,
#)

### Let's cache this bitch
viewlib.cache_content(
request=request,
key=c.pokemon.identifier,
template='/pokedex/pokemon.mako',
do_work=_do_pokemon,
)

Expand Down Expand Up @@ -836,13 +829,16 @@ def pokemon_flavor_view(request):
# Some Javascript
c.javascripts.append(('pokedex', 'pokemon'))

# XXX(pyramid) cache me
#return self.cache_content(
# key=c.form.identifier,
# template='/pokedex/pokemon_flavor.mako',
# do_work=self._do_pokemon_flavor,
#)
viewlib.cache_content(
request=request,
key=c.form.identifier,
do_work=_do_pokemon_flavor,
)

return {}

def _do_pokemon_flavor(request, cache_key):
c = request.tmpl_context
c.sprites = {}

config = request.registry.settings
Expand Down Expand Up @@ -880,8 +876,6 @@ def sprite_exists(directory):
weights = {'pokemon': c.pokemon.weight, 'trainer': c.trainer_weight}
c.weights = pokedex_helpers.scale_sizes(weights, dimensions=2)

return {}


def pokemon_locations_view(request):
"""Spits out a page listing detailed location information for this
Expand All @@ -899,12 +893,16 @@ def pokemon_locations_view(request):
c.prev_species, c.next_species = _prev_next_species(c.pokemon.species)

# Cache it yo
# XXX(pyramid)
#return self.cache_content(
# key=c.pokemon.identifier,
# template='/pokedex/pokemon_locations.mako',
# do_work=self._do_pokemon_locations,
#)
viewlib.cache_content(
request=request,
key=c.pokemon.identifier,
do_work=_do_pokemon_locations,
)

return {}

def _do_pokemon_locations(request, cache_key):
c = request.tmpl_context

# For the most part, our data represents exactly what we're going to
# show. For a given area in a given game, this Pokémon is guaranteed
Expand Down Expand Up @@ -1005,8 +1003,6 @@ def pokemon_locations_view(request):
continue
c.region_versions[region][0:0] = version_group.versions

return {}


def parse_size_view(request):
u"""Parses a height or weight and returns a bare number in Pokémon
Expand Down
21 changes: 15 additions & 6 deletions splinext/pokedex/views/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from .. import db
from .. import helpers
from . import viewlib

def type_list(request):
c = request.tmpl_context
Expand Down Expand Up @@ -52,9 +53,9 @@ def type_list(request):
400: +2,
200: +1,
100: 0,
50: -1,
25: -2,
0: -2,
50: -1,
25: -2,
0: -2,
}
if c.secondary_type:
attacking_score_conversion[0] = -4
Expand Down Expand Up @@ -88,7 +89,17 @@ def type_view(request):
language=c.game_language,
)

### XXX(pyramid) cache me
### Cache
viewlib.cache_content(
request=request,
key=c.type.identifier,
do_work=_do_type,
)

return {}

def _do_type(request, cache_key):
c = request.tmpl_context

# Eagerload a bit of type stuff
db.pokedex_session.query(t.Type) \
Expand Down Expand Up @@ -116,5 +127,3 @@ def type_view(request):
joinedload('pokemon.stats'),
) \
.one()

return {}
12 changes: 8 additions & 4 deletions splinext/pokedex/views/viewlib.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

import zlib

from beaker.util import func_namespace
from mako.runtime import capture

### Caching

def cache_content(request, key, do_work, template):
def cache_content(request, key, do_work):
"""Argh!
Okay, so. Use this when you want to cache the BODY of a page but not
Expand All @@ -22,8 +24,8 @@ def cache_content(request, key, do_work, template):
will only be called if the page hasn't yet been cached. It'll be
passed the key.
``template``
Name of the template to use.
The name and module of this function will be used as part of the cache
key.
Also, DO NOT FORGET TO wrap the cachable part of your template in a
<%lib:cache_content> tag, or nothing will get cached!
Expand All @@ -39,8 +41,10 @@ def cache_content(request, key, do_work, template):
# TODO(pyramid)
#key = u"{0}/{1}".format(key, c.lang)

namespace = func_namespace(do_work)
# Cache for... ten hours? Sure, whatever
content_cache = cache.get_cache('content_cache:' + template,
# TODO: use get_cache_region instead
content_cache = cache.get_cache('content_cache:' + namespace,
expiretime=36000)

# XXX This is dumb. Caches don't actually respect the 'enabled'
Expand Down

0 comments on commit 4ed803e

Please sign in to comment.