Skip to main content

NFS-safe persistent dictionary. Atomic file-per-key writes, in-memory read cache.

Project description

nfsdict

NFS-safe persistent dictionary for Python (CPython & PyPy).

One file per key, atomic writes via temp + rename, in-memory read cache so reads never touch NFS after initial load. Corruption of one file loses that key only — never the whole store.

A best-effort key index speeds up init by avoiding expensive directory scans. The index is advisory — exact lookups always fall back to disk, so keys written by other processes are never lost.

Install

pip install nfsdict

Usage

from nfsdict import NfsDict

# default: pickle serializer, ~/.local/share/nfsdict/default/
d = NfsDict()

# custom name and directory
d = NfsDict("my_cache", base_dir="/shared/nfs/cache")

# json serializer (human-readable, but values must be JSON-serializable)
d = NfsDict("my_cache", serializer="json")

# lazy loading: only keys are loaded at init, values fetched on first access
d = NfsDict("my_cache", lazy=True)

# use it like a normal dict
d["key"] = {"nested": [1, 2, 3]}
print(d["key"])
del d["key"]
print(len(d))

# re-read from disk (full scan, call if another process may have written)
d.sync()

# context manager flushes index on exit
with NfsDict("my_cache") as d:
    d["key"] = "value"

How it works

  • Each key is stored as a separate file, named by its SHA-256 hash, inside a two-character prefix subdirectory for filesystem-friendly sharding.
  • Writes are atomic: data is written to a temp file, fsynced, then os.replaced into place.
  • An in-memory cache is populated once at init and updated on every write/delete — so reads are pure dict lookups with zero NFS calls.
  • If a key is not in the cache, __getitem__ falls back to reading its file directly from disk (covers keys written by other processes).
  • A best-effort key index avoids the expensive rglob directory scan on startup. If the index is missing or corrupted, a full scan runs automatically and rebuilds it.
  • Corrupted value files are silently skipped during loading: you lose that one key, not the whole store.

API

NfsDict(name="default", base_dir=None, serializer="pickle", lazy=False)

Parameter Description
name Namespace / subdirectory name
base_dir Storage root. Defaults to platformdirs.user_data_dir
serializer "pickle" or "json"
lazy If True, values are loaded on first access, not at init

Supports the full MutableMapping interface: __getitem__, __setitem__, __delitem__, __contains__, __iter__, __len__, plus:

  • .sync() — full re-read from disk
  • .close() — flush the key index to disk
  • Context manager (with NfsDict(...) as d:)

License

MIT

Project details


Download files

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

Source Distribution

nfsdict-0.2.2.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nfsdict-0.2.2-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file nfsdict-0.2.2.tar.gz.

File metadata

  • Download URL: nfsdict-0.2.2.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for nfsdict-0.2.2.tar.gz
Algorithm Hash digest
SHA256 8c9bfb7b4877bbdc0eda37ff7ae345a48485c8fe399eeabeb28bc722c3fd5145
MD5 9890b07dd15a9855ddf31a89ed0c0422
BLAKE2b-256 43cf26a567016af75dd15b6a706c4456fc5bfa456200bda96cb423e0cff1af56

See more details on using hashes here.

File details

Details for the file nfsdict-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: nfsdict-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for nfsdict-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1c0687389108a645c7789146fbca52fd863495bc5073393ceb9d0d5f0aed0fde
MD5 f3478344f543646496e77cb38ba7a5c1
BLAKE2b-256 fe88522e5ef860dd019d8ad632671221f7b685489d13387aafd6737fad5223ce

See more details on using hashes here.

Supported by

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