示例#1
0
    def accessed(self, identifier, type='IP'):
        """
        Handles recording the user's access.

        Stores the current timestamp in the "accesses" list within the cache.
        """
        start_usage = (None, self.hourly_rate_limit, self.daily_rate_limit) if type == 'IP' else \
                                                        (None, self.key_hourly_rate_limit, self.key_daily_rate_limit)
        _, hourly_rate_limit, daily_rate_limit = start_usage
        usage = cache.get(identifier, start_usage)
        now = datetime.utcnow().replace(tzinfo=pytz.utc)
        last_usage, old_hourly_rate_limit, old_daily_rate_limit = usage
        if not last_usage:
            cache.set(identifier,
                      (now, max(old_hourly_rate_limit - 1,
                                0), max(old_daily_rate_limit - 1, 0)))
        else:
            if last_usage + timedelta(
                    hours=1) >= now and now.hour == last_usage.hour:
                new_hourly_rate_limit = max(old_hourly_rate_limit - 1, 0)
            else:
                new_hourly_rate_limit = hourly_rate_limit
            if last_usage + timedelta(
                    days=1) >= now and now.day == last_usage.day:
                new_daily_rate_limit = max(old_daily_rate_limit - 1, 0)
            else:
                new_daily_rate_limit = daily_rate_limit
            cache.set(identifier,
                      (now, new_hourly_rate_limit, new_daily_rate_limit))
示例#2
0
 def _caching(*args, **kwargs):
     start = time.time()
     if not cache:
         from_cache = False
         res = fn(*args, **kwargs)
     else:
         key = json.dumps(args) + json.dumps(
             kwargs
         ) + request.method + request.path + request.body.read()
         request.body.seek(0)
         cached_content, content_type = cache.get(key, (None, None))
         if cached_content:
             if content_type:
                 response.headers['Content-Type'] = content_type
             from_cache = True
             res = cached_content
         else:
             from_cache = False
             res = fn(*args, **kwargs)
             if type(res) == str:
                 content_type = response.headers.get('Content-Type')
                 cache.set(key, (res, content_type))
     if config.get('debug', True):
         end = time.time()
         response.headers['X-ChEMBL-in-cache'] = from_cache
         response.headers['X-ChEMBL-retrieval-time'] = end - start
     return res
示例#3
0
    def accessed(self, identifier, type='IP'):
        """
        Handles recording the user's access.

        Stores the current timestamp in the "accesses" list within the cache.
        """
        start_usage = (None, self.hourly_rate_limit, self.daily_rate_limit) if type == 'IP' else \
                                                        (None, self.key_hourly_rate_limit, self.key_daily_rate_limit)
        _, hourly_rate_limit, daily_rate_limit = start_usage
        try:
            usage = cache.get(identifier, start_usage)
        except:
            usage = start_usage
        now = datetime.utcnow().replace(tzinfo=pytz.utc)
        last_usage, old_hourly_rate_limit, old_daily_rate_limit = usage
        if not last_usage:
            try:
                cache.set(identifier, (now, max(old_hourly_rate_limit-1, 0), max(old_daily_rate_limit-1, 0)))
            except:
                pass
        else:
            if last_usage + timedelta(hours=1) >= now and now.hour == last_usage.hour:
                new_hourly_rate_limit = max(old_hourly_rate_limit-1, 0)
            else:
                new_hourly_rate_limit = hourly_rate_limit
            if last_usage + timedelta(days=1) >= now and now.day == last_usage.day:
                new_daily_rate_limit = max(old_daily_rate_limit-1, 0)
            else:
                new_daily_rate_limit = daily_rate_limit
            try:    
                cache.set(identifier, (now, new_hourly_rate_limit, new_daily_rate_limit))
            except:
                pass
示例#4
0
 def _caching(*args, **kwargs):
     start = time.time()
     if not cache:
         from_cache = False
         res = fn(*args, **kwargs)
     else:
         key = json.dumps(args) + json.dumps(kwargs) + json.dumps([(base64.b64encode(k), base64.b64encode(v))
                 for k,v in request.params.items()]) + request.method + request.path + request.body.read()
         request.body.seek(0)
         try:
             cached_content, content_type = cache.get(key, (None, None))
         except:
             cached_content, content_type = (None, None)
         if cached_content:
             if content_type:
                 response.headers['Content-Type'] = content_type
             from_cache = True
             res = cached_content
         else:
             from_cache = False
             res = fn(*args, **kwargs)
             if type(res) == str:
                 content_type = response.headers.get('Content-Type')
                 try:
                     cache.set(key, (res, content_type))
                 except:
                     pass
     if config.get('debug', True):
         end = time.time()
         response.headers['X-ChEMBL-in-cache'] = from_cache
         response.headers['X-ChEMBL-retrieval-time'] = end - start
     return res
示例#5
0
 def _caching(*args, **kwargs):
     start = time.time()
     if not cache:
         from_cache = False
         res = fn(*args, **kwargs)
     else:
         key = json.dumps(args) + json.dumps(kwargs) + request.method + request.path + request.body.read()
         request.body.seek(0)
         cached_content, content_type = cache.get(key, (None, None))
         if cached_content:
             if content_type:
                 response.headers['Content-Type'] = content_type
             from_cache = True
             res = cached_content
         else:
             from_cache = False
             res = fn(*args, **kwargs)
             if type(res) == str:
                 content_type = response.headers.get('Content-Type')
                 cache.set(key, (res, content_type))
     if config.get('debug', True):
         end = time.time()
         response.headers['X-ChEMBL-in-cache'] = from_cache
         response.headers['X-ChEMBL-retrieval-time'] = end - start
     return res
示例#6
0
 def _caching(*args, **kwargs):
     start = time.time()
     if not cache:
         from_cache = False
         res = fn(*args, **kwargs)
     else:
         key = json.dumps(args) + json.dumps(kwargs) + json.dumps([
             (base64.b64encode(k), base64.b64encode(v))
             for k, v in request.params.items()
         ]) + request.method + request.path + request.body.read()
         request.body.seek(0)
         try:
             cached_content, content_type = cache.get(key, (None, None))
         except:
             cached_content, content_type = (None, None)
         if cached_content:
             if content_type:
                 response.headers['Content-Type'] = content_type
             from_cache = True
             res = cached_content
         else:
             from_cache = False
             res = fn(*args, **kwargs)
             if type(res) == str:
                 content_type = response.headers.get('Content-Type')
                 try:
                     cache.set(key, (res, content_type))
                 except:
                     pass
     if config.get('debug', True):
         end = time.time()
         response.headers['X-ChEMBL-in-cache'] = from_cache
         response.headers['X-ChEMBL-retrieval-time'] = end - start
     return res