Skip to content

neetjn/falcon-redis-cache

Repository files navigation

falcon-redis-cache

build codecov

PyPI version

Redis cache middleware for falcon resources. Pulled from py-blog project @ https://github.com/neetjn/py-blog

About

falcon-redis-cache is a very simple middlware for the Falcon Web/REST framework for Python. This middleware caches resources over the wire using Redis, and helps both simplify and automate redundant tasks such as serving responses directly from cache.

Use

This project should be compaitable with any version of Falcon. Support is available for both Python 2.7 and 3.4+.

falcon-redis-cache can be installed with pip like so:

pip install falcon-redis-cache

Once installed, the middleware can be instantiated extending the Falcon api:

import falcon
from falcon_redis_cache.middleware import RedisCacheMiddleware


api = falcon.API(middleware=[
  RedisCacheMiddleware(redis_host=..., redis_port=..., redis_db=...)
])

To ensure your resources are compatible, inherit from the provided resource:

from falcon_redis_cache.resource import CacheCompaitableResource


class MyResource(CacheCompaitableResource):

  route = '/'

  # enabled by default
  # - tells middleware to enable caching on resource
  use_cache = True
  # disabled by default
  # - tells middleware to cache using auth credentials
  unique_cache = False
  # disabled by default
  # - tells middleware to process matching resources with unique query strings
  cache_with_query = False
  # list of resource definitions to clear cache from in the event of a change
  # for ex; if a post, put, or delete request is made on this resource...
  # any binded resources will have their caches cleaned
  binded_resources = []

  def on_get(self, req, res):
    ...

You may serve directly from cache without having Falcon hit responders by using the from_cahe hook:

from falcon_redis_cache.hooks import CacheProvider


class ItemResource(CacheCompaitableResource):

  route = '/item/{item_id}/'

  @CacheProvider.from_cache
  def on_get(self, req, resp, item_id):
    # if resource already exists in cache, responder will not be processed by Falcon
    ...

For clearing resource cache where bindings are not possible or inherantly difficult, refer to the utility method clear_resource_cache:

import inject
import redis

from falcon_redis_cache.utils import clear_resource_cache


class ItemCommentResource(CacheCompaitableResource):

  route '/item/{item_id}/comment'

  def on_post(self, req, resp, item_id):
    # method makes use of python-inject for redis DI
    clear_resource_cache(ItemResource, req, item_id=item_id)

inject.configure(lambda binder: binder.bind(redis.Redis, ...)))

Contributors

Contributing guidelines are as follows,

  • Any new features added must also be unit tested in the tests subdirectory.
    • Branches for bugs and features should be structured like so, issue-x-username.
  • Include your name and email in the contributors list.

Copyright (c) 2019 John Nolette Licensed under the MIT license.

About

Redis cache middleware for falcon resources. Pulled from py-blog project @ https://github.com/neetjn/py-blog

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages