def cached(cache_key=default_cache_key, cache_path=None):
    def cached(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            # Calculation of the cache key is delegated to a function
            # that's passed in via the decorator call
            # (`default_cache_key` by default).
            try:
                key = str(cache_key(*args, **kwargs)).encode('ascii')
            except DontCache:
                return func(*args, **kwargs)

            hashed_key = hashlib.sha1(key).hexdigest()[:8]

            # We construct the filename using the cache key.  If the
            # file exists, unpickle and return the value.
            filename = os.path.join(
                cache_path or CACHE_PATH,
                '{}.{}-cache-{}'.format(
                    func.__module__, func.__name__, hashed_key))

            if os.path.exists(filename):
                filesize = os.path.getsize(filename)
                size = "%0.1f MB" % (filesize / (1024 * 1024.0))
                logger.debug(" * cache hit: {} ({})".format(filename, size))
                return numpy_pickle.load(filename)
            else:
                logger.debug(" * cache miss: {}".format(filename))
                value = func(*args, **kwargs)
                tmp_filename = '{}-{}.tmp'.format(
                    filename,
                    ''.join(random.sample(string.ascii_letters, 4)),
                    )
                try:
                    numpy_pickle.dump(value, tmp_filename, compress=9)
                    os.rename(tmp_filename, filename)
                except Exception:
                    logger.exception(
                        "Saving pickle {} resulted in Exception".format(
                        filename))
                return value

        wrapper.uncached = func
        return wrapper
    return cached
dataset/ETHPy150Open dnouri/nolearn/nolearn/cache.py/cached/original