Beispiel #1
0
    def test_remove_expired_entries(self, datetime_mock, datetime_mock2):
        expire_after = timedelta(minutes=10)
        start_time = datetime.utcnow().replace(year=2010, minute=0)
        datetime_mock.utcnow.return_value = start_time
        datetime_mock2.utcnow.return_value = start_time

        s = CachedSession(CACHE_NAME, CACHE_BACKEND, expire_after=expire_after)
        s.get(httpbin('get'))
        s.get(httpbin('relative-redirect/3'))
        datetime_mock.utcnow.return_value = start_time + expire_after * 2
        datetime_mock2.utcnow.return_value = datetime_mock.utcnow.return_value

        ok_url = 'get?x=1'
        s.get(httpbin(ok_url))
        self.assertEqual(len(s.cache.responses), 3)
        self.assertEqual(len(s.cache.keys_map), 3)
        s.remove_expired_responses()
        self.assertEqual(len(s.cache.responses), 1)
        self.assertEqual(len(s.cache.keys_map), 0)
        self.assertIn(ok_url, list(s.cache.responses.values())[0][0].url)
Beispiel #2
0
    def test_remove_expired_entries(self, datetime_mock, datetime_mock2):
        expire_after = timedelta(minutes=10)
        start_time = datetime.utcnow().replace(year=2010, minute=0)
        datetime_mock.utcnow.return_value = start_time
        datetime_mock2.utcnow.return_value = start_time

        s = CachedSession(CACHE_NAME, CACHE_BACKEND, expire_after=expire_after)
        s.get(httpbin('get'))
        s.get(httpbin('relative-redirect/3'))
        datetime_mock.utcnow.return_value = start_time + expire_after * 2
        datetime_mock2.utcnow.return_value = datetime_mock.utcnow.return_value

        ok_url = 'get?x=1'
        s.get(httpbin(ok_url))
        self.assertEqual(len(s.cache.responses), 3)
        self.assertEqual(len(s.cache.keys_map), 3)
        s.remove_expired_responses()
        self.assertEqual(len(s.cache.responses), 1)
        self.assertEqual(len(s.cache.keys_map), 0)
        self.assertIn(ok_url, list(s.cache.responses.values())[0][0].url)
Beispiel #3
0
def ensure_requests_session(config):
    """Build the singleton requests.Session (or subclass)
    """
    session_attr = "check_links_requests_session"

    if not hasattr(config.option, session_attr):
        if config.option.check_links_cache:
            from requests_cache import CachedSession
            conf_kwargs = getattr(config.option, "check_links_cache_kwargs", {})
            kwargs = dict(default_cache)
            kwargs.update(conf_kwargs)
            requests_session = CachedSession(**kwargs)
            if kwargs.get("expire_after"):
                requests_session.remove_expired_responses()
        else:
            requests_session = Session()

        requests_session.headers['User-Agent'] = 'pytest-check-links'

        setattr(config.option, session_attr, requests_session)

    return getattr(config.option, session_attr)
Beispiel #4
0
def expire_cache(expire=-1):
    r"""
    Set cache expiration time

    This function can be used to modify the cache expiration time. This is
    useful when one wants to update only part of the cache. The procedure would
    be to set `expire` to 1 (second), call the :func:`~stplanpy.cycle.routes`
    function for the routes that need to be updated, and call
    :func:`~stplanpy.cycle.expire_cache` again to set expire back to -1 (never
    expires).
    
    Parameters
    ----------
    expire : int, defaults to -1
        Time after which the cache expires in seconds. Options are: -1 to never
        expire, 0 to disable caching, or a number. Days can be set using e.g.
        timedelta(days=180). Defaults to -1.

    See Also
    --------
    ~stplanpy.cycle.routes
    
    Examples
    --------
    .. code-block:: python

        import pandas as pd
        import geopandas as gpd
        from shapely import wkt
        from stplanpy import cycle

        # Define two origin-destination lines
        df = pd.DataFrame(
            {"geometry": [
            "LINESTRING(-11785727.431 4453819.337, -11782276.436 4448452.023)", 
            "LINESTRING(-11785787.392 4450797.573, -11787086.209 4449884.472)"]})

        # Convert to WTK
        df["geometry"] = gpd.GeoSeries.from_wkt(df["geometry"])

        # Create GeoDataFrame
        gdf = gpd.GeoDataFrame(df, geometry='geometry', crs="EPSG:6933")

        # Read the Cycle Streets API key
        cyclestreets_key = cycle.read_key()

        # Compute routes for the 1st time
        gdf["geometry"] = gdf.routes(api_key=cyclestreets_key)

        # Set cache expiration
        cycle.expire_cache(expire=1)

        # Update cache for second route
        gdf = gdf.copy().iloc[[1]]
        gdf["geometry"] = gdf.routes(api_key=cyclestreets_key)

        # Reset cache expiration
        cycle.expire_cache()
    """
# Initialize cache and change expiration time
    
    os.environ["SQLITE_TMPDIR"] = "."
    session = CachedSession("cyclestreets_cache")
    session.remove_expired_responses(expire_after=expire)