Cache

EasyCaching Cache is shared & persistent a Key / Value store, backed by a familiar RBDMS.

Basic Usage

Create EasyCacheManager

import asyncio
from easycaching import EasyCacheManager

async def main():
    # create EasyCache instance
    cache = await EasyCacheManager.create(
        'test'
    )

Create Cache instance

    # inside main()
    test = await cache.create_cache('test')

Adding Data to Cache

    # set
    await test.set('foo', 'bar')

Getting Data from Cache


    cached_value = await test.get('foo')

Checking if Key Exists

    # boolean
    exists = await test.contains('foo')

Delete data from Cache

Removing a single Key

    # delete
    await test.delete('foo')

Removing all entries from a cache

    await test.clear()

Iterating over data in Cache

    async for cache_item in test:
        print(cache_item)
{'foo': 'bar'}
{'is': 'basic'}

Accessing Cache via Manager

If a cache already exists, it is also accessible via manager

    await cache.cache['test'].set('first', 'worst')
    await cache.cache['test'].set('second', 'best')
    await cache.cache['test'].set('third', 'treasure chest')

Safely Exiting EasyCacheManager

To ensure Easycaching correctly exits, ensure that .close() is awaited each time.

    await cache.close()

FastAPI Usage

EasyCaching integrates very easily into the FastAPI ecosystem

# basic.py
from fastapi import FastAPI
from easycaching import EasyCacheManager

app = FastAPI()

@app.on_event('startup')
async def start_cache():
    app.cache = await EasyCacheManager.create(
        'test'
    )
    # create cache instance
    await app.cache.create_cache('test')


@app.get('/cache')
async def view_cache(key: str):
    return {
        key:  await app.cache.cache['test'][key]
    }

@app.post('/cache')
async def set_cache(key: str, value: str):
    return await app.cache.cache['test'].set(
        key, 
        {'json': value}
    )

uvicorn --host 0.0.0.0 --port 8220 --workers 5 basic:app