Skip to main content

Shared, persistent, and smart caching

Documentation Status PyPI version Test EasyCaching

Documentation

https://easycache.readthedocs.io/en/latest/

What is it?

easycaching provides a single shared interface for storing and retreiving data from memory among many processes(forks) of an application.

Features

  • fast and shared access to data
  • persistent cache backed by a database
  • auto-forking
  • python syntax

Get Started

pip install easycaching

Cache Usage

import asyncio
from easycaching import EasyCacheManager

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

    test = await cache.create_cache('test')

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


    # get
    cached_value = await test.get('foo')

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

    # iterate over cache items
    async for cache_item in test:
        print(cache_item)

    # delete
    await test.delete('foo')

    # clear all cache
    await test.clear()

    # access 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 exit
    await cache.close()

asyncio.run(main())

Queue Usage

import asyncio
from easycaching import EasyCacheManager

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

    test_queue = await cache.create_queue('test')

    # add items to queue
    await test_queue.put('first')
    await test_queue.put('second')
    await test_queue.put('third')

    # grab items from queue
    result = await test_queue.get()

    await test_queue.get() # second
    await test_queue.get() # third
    result = await test_queue.get() # empty
    {'warning': 'queue empty'}

    # empty a queue
    await test_queue.clear()

    # accessing via manager

    await cache.queues['test'].put('fourth')
    await cache.queues['test'].put('fifth') 

    # safely exit
    await cache.close()

FastAPI Usage - Cache

# 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 8230 basic:app --workers=5

FastAPI Usage - Queue

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

app = FastAPI()

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

@app.post('/queue')
async def create_queue(name: str):
    queue = await app.cache.create_queue(name)
    return f"queue {name} created"

@app.post('/queue/{queue}')
async def add_to_queue(queue: str, data: dict):
    return await cache.queues[queue].put(data)

@app.get('/queue/{queue}')
async def pull_from_queue(queue: str):
    return await cache.queues[queue].get()
uvicorn --host 0.0.0.0 --port 8220 --workers 5 basic:app

Under the Hood

easycaching utilizes the smart caching of aiopyql to provide cache acesss, cache storage, and perhaps most importantly updates and invalidation.

Data access sharing is made possible via proxy methods using easyrpc. A cache background task is created & managed by gunicorn which main application workers access via proxies.

Release files for easycaching 0.108

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for easycaching 0.108
File Interpreter ABI Platform
easycaching-0.108-py3-none-any.whl Python 3 none any Details

Release files / easycaching-0.108-py3-none-any.whl

Download URL easycaching-0.108-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
35b3ad98d71ab5f61a618cf3b852293e9ada0be1b5a5d8a8ba018cf917b23048
BLAKE2b-256 checksum
How to use checksums
dbc49e95c3ce99379b7e7ae5f896d24c11e0b72bca18b1c0f2d69636906c6725
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.108 This release

1 release file

0.107

1 release file

0.106

1 release file

0.105

1 release file

0.104

1 release file

0.103

1 release file

0.102

1 release file

0.101

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page