Skip to main content

In-memory cache with multiprocessing support

Project description

Introduction

UpCache is a pure Python cache with multiprocessing support.

Most Python cache frameworks require external services such as Memcached or Redis which require setup and maintenance. UpCache tries to ameliorate the process of creating a shared cache for multiprocessing workers such as those found in WSGI applications.

Getting Started

Using UpCache requires at least one instance of UpCache to remain open and available for contents to be preserved between worker processes.

Sample code provided will create a key-value pair in one process and allows another process to access it:

from multiprocessing import Process
from upcache import UpCache
import time

def worker1(name: str) -> None:
    cache = UpCache(name)
    cache.set(b'Light Entertainment Awards', b'Dickie Attenborough')
    cache.close()

def worker2(name: str) -> None:
    cache = UpCache(name)
    time.sleep(1)
    print(cache.get(b'Light Entertainment Awards'))
    cache.close()

if __name__ == '__main__':
    cache_name = 'TVHosts'
    p1 = Process(target=worker1, args=(cache_name,))
    p2 = Process(target=worker2, args=(cache_name,))

    p1.start()
    p2.start()

    p1.join()
    p2.join()

Architecture

UpCache spawns an internally-hosted TCP server which facilitates inter-process communication and provides a synchronized in-memory key-value store.

To avoid overlapping TCP servers from spawning when using UpCache, the library creates an exclusive connection file to claim ownership as the process to spawn the TCP server. Once the TCP server begins running on an ephemeral port, it writes the connection info into the connection file for all UpCache clients to connect to the TCP server for the key-value store.

UpCache clients are typically shared per cache per process to avoid heavy resource contention (one client equals one TCP socket).

Once all clients disconnect, the UpCache TCP server shuts down and disposes of all contents (both the in-memory key-value store and connection file).

Features

UpCache supports the following:

  • Basic key operations (get/set/drop, increment/decrement, clear, count, keys, items)
  • Waiting for key events
  • Subscriptions to key events

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

upcache-0.2.6-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

Details for the file upcache-0.2.6-py3-none-any.whl.

File metadata

  • Download URL: upcache-0.2.6-py3-none-any.whl
  • Upload date:
  • Size: 17.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.2

File hashes

Hashes for upcache-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 a2495546d8262bb286c5ff24e6161b06b269894e10e87342e268b233396f8a2f
MD5 5fa0897d6c08db30004899b9249d607b
BLAKE2b-256 bcddd93c4d7ea4714da2c21ce2f76b4befba819e90ca47d496be9ae3e010ac07

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page