def set(self, query, result, expire=3600): """ Cache the given position for all networks present in the query. :param query: The query for which we got a result. :type query: :class:`ichnaea.api.locate.query.Query` :param result: The position result obtained for the query. :type result: :class:`~ichnaea.api.locate.fallback.ExternalResult` :param expire: Time in seconds to cache the result. :type expire: int """ if not self._should_cache(query): return cache_keys = self._cache_keys(query) if result.not_found(): cache_value = LOCATION_NOT_FOUND else: cache_value = result._asdict() try: cache_value = floatjson.float_dumps(cache_value) cache_values = dict([(key, cache_value) for key in cache_keys]) with self.redis_client.pipeline() as pipe: pipe.mset(cache_values) for cache_key in cache_keys: pipe.expire(cache_key, expire) pipe.execute() except (simplejson.JSONDecodeError, RedisError): self.raven_client.captureException()
def setUp(self): super(TestSource, self).setUp() self.api_key = ApiKeyFactory.build(allow_fallback=True) self.fallback_model = DummyModel(lat=51.5366, lon=0.03989, radius=1500.0) self.fallback_result = { 'location': { 'lat': self.fallback_model.lat, 'lng': self.fallback_model.lon, }, 'accuracy': float(self.fallback_model.radius), 'fallback': 'lacf', } self.fallback_cached_result = floatjson.float_dumps({ 'lat': self.fallback_model.lat, 'lon': self.fallback_model.lon, 'accuracy': float(self.fallback_model.radius), 'fallback': 'lacf', })
def set(self, query, result): """ Cache the given position for all networks present in the query. :param query: The query for which we got a result. :type query: :class:`ichnaea.api.locate.query.Query` :param result: The position result obtained for the query. :type result: :class:`~ichnaea.api.locate.fallback.ExternalResult` """ if not self._should_cache(query): return cache_keys = self._cache_keys(query) if result.not_found(): cache_value = LOCATION_NOT_FOUND else: cache_value = result._asdict() try: cache_value = floatjson.float_dumps(cache_value) cache_values = dict([(key, cache_value) for key in cache_keys]) with self.redis_client.pipeline() as pipe: pipe.mset(cache_values) for cache_key in cache_keys: pipe.expire(cache_key, self.cache_expire) pipe.execute() except (simplejson.JSONDecodeError, RedisError): self.raven_client.captureException()
def setUp(self): super(TestSource, self).setUp() self.api_key = ApiKeyFactory.build(allow_fallback=True) self.fallback_model = DummyModel( lat=51.5366, lon=0.03989, radius=1500.0) self.fallback_result = { 'location': { 'lat': self.fallback_model.lat, 'lng': self.fallback_model.lon, }, 'accuracy': float(self.fallback_model.radius), 'fallback': 'lacf', } self.fallback_cached_result = floatjson.float_dumps({ 'lat': self.fallback_model.lat, 'lon': self.fallback_model.lon, 'accuracy': float(self.fallback_model.radius), 'fallback': 'lacf', })
def test_error(self): with pytest.raises(TypeError): float_dumps(object())