{"id":3490,"library":"fcache","title":"Fcache: File-Based Cache","description":"Fcache is a Python library that provides a dictionary-like, file-based cache module. It is designed to be simple to use, includes an optional write buffer, and is compatible with Python's `shelve` module. The current version is 0.6.0, and releases occur on an as-needed basis, with the last major update in late 2024.","status":"active","version":"0.6.0","language":"en","source_language":"en","source_url":"https://github.com/tsroten/fcache","tags":["cache","file-based","persistence","dictionary-like"],"install":[{"cmd":"pip install fcache","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The primary class `FileCache` resides within the `fcache.cache` submodule, not directly under `fcache`.","wrong":"import fcache","symbol":"FileCache","correct":"from fcache.cache import FileCache"}],"quickstart":{"code":"from fcache.cache import FileCache\n\n# Create a cache named 'myapp' in a default location\nmycache = FileCache('myapp')\n\n# Store data\nmycache['greeting'] = 'Hello, Fcache!'\nmycache['number'] = 123\n\n# Retrieve data\nprint(f\"Retrieved greeting: {mycache['greeting']}\")\nprint(f\"Retrieved number: {mycache['number']}\")\n\n# Check if a key exists\nprint(f\"'greeting' in cache: {'greeting' in mycache}\")\n\n# Update data\nmycache['greeting'] = 'Hola, Fcache!'\nprint(f\"Updated greeting: {mycache['greeting']}\")\n\n# Close the cache to ensure data is flushed (important!)\nmycache.close()\n\n# Re-open and verify persistence\nanother_cache_instance = FileCache('myapp')\nprint(f\"Re-opened greeting: {another_cache_instance['greeting']}\")\nanother_cache_instance.close()\n","lang":"python","description":"This quickstart demonstrates how to initialize a `FileCache`, store and retrieve data, check for key existence, update values, and properly close the cache to ensure data persistence. Re-opening verifies that the data is saved to disk."},"warnings":[{"fix":"Always call `.close()` on your `FileCache` instance when it's no longer needed, or use it within a `with` statement if context manager support is available (check documentation for future versions).","message":"Failing to call `FileCache.close()` can lead to data loss. Buffered writes to the file system may not be flushed, especially upon unexpected program termination.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Periodically clear stale or unnecessary entries using `del mycache[key]` or `mycache.clear()` for the entire cache. Monitor disk usage of your cache directory.","message":"File-based caches, if not explicitly managed, can consume significant disk space over time. `fcache` does not automatically implement eviction policies like LRU or TTL out-of-the-box.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use `FileCache.delete()` with extreme caution and ensure it's the intended action to completely destroy the cache. Consider backups if the cached data is critical.","message":"The `FileCache.delete()` method permanently removes the cache directory and all its contents from the file system. This operation is irreversible.","severity":"breaking","affected_versions":"All versions"},{"fix":"For highly concurrent or multi-process environments, implement external locking or ensure that only one process/thread modifies a specific cache instance at a time. Consult `shelve` documentation for its concurrency guarantees.","message":"While `fcache` leverages `shelve` for persistence, concurrent access from multiple processes or threads without proper synchronization mechanisms (not provided by `fcache` itself for all operations) could potentially lead to data corruption or race conditions, especially on write operations.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}