Skip to main content

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 tests build/deploy codecov
Docs documentation
Package PyPi PyPi versions code style
Support buy-me-a-coffee

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 Bloats will implement the method .reflate(), capable not only to deflate the current Bloat but to inflate it again. The design is still a WIP.

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dory-cache-0.1.0.tar.gz (5.0 kB view hashes)

Uploaded Source

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