Skip to main content

A basic but fast, persistent and threadsafe caching system

Project description

# A basic but fast, persistent and threadsafe caching system

**Basic commands**

Importing required modules first:

from webscrapetools import urlcaching

Initializing the cache:

urlcaching.set_cache_path('.wst_cache')

The option _expiry_days_ sets the cache expiry period, default is 10 days.

This is a required step: otherwise responses to url calls will simply not be cached.
Cache data are stored in the specified folder, so that re-using the same string makes the cache persistent. This creates
the folder on the fly if it does not exist.
The following command cleans up the cache, making sure we start with no prior data:

urlcaching.empty_cache()

Opening an url with the following command stores the repsonse content behind the scene, so that subsequent calls will
not hit the network.

urlcaching.open_url('http://www.google.com')


**Full example**

from webscrapetools import urlcaching
import time

# Initializing the cache
urlcaching.set_cache_path('.wst_cache')

# Making sure we start from scratch
urlcaching.empty_cache()

# Demo with 5 identical calls... only the first one is delayed, all others are hitting the cache
count_calls = 1
while count_calls <= 5:
start_time = time.time()
urlcaching.open_url('http://deelay.me/5000/http://www.google.com')
duration = time.time() - start_time
print('duration for call {}: {:0.2f}'.format(count_calls, duration))
count_calls += 1

# Cleaning up
urlcaching.empty_cache()

The code above outputs the following:

duration for call 1: 6.74
duration for call 2: 0.00
duration for call 3: 0.00
duration for call 4: 0.00
duration for call 5: 0.00

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

webscrapetools-0.4.1.tar.gz (9.9 kB view details)

Uploaded Source

File details

Details for the file webscrapetools-0.4.1.tar.gz.

File metadata

  • Download URL: webscrapetools-0.4.1.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.2

File hashes

Hashes for webscrapetools-0.4.1.tar.gz
Algorithm Hash digest
SHA256 ee2fe3a4bd77cbd509023cc41dcdb50b61196f2c7053a19f8b309aa28f2771a7
MD5 70c8eb9a2780f545ff384791f0e70018
BLAKE2b-256 0be4362845e97e0006d3fec9994ac5c54befc9f0119e37c444ddf7941b76e060

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page