{"library":"shelved-cache","title":"Shelved Cache","description":"Shelved Cache (version 0.5.0) is a Python library that provides a persistent cache implementation for `cachetools` objects, storing entries to disk using Python's `shelve` module. It allows for fast, persistent caching of function results across program executions. The library is actively maintained, integrating with standard Python caching mechanisms.","language":"python","status":"active","last_verified":"Wed May 20","install":{"commands":["pip install shelved-cache"],"cli":null},"imports":["from shelved_cache import PersistentCache","from shelved_cache import AsyncPersistentCache"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import cachetools\nfrom shelved_cache import PersistentCache\nfrom cachetools import LRUCache\nimport os\n\n# Define a unique cache file path\ncache_file = \"my_function_cache.db\"\n\n# --- Cleanup any previous cache files for a clean run ---\n# shelve can create multiple files (.db, .bak, .dir, .dat)\nfor ext in [\"\", \".bak\", \".dir\", \".dat\"]:\n    if os.path.exists(cache_file + ext):\n        os.remove(cache_file + ext)\n\n# Initialize a persistent LRU cache, linking it to a file\npc_for_square = PersistentCache(LRUCache, cache_file, maxsize=100)\n\n@cachetools.cached(pc_for_square)\ndef square(x):\n    print(f\"Calculating square for {x}...\")\n    return x * x\n\n# First call: calculation happens\nresult1 = square(5)\nprint(f\"First call: square(5) = {result1}\")\n\n# Second call: result is retrieved from cache (no 'Calculating...' output)\nresult2 = square(5)\nprint(f\"Second call: square(5) = {result2}\")\n\n# A different argument will trigger a new calculation and cache entry\nresult3 = square(10)\nprint(f\"Third call: square(10) = {result3}\")\n\n# It's crucial to close the persistent cache to ensure data is written to disk.\npc_for_square.close()\n\nprint(\"\\n--- Cache closed and reopened to demonstrate persistence ---\")\n\n# Simulate a new application run or process by creating a new PersistentCache instance\n# linked to the *same* file.\npc_reopened = PersistentCache(LRUCache, cache_file, maxsize=100)\n\n@cachetools.cached(pc_reopened)\ndef square_reopened(x):\n    # This should *not* print if the value was correctly persisted\n    print(f\"Calculating square (reopened) for {x}...\")\n    return x * x\n\n# This call should retrieve from the persisted cache without recalculating\nresult_reopened = square_reopened(5)\nprint(f\"Reopened call: square_reopened(5) = {result_reopened}\")\n\npc_reopened.close()\n\n# --- Final cleanup of created cache files ---\nfor ext in [\"\", \".bak\", \".dir\", \".dat\"]:\n    if os.path.exists(cache_file + ext):\n        os.remove(cache_file + ext)\n","lang":"python","description":"This quickstart demonstrates how to use `shelved-cache` to make a `cachetools.LRUCache` persistent. It shows a function being decorated, calls it multiple times to demonstrate caching, explicitly closes the cache, and then reopens it in a simulated new process to prove persistence. Ensure proper cleanup of the cache files created.","tag":null,"tag_description":null,"last_tested":"2026-04-25","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]},"compatibility":{"tag":null,"tag_description":null,"last_tested":"2026-05-20","installed_version":"0.4.0","pypi_latest":"0.5.0","is_stale":true,"summary":{"python_range":"3.10–3.9","success_rate":100,"avg_install_s":1.7,"avg_import_s":0.09,"wheel_type":"wheel"},"results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"shelved-cache","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.06,"mem_mb":2.7,"disk_size":"18.0M"},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"shelved-cache","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.08,"mem_mb":2.7,"disk_size":"18.0M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"shelved-cache","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.7,"import_time_s":0.05,"mem_mb":2.7,"disk_size":"18M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"shelved-cache","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.05,"mem_mb":2.7,"disk_size":"18M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"shelved-cache","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.11,"mem_mb":3.2,"disk_size":"19.8M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"shelved-cache","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.14,"mem_mb":3.2,"disk_size":"19.8M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"shelved-cache","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.6,"import_time_s":0.1,"mem_mb":3.2,"disk_size":"20M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"shelved-cache","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.14,"mem_mb":3.2,"disk_size":"20M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"shelved-cache","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.1,"mem_mb":3.1,"disk_size":"11.7M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"shelved-cache","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.12,"mem_mb":3.1,"disk_size":"11.7M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"shelved-cache","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.5,"import_time_s":0.1,"mem_mb":3.1,"disk_size":"12M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"shelved-cache","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.11,"mem_mb":3.1,"disk_size":"12M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"shelved-cache","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.09,"mem_mb":3.2,"disk_size":"11.4M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"shelved-cache","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.1,"mem_mb":3.1,"disk_size":"11.3M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"shelved-cache","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.6,"import_time_s":0.09,"mem_mb":3,"disk_size":"12M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"shelved-cache","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.12,"mem_mb":2.9,"disk_size":"12M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"shelved-cache","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.06,"mem_mb":2.6,"disk_size":"17.4M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"shelved-cache","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.08,"mem_mb":2.6,"disk_size":"17.4M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"shelved-cache","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.9,"import_time_s":0.05,"mem_mb":2.6,"disk_size":"18M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"shelved-cache","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.09,"mem_mb":2.6,"disk_size":"18M"}]}}