https://github.com/neetjn/falcon-redis-cache
Redis cache middleware for falcon resources. Pulled from py-blog project @ https://github.com/neetjn/py-blog
https://github.com/neetjn/falcon-redis-cache
cache falcon middleware python redis rest
Last synced: 4 months ago
JSON representation
Redis cache middleware for falcon resources. Pulled from py-blog project @ https://github.com/neetjn/py-blog
- Host: GitHub
- URL: https://github.com/neetjn/falcon-redis-cache
- Owner: neetjn
- License: mit
- Created: 2019-02-07T20:56:00.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-04-16T00:02:28.000Z (over 6 years ago)
- Last Synced: 2025-06-14T03:05:19.314Z (4 months ago)
- Topics: cache, falcon, middleware, python, redis, rest
- Language: Python
- Size: 55.7 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# falcon-redis-cache
[](https://travis-ci.org/neetjn/falcon-redis-cache)
[](https://codecov.io/gh/neetjn/falcon-redis-cache)[](https://badge.fury.io/py/falcon-redis-cache)
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:
```bash
pip install falcon-redis-cache
```Once installed, the middleware can be instantiated extending the Falcon api:
```python
import falcon
from falcon_redis_cache.middleware import RedisCacheMiddlewareapi = falcon.API(middleware=[
RedisCacheMiddleware(redis_host=..., redis_port=..., redis_db=...)
])
```To ensure your resources are compatible, inherit from the provided resource:
```python
from falcon_redis_cache.resource import CacheCompaitableResourceclass 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:
```python
from falcon_redis_cache.hooks import CacheProviderclass 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`:
```python
import inject
import redisfrom 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
* **John Nolette** (john@neetgroup.net)
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.