Skip to content

Commit

Permalink
introducing LocalCacheProxy to skip pickling stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
codingcatgirl committed Dec 27, 2018
1 parent 957ce0c commit 3eeda53
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 19 deletions.
14 changes: 10 additions & 4 deletions src/c3nav/mapdata/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@
from c3nav.mapdata.models.level import Level
from c3nav.mapdata.models.locations import (Location, LocationGroupCategory, LocationRedirect, LocationSlug,
SpecificLocation)
from c3nav.mapdata.utils.cache.local import LocalCacheProxy
from c3nav.mapdata.utils.cache.stats import increment_cache_key
from c3nav.mapdata.utils.locations import (get_location_by_id_for_request, get_location_by_slug_for_request,
searchable_locations_for_request, visible_locations_for_request)
from c3nav.mapdata.utils.models import get_submodels
from c3nav.mapdata.utils.user import can_access_editor, get_user_data
from c3nav.mapdata.views import set_tile_access_cookie

request_cache = LocalCacheProxy(maxsize=64)


def optimize_query(qs):
if issubclass(qs.model, SpecificLocation):
Expand Down Expand Up @@ -85,15 +88,18 @@ def wrapped_func(self, request, *args, **kwargs):
for param, type_ in cache_parameters.items():
value = int(param in request.GET) if type_ == bool else type_(request.GET.get(param))
cache_key += ':'+urlsafe_base64_encode(str(value).encode()).decode()
data = cache.get(cache_key)
if data is not None:
response = Response(data)
print(cache_key)
data = request_cache.get(cache_key)
print(data)
if data is not None:
print('HA CACHE')
response = Response(data)

if response is None:
with GeometryMixin.dont_keep_originals():
response = func(self, request, *args, **kwargs)
if cache_parameters is not None and response.status_code == 200:
cache.set(cache_key, response.data, 900)
request_cache.set(cache_key, response.data, 900)

if response.status_code == 200:
response['ETag'] = etag
Expand Down
42 changes: 42 additions & 0 deletions src/c3nav/mapdata/utils/cache/local.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from collections import OrderedDict

from django.core.cache import cache


class NoneFromCache:
pass


class LocalCacheProxy:
# django cache, buffered using a LRU cache
# only usable for stuff that never changes, obviously
def __init__(self, maxsize=128):
self._maxsize = maxsize
self._items = OrderedDict()

def get(self, key, default=None):
print('get')
try:
# first check out cache
result = self._items[key]
except KeyError:
# not in our cache
result = cache.get(key, default=NoneFromCache)
if result is not NoneFromCache:
self._items[key] = result
self._prune()
else:
result = default
else:
self._items.move_to_end(key, last=True)
return result

def _prune(self):
# remove old items
while len(self._items) > self._maxsize:
self._items.pop(next(iter(self._items.keys())))

def set(self, key, value, expire):
cache.set(key, value, expire)
self._items[key] = value
self._prune()
32 changes: 17 additions & 15 deletions src/c3nav/mapdata/utils/locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from typing import List, Mapping, Optional

from django.apps import apps
from django.core.cache import cache
from django.db.models import Prefetch, Q
from django.utils.functional import cached_property
from django.utils.translation import ugettext_lazy as _
Expand All @@ -20,12 +19,15 @@
from c3nav.mapdata.models.geometry.level import LevelGeometryMixin, Space
from c3nav.mapdata.models.geometry.space import SpaceGeometryMixin
from c3nav.mapdata.models.locations import LocationRedirect, LocationSlug, SpecificLocation
from c3nav.mapdata.utils.cache.local import LocalCacheProxy
from c3nav.mapdata.utils.models import get_submodels

proxied_cache = LocalCacheProxy(maxsize=128)


def locations_for_request(request) -> Mapping[int, LocationSlug]:
cache_key = 'mapdata:locations:%s' % AccessPermission.cache_key_for_request(request)
locations = cache.get(cache_key, None)
locations = proxied_cache.get(cache_key, None)
if locations is not None:
return locations

Expand Down Expand Up @@ -110,15 +112,15 @@ def locations_for_request(request) -> Mapping[int, LocationSlug]:
# noinspection PyStatementEffect
obj.point

cache.set(cache_key, locations, 1800)
proxied_cache.set(cache_key, locations, 1800)

return locations


def get_better_space_geometries():
# change space geometries for better representative points
cache_key = 'mapdata:better_space_geometries:%s' % MapUpdate.current_cache_key()
result = cache.get(cache_key, None)
result = proxied_cache.get(cache_key, None)
if result is not None:
return result

Expand All @@ -130,28 +132,28 @@ def get_better_space_geometries():
if not geometry.is_empty:
result[space.pk] = geometry

cache.set(cache_key, result, 1800)
proxied_cache.set(cache_key, result, 1800)

return result


def visible_locations_for_request(request) -> Mapping[int, Location]:
cache_key = 'mapdata:locations:real:%s' % AccessPermission.cache_key_for_request(request)
locations = cache.get(cache_key, None)
locations = proxied_cache.get(cache_key, None)
if locations is not None:
return locations

locations = {pk: location for pk, location in locations_for_request(request).items()
if not isinstance(location, LocationRedirect) and (location.can_search or location.can_describe)}

cache.set(cache_key, locations, 1800)
proxied_cache.set(cache_key, locations, 1800)

return locations


def searchable_locations_for_request(request) -> List[Location]:
cache_key = 'mapdata:locations:searchable:%s' % AccessPermission.cache_key_for_request(request)
locations = cache.get(cache_key, None)
locations = proxied_cache.get(cache_key, None)
if locations is not None:
return locations

Expand All @@ -160,27 +162,27 @@ def searchable_locations_for_request(request) -> List[Location]:

locations = sorted(locations, key=operator.attrgetter('order'), reverse=True)

cache.set(cache_key, locations, 1800)
proxied_cache.set(cache_key, locations, 1800)

return locations


def locations_by_slug_for_request(request) -> Mapping[str, LocationSlug]:
cache_key = 'mapdata:locations:by_slug:%s' % AccessPermission.cache_key_for_request(request)
locations = cache.get(cache_key, None)
locations = proxied_cache.get(cache_key, None)
if locations is not None:
return locations

locations = {location.slug: location for location in locations_for_request(request).values() if location.slug}

cache.set(cache_key, locations, 1800)
proxied_cache.set(cache_key, locations, 1800)

return locations


def levels_by_short_label_for_request(request) -> Mapping[str, Level]:
cache_key = 'mapdata:levels:by_short_label:%s' % AccessPermission.cache_key_for_request(request)
levels = cache.get(cache_key, None)
levels = proxied_cache.get(cache_key, None)
if levels is not None:
return levels

Expand All @@ -189,7 +191,7 @@ def levels_by_short_label_for_request(request) -> Mapping[str, Level]:
for level in Level.qs_for_request(request).filter(on_top_of_id__isnull=True).order_by('base_altitude')
)

cache.set(cache_key, levels, 1800)
proxied_cache.set(cache_key, levels, 1800)

return levels

Expand All @@ -205,7 +207,7 @@ def get_location_by_id_for_request(pk, request):

def get_location_by_slug_for_request(slug: str, request) -> Optional[LocationSlug]:
cache_key = 'mapdata:location:by_slug:%s:%s' % (AccessPermission.cache_key_for_request(request), slug)
location = cache.get(cache_key, None)
location = proxied_cache.get(cache_key, None)
if location is not None:
return location

Expand All @@ -230,7 +232,7 @@ def get_location_by_slug_for_request(slug: str, request) -> Optional[LocationSlu
else:
location = locations_by_slug_for_request(request).get(slug, None)

cache.set(cache_key, location, 1800)
proxied_cache.set(cache_key, location, 1800)

return location

Expand Down

0 comments on commit 3eeda53

Please sign in to comment.