Skip to main content

A simple file-based cache module for Python

Project description

About

fcache is a simple file-based cache module for Python. It stores cache files in OS-appropriate, application-specific cache directories. Cached data can optionally expire after a certain amount of time. fcache can store any data supported by the pickle module. fcache uses the appdirs module’s “user_cache_dir” method to determine where to store cache files.

Installation

Install using pip:

pip install fcache

Usage

Setting and Getting Data

Let’s say you just fetched some weather data that you want to cache for the next hour:

>>> nyc_weather = {"temp": "77", "conditions": "cloudy"}

Let’s store the data using fcache:

>>> import fcache
>>> cache = fcache.Cache("weather", "fetchweather")
>>> cache.set("nyc", nyc_weather, 60 * 60)

We just created a new cache named “weather” for the “fetchweather” application. Then, we put the current weather into the cache under the key “nyc” for 1 hour. We can fetch the data for the next hour; after that, it expires.

30 minutes later, you already forgot what the weather is. Let’s retrieve the data:

>>> cache.get("nyc")
{'temp': '77', 'conditions': 'cloudy'}

Removing Data

Now that you’ve checked the weather, you’ve decided you don’t need to store New York’s weather data any longer:

>>> cache.remove("nyc")

NOTE: the “weather” cache still exists and can be used to save more data – only the “nyc” data was deleted.

Flushing a Cache

Perhaps you’ve spent all afternoon gathering weather data and storing it in your cache. But, now you want to flush all the data in anticipation for tomorrow’s data. In other words, you want to remove the data, but not remove the cache file itself:

>>> cache.flush()

Deleting a Cache

Let’s say you’re finished caching weather data altogether and want to delete your “weather” cache file:

>>> cache.delete()

License

fcache is released under the OSI-approved MIT License. See the file LICENSE.txt for more information.

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

fcache-0.1.tar.gz (5.1 kB view details)

Uploaded Source

File details

Details for the file fcache-0.1.tar.gz.

File metadata

  • Download URL: fcache-0.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for fcache-0.1.tar.gz
Algorithm Hash digest
SHA256 e77769c4ea15f6034ddc062706ba8eaebc0a2a97ddf2cdc5ef7a59a7b657f0b8
MD5 77afc1bfb222ce6aac24047e29485c33
BLAKE2b-256 3a8a5096c1ef421bee4ff1747c7c3029c6436b1f6ae83e0e9694352f7032d578

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