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.4.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.4-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file django_aws_s3_cache-0.1.4.tar.gz.

File metadata

  • Download URL: django_aws_s3_cache-0.1.4.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.4.tar.gz
Algorithm Hash digest
SHA256 45355007f76e19635e445af2e4db8db35f60eb7b4945e3f7046b568f493dc4d8
MD5 177cdc557c857491aa5d8a25a6c80eff
BLAKE2b-256 42ab3ff90fcd44f20eb1d0657e993a6fd85128978213c95445dd07d9861c8f07

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_aws_s3_cache-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b38e7c4e842199d252ae2bed1427fbd669372824184963260a792e129d97a528
MD5 d158d46147c150d5ce84ea7deca1fe26
BLAKE2b-256 1fecb1d38d886f21db88b99f340ef83d9138341f7e72121eec84e820c52b57d0

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