No project description provided
Project description
Dory
Dory is a Python3.8+ out-of-the box smart cache library. It simplifies multiple cache features and brings Bloats to the table, a tool designed to make smarter your application cache.
| Badges | |
|---|---|
| Build | |
| Docs | |
| Package | |
| Support |
Table of content
Installation
Installation is as simple as:
pip install dory-cache
Features
Cache utils
Dory simplifies several cache utilities with an out-of-the-box interface. For example, a decorator to cache views comfortably:
@api.get('/foo')
@dory.cache(key='foo', timeout=timedelta(hours=1))
def foo(request):
"""
Render a Foo
"""
...
More about it on the docs 🔥
Bloats
Porcupinefish have the ability to inflate their bodies by swallowing water or air, thereby becoming rounder.
Bloats responds to the necessity to have a solid and clean way to define cache usage and to permit an smart cache approach to your system.
The main idea behind it is that a Bloat has the ability to inflate -as a Porcupinefish does- meaning that has the ability to cache a key/value given a certain configuration.
Also, has the ability to deflate meaning exactly the contrary, that deletes the given key/value from the cache. Having a Bloat decoupled gives the application the ability to interact with the cache in a comfortable way around all the project.
For example, let's pretend that we have a model called Product wich can be either renderizer or edited.
class Product(Model):
"""
Store information about a Product
"""
id: int = PrimaryKey()
name: str = Text()
description: str = Text()
So, to improve the Product performance we cache the Product serialization view (GET /product).
@api.get('/product/<id>')
@dory.cache(key=lambda request, id: "product:%s" % id, timeout=timedelta(hours=1))
def get_product(request, id) -> Response:
"""
Serialize a Product
"""
...
Now everything works faster and as expected, but we did not contemplate that since the Product can be edited (POST /product), we could have cached an outdated version of the Product, so we need a way to force the cache to refresh itself. This is where Bloats come in handy!
Instead of caching the view with a custom key, decouple the cache configuration on a Bloat:
class Product(dory.Bloat):
"""
Product's bloat
"""
prefix: str = 'product'
key: str
timeout: timedelta = timedelta(hours=1)
enabled: bool = True
def __init__(product_id: int):
self.key = '%s' % product_id
@api.get('/product/<id>')
@bloats.Product.cache(args=lambda request, id: dict(product_id=id))
def get_product(request, id) -> Response:
"""
Serialize a Product
"""
...
And now, when a Product is edited, you can force the view to refresh the cache using the Bloat as a middle-man.
@api.post('/product/<id>')
def edit_product(request, id):
"""
Edit a Product
"""
# edit the product
services.product.edit(request.body)
# clean up outdated cache
bloats.Product(product_id=id).deflate()
Now your cache will always be in sync and it'll be configured in a cleaner way! 🔥
Roadmap
-
Cache utils (See Cache utils)
- Cache decorator
- Ratelimit
-
Bloats 🐡 (See Bloats)
-
Django signals
class Product(models.Model, dory.django.BloatModel): """ Store information about a Product """ id: int name: str = m.CharField(max_length=24) description: str = m.TextField()
@api.get('/product/<id>') @bloats.Product.cache(args=lambda request, id: dict(product_id=id), deflate_on=models.Product.post_save) def get_product(request, id): """ Serialize a Product """ ...
-
Bloats v2
- The v2 of the
Bloatswill implement the method.reflate(), capable not only todeflatethe currentBloatbut toinflateit again. The design is still aWIP.
- The v2 of the
Contributing
Suggestions and contributions are extremely welcome! ❤️
Just open an issue or a PR, and I'll respond as soon as I can.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file dory-cache-0.1.0.tar.gz.
File metadata
- Download URL: dory-cache-0.1.0.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e62196ae8ad5fbdf45badae280edb66fb44396621ff5b0fb7d6ff82cad9ef028
|
|
| MD5 |
7d67583ca81d052a5012591f91ef41bd
|
|
| BLAKE2b-256 |
ec8e52dcfffd555aa2fb51ad541839a41b9e06e84f382653affb0fc4b392d918
|