diff --git a/examples/gunicorn-aiocache-api/README.md b/examples/gunicorn-aiocache-api/README.md new file mode 100644 index 0000000..ce80cd4 --- /dev/null +++ b/examples/gunicorn-aiocache-api/README.md @@ -0,0 +1,21 @@ +# Gunicorn + AioHTTP + AioCache + SharedMemoryDict + +## Requirements: +- SharedMemoryDict + AioCache (`pip install shared-memory-cache[aiocache]`) +- AioHTTP +- Gunicorn + +## To Run: +``` +$ gunicorn main:app --config gunicorn_config.py -w 3 --worker-class aiohttp.GunicornWebWorker +``` + +## Write on cache +``` +$ curl -d "key=foo&value=bar" localhost:8000/ +``` + +## Read from cache +``` +$ curl localhost:8000/?key=foo +``` diff --git a/examples/gunicorn-aiocache-api/gunicorn_config.py b/examples/gunicorn-aiocache-api/gunicorn_config.py new file mode 100644 index 0000000..88d55da --- /dev/null +++ b/examples/gunicorn-aiocache-api/gunicorn_config.py @@ -0,0 +1,9 @@ +from shared_memory_dict.hooks import create_shared_memory, free_shared_memory + + +def on_starting(server): + create_shared_memory(name='sm', size=1024) + + +def on_exit(server): + free_shared_memory(name='sm') diff --git a/examples/gunicorn-aiocache-api/main.py b/examples/gunicorn-aiocache-api/main.py new file mode 100644 index 0000000..952a779 --- /dev/null +++ b/examples/gunicorn-aiocache-api/main.py @@ -0,0 +1,38 @@ +from aiocache import caches +from aiohttp import web + + +caches.set_config({ + 'default': { + 'cache': 'shared_memory_dict.caches.aiocache.SharedMemoryCache', + 'name': 'sm' + } +}) + + +class Handler(web.View): + + async def get(self): + key = self.request.query.get('key', '') + text = await self.request.app['cache'].get(key) + return web.Response(text=text) + + async def post(self): + data = await self.request.post() + await self.request.app['cache'].set(data['key'], data['value']) + return web.Response(text='OK!') + + +async def app_shutdown(app): + await app['cache'].close() + + +app = web.Application() +app.router.add_view('/', Handler) + +app['cache'] = caches.get('default') +app.on_shutdown.append(app_shutdown) + + +if __name__ == '__main__': + web.run_app(app)