Skip to main content

A collection of caching utilities.

Project description

A hierarchy of classes to provide caching functionality with minimal fuss.

When using the cache, just use the fetch method. This will pull from the cache if it’s present and not expired otherwise will use ‘fetch_from_source’.

Cache

Abstract class. Not expected to be used. Provides the methods needed by subclasses provided by the package.

Methods that need to be implemented in the concrete class:

key fetch_from_source cache fetch_from_cache_by_key delete_by_key expiry_time

Optional:

cook

BaseMemoryClass

Abstract class.

Methods that must be implemented in concrete class:

key fetch_from_source calculate_expiry

Optional:

cook

SimpleMemoryClass

Abstract class.

Methods that must be implemented in concrete class:

key fetch_from_source

Optinal:

cook

BaseHttpMemoryCache

Abstract class.

Caches requests objects or objects generated from requests.

TODO: Improve expiry calculations. Currently only uses max age.

Methods that must be implemented in concrete class:

key fetch_from_source get_request_for_item (if caching objects generated from request objects)

Optinal:

cook

HttpMemoryCache

Concrete class.

Uses the url string as the key. Uses requests.get as fetch_from_source.

Use: Instantiate and use instance.fetch(url) to fetch from the cache or source.

BinaryFileCache

Abstract class.

Reads from and writes to binary files. Suitable for caching images.

Methods that must be implemented in concrete class:

key fetch_from_source

Optional:

cook

FileCache

Abstract class.

Reads from and writes to files with encoding.

Methods that must be implemented in concrete class:

key fetch_from_source

Optional:

cook encode decode

JsonFileCache

Abstract class.

This is just FileCache with JSON encoding and decoding.

fetch_from_source should return a JSON string. If OBJECT_PAIRS_HOOK is set, then decode will use that. Typically use: collections.OrderedDict to retain the order of elements.

Methods that must be implemented in concrete class:

key fetch_from_source

Optional:

cook OBJECT_PAIRS_HOOK

TwoLevelCache

Concrete class.

Generally this means an in-memory cache joined to a persistent cache (e.g, file, database). That means your cache persists over many executions, but doesn’t need to access the slower persistent cache more than once per item fetched.

To use it, implement the two caches and instantiate TwoLevelCache to join them. Pass in-memory cache class as the transient_cache and the persistent cache class as (surprise!) the pesistent_cache.

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

cachingutil-1.2.0.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

cachingutil-1.2.0-py2.py3-none-any.whl (19.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file cachingutil-1.2.0.tar.gz.

File metadata

  • Download URL: cachingutil-1.2.0.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for cachingutil-1.2.0.tar.gz
Algorithm Hash digest
SHA256 d8d03b02f40fdcce7b5c5e6d7ef37999b6dabb811377d9e435f146b4254b16c2
MD5 97d60145bff7bba0f83c702ab87aef68
BLAKE2b-256 3c97b48ccedc041a461319cc8d1824e75622eb926c587f1de3617e6648f49317

See more details on using hashes here.

File details

Details for the file cachingutil-1.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for cachingutil-1.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7d9f0a7bd884855b6965fc19e0f5126190b33cfe5fc1938e2157af2bbd0f0f5d
MD5 4d18586278f5006ca7291d9f8cbc1610
BLAKE2b-256 59c5803b3d4e71e0ad3a2947061d65f3a7398d01f7ceaf4aa655719385aae200

See more details on using hashes here.

Supported by

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