Skip to main content

A Django cache backend using AWS S3

Project description

Django AWS S3 Cache Backend

A Django cache backend implementation that uses Amazon AWS S3 as the storage backend. This backend stores cache items as JSON in an S3 bucket, with each item including an expiry timestamp. Items are retrieved from the cache only if they haven't expired. Expired items remain in the bucket and are not automatically deleted by this backend.

Installation

Install the package via pip:

pip install django-aws-s3-cache

Configuration

In your Django settings, define the CACHES and point to the S3 backend class. It can be used as the only cache or alongside other cache backends.

Using S3 as the Default Cache

CACHES = {
    "default": {
        "BACKEND": "django_aws_s3_cache.S3CacheBackend",
        "LOCATION": "S3_CACHE_BUCKET_NAME",
        "OPTIONS": {
            "BUCKET_NAME": "your-s3-bucket-name",
        }
    }
}

Using S3 Alongside Other Caches

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": REDIS_URL,
    },
    "s3": {
        "BACKEND": "django-aws-s3-cache.S3CacheBackend",
        "LOCATION": S3_CACHE_BUCKET_NAME,
    }
}

Example Usage

Here's how to use the S3 cache in your Django application:

from django.core.cache import caches

# Access the S3 cache
s3_cache = caches["s3"]

# Set a value in the cache
s3_cache.set("my_key", "my_value", timeout=60*15)  # 15 minutes

# Get a value from the cache
value = s3_cache.get("my_key")

# Delete a value from the cache
s3_cache.delete("my_key")

# Clear the entire cache
s3_cache.clear()

Using the Default Cache

If you have configured the S3 cache as the default cache, you can use it directly from the cache module:

from django.core.cache import cache

# Set a value in the cache
cache.set("my_key", "my_value", timeout=60*15)  # 15 minutes

# Get a value from the cache
value = cache.get("my_key")

# Delete a value from the cache
cache.delete("my_key")

# Clear the entire cache
cache.clear()

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

django-aws-s3-cache-0.1.3.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_aws_s3_cache-0.1.3-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file django-aws-s3-cache-0.1.3.tar.gz.

File metadata

  • Download URL: django-aws-s3-cache-0.1.3.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.6

File hashes

Hashes for django-aws-s3-cache-0.1.3.tar.gz
Algorithm Hash digest
SHA256 e5f88cc57eb59fb10cbb79e6efd6f7133b401d7427ad274cde9f8f7c869b5cae
MD5 e285b30e4329d05987e612060dfd7221
BLAKE2b-256 bcb2d8214803569ac55084b2c8f9777a7ae3ec3f81abc3c1b6ef156f75fc5731

See more details on using hashes here.

File details

Details for the file django_aws_s3_cache-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for django_aws_s3_cache-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8fd26bfd5b69269e79b957d1113a65accf3f40c0d5912dccfca99e18dfef474f
MD5 290bb0e54cf41253e3e49106cfd05402
BLAKE2b-256 268d5d60ce695869ca91fbe66e2a6e90685694e2ad5094f18ca2a34355b8f9d7

See more details on using hashes here.

Supported by

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