コード例 #1
0
 def iterator(self):
     superiter = super(CachingQuerySet, self).iterator()
     while True:
         obj = superiter.next()
         # Use cache.add instead of cache.set to prevent race conditions (see CachingManager)
         cache.add(obj.cache_key, obj, CACHE_DURATION)
         yield obj
コード例 #2
0
def get_response(state_input):
    # Check if park_list is in cache; otherwise, make a request on the API
    # Indentifier for National Park API is <state-input>
    cached_park_list = cache.fetch(state_input, cache_list.DataList)
    if cached_park_list:
        log.info('National Park API - Return from Cache')
        return cached_park_list
    else:
        log.info('National Park API - return from API call')

        try:
            query = {'stateCode': state_input, 'api_key': NTL_PARK_KEY}
            response = requests.get(API_URL, params=query)
            response.raise_for_status(
            )  # will raise an exception for 400(client) or 500(server) errors
            data = response.json()
            park_list = get_info(data)
            #send to the cache the park list(data), identifier(state_input) and expiry
            natlParks_data_list_for_cache = cache_list.DataList(
                park_list, state_input, now_plus_expiry())

            cache.add(natlParks_data_list_for_cache)
            return park_list
        except requests.exceptions.HTTPError as e:
            log.exception(e)
            raise e
        except Exception as ex:
            log.exception(ex)
            raise ex
コード例 #3
0
ファイル: managers.py プロジェクト: boar/boar
 def iterator(self):
     superiter = super(CachingQuerySet, self).iterator()
     while True:
         obj = superiter.next()
         # Use cache.add instead of cache.set to prevent race conditions (see CachingManager)
         cache.add(obj.cache_key, obj, CACHE_DURATION)
         yield obj
コード例 #4
0
ファイル: fields.py プロジェクト: anarcher/django-caching
 def all(self):
     key = instance._get_cache_key(field=field_name)
     qs = super(CachingRelatedManager, self).get_query_set()
     PKListQuerySet = get_pk_list_query_set(qs.__class__)
     qs = qs._clone(klass=PKListQuerySet)
     pk_list = cache.get(key)
     if pk_list is None:
         pk_list = list(qs.values_list('pk', flat=True))
         cache.add(key, pk_list, CACHE_DURATION)
     else:
         qs.from_cache = True
     qs.pk_list = pk_list
     return qs
コード例 #5
0
 def all(self):
     key = instance._get_cache_key(field=field_name)
     qs = super(CachingRelatedManager, self).get_query_set()
     PKListQuerySet = get_pk_list_query_set(qs.__class__)
     qs = qs._clone(klass=PKListQuerySet)
     pk_list = cache.get(key)
     if pk_list is None:
         pk_list = qs.values_list('pk', flat=True)
         cache.add(key, pk_list, CACHE_DURATION)
     else:
         qs.from_cache = True
     qs.pk_list = pk_list
     return qs
コード例 #6
0
def get_trails(lat, lon):
    # Check if trail_list is in cache; otherwise, make a request on the API
    # Indentifier for Hiking API is <lat><long>
    latLon = f'{lat}+{lon}'
    cached_trail_list = cache.fetch((latLon), cache_list.DataList)

    # When there is an exist cached available
    if cached_trail_list:
        log.info('Hiking API - Return from Cache')
        return cached_trail_list
    else:
        log.info('Hiking API - New API call')

        try:
            query = {'lat': lat, 'lon': lon, 'key': HIKING_KEY}
            response = requests.get(HIKING_URL, params=query)
            response.raise_for_status(
            )  # will raise an exception for 400(client) or 500(server) errors
            data = response.json()
            trail_list = list()
            list_of_trails = data['trails']

            if list_of_trails:
                for trail in list_of_trails:
                    trail_list_w_info = dict()
                    if trail['name'] and trail['length'] and trail[
                            'difficulty'] and trail['summary']:
                        trail_list_w_info['name'] = trail['name']
                        trail_list_w_info['length'] = trail['length']
                        if trail['summary'] == 'Needs Summary':
                            trail_list_w_info['summary'] = 'N/A'
                        else:
                            trail_list_w_info['summary'] = trail['summary']

                        trail_list.append(trail_list_w_info)

            latLon = f'{lat}+{lon}'
            hiking_trails_data_list_for_cache = cache_list.DataList(
                trail_list, latLon, now_plus_expiry())
            cache.add(hiking_trails_data_list_for_cache)

            return trail_list
        except requests.exceptions.HTTPError as e:
            log.exception(e)
            raise e
        except Exception as e:
            log.exception(e)
            raise e
コード例 #7
0
def get_weather(lat, lon):
    # Check if weather_list is in cache; otherwise, make a request on the API
    # Indentifier for Weather API is <lat><lon>_weather
    latLon = f'{lat}+{lon}_weather'
    cached_weather_list = cache.fetch((latLon), cache_list.DataList)
    if cached_weather_list:
        log.info('Weather API - Return from Cache')
        return cached_weather_list
    else:
        log.info('Weather API - new API Call')
        try:
            params = {
                'lat': lat,
                'lon': lon,
                'exclude': 'current,alerts,hourly,minutely',
                'units': 'imperial',
                'appid': WEATHER_KEY
            }
            response = requests.get(API_URL, params=params)
            response.raise_for_status()
            data = response.json()

            weather_list = store_data(data)
            # cache new weather_list for 1 day
            latLon = f'{lat}+{lon}_weather'
            weather_data_list_for_cache = cache_list.DataList(
                weather_list, (latLon), now_plus_expiry())
            cache.add(weather_data_list_for_cache)
            return weather_list

        except requests.exceptions.HTTPError as e:
            log.exception(e)
            raise e
        except Exception as e:
            log.exception(f'Error occurred. More detail: {e}')
            log.exception(f'Error Message from request: {response.text}')
            raise e