|
4 | 4 |
|
5 | 5 | # pyBiocFileCache |
6 | 6 |
|
7 | | -File system based cache for resources & metadata. Compatible with [BiocFileCache R package](https://github.com/Bioconductor/BiocFileCache) |
| 7 | +`pyBiocFileCache` is a Python package that provides a robust file caching system with resource validation, cache size management, file compression, and resource tagging. Compatible with [BiocFileCache R package](https://github.com/Bioconductor/BiocFileCache). |
8 | 8 |
|
9 | | -***Note: Package is in development. Use with caution!!*** |
| 9 | +## Installation |
10 | 10 |
|
11 | | -### Installation |
| 11 | +Install from [PyPI](https://pypi.org/project/pyBiocFileCache/), |
12 | 12 |
|
13 | | -Package is published to [PyPI](https://pypi.org/project/pyBiocFileCache/) |
14 | | - |
15 | | -``` |
| 13 | +```bash |
16 | 14 | pip install pybiocfilecache |
17 | 15 | ``` |
18 | 16 |
|
19 | | -#### Initialize a cache directory |
20 | | - |
21 | | -``` |
22 | | -from pybiocfilecache import BiocFileCache |
23 | | -import os |
| 17 | +## Quick Start |
24 | 18 |
|
25 | | -bfc = BiocFileCache(cache_dir = os.getcwd() + "/cache") |
26 | | -``` |
| 19 | +```python |
| 20 | +from biocfilecache import BiocFileCache |
27 | 21 |
|
28 | | -Once the cache directory is created, the library provides methods to |
29 | | -- `add`: Add a resource or artifact to cache |
30 | | -- `get`: Get the resource from cache |
31 | | -- `remove`: Remove a resource from cache |
32 | | -- `update`: update the resource in cache |
33 | | -- `purge`: purge the entire cache, removes all files in the cache directory |
| 22 | +# Initialize cache |
| 23 | +cache = BiocFileCache("path/to/cache/directory") |
34 | 24 |
|
35 | | -### Add a resource to cache |
| 25 | +# Add a file to cache |
| 26 | +resource = cache.add("myfile", "path/to/file.txt") |
36 | 27 |
|
37 | | -(for testing use the temp files in the `tests/data` directory) |
| 28 | +# Retrieve a file from cache |
| 29 | +resource = cache.get("myfile") |
38 | 30 |
|
39 | | -``` |
40 | | -rec = bfc.add("test1", os.getcwd() + "/test1.txt") |
41 | | -print(rec) |
| 31 | +# Use the cached file |
| 32 | +print(resource.rpath) # Path to cached file |
42 | 33 | ``` |
43 | 34 |
|
44 | | -### Get resource from cache |
| 35 | +## Advanced Usage |
45 | 36 |
|
46 | | -``` |
47 | | -rec = bfc.get("test1") |
48 | | -print(rec) |
49 | | -``` |
| 37 | +### Configuration |
50 | 38 |
|
51 | | -### Remove resource from cache |
| 39 | +```python |
| 40 | +from biocfilecache import BiocFileCache, CacheConfig |
| 41 | +from datetime import timedelta |
| 42 | +from pathlib import Path |
52 | 43 |
|
53 | | -``` |
54 | | -rec = bfc.remove("test1") |
55 | | -print(rec) |
| 44 | +# Create custom configuration |
| 45 | +config = CacheConfig( |
| 46 | + cache_dir=Path("cache_directory"), |
| 47 | + max_size_bytes=1024 * 1024 * 1024, # 1GB |
| 48 | + cleanup_interval=timedelta(days=7), |
| 49 | + compression=True |
| 50 | +) |
| 51 | + |
| 52 | +# Initialize cache with configuration |
| 53 | +cache = BiocFileCache(config=config) |
56 | 54 | ``` |
57 | 55 |
|
58 | | -### Update resource in cache |
| 56 | +### Resource Management |
59 | 57 |
|
60 | | -``` |
61 | | -rec = bfc.get("test1"m os.getcwd() + "test2.txt") |
62 | | -print(rec) |
63 | | -``` |
| 58 | +```python |
| 59 | +# Add file with tags and expiration |
| 60 | +from datetime import datetime, timedelta |
64 | 61 |
|
65 | | -### purge the cache |
| 62 | +resource = cache.add( |
| 63 | + "myfile", |
| 64 | + "path/to/file.txt", |
| 65 | + tags=["data", "raw"], |
| 66 | + expires=datetime.now() + timedelta(days=30) |
| 67 | +) |
66 | 68 |
|
67 | | -``` |
68 | | -bfc.purge() |
| 69 | +# List resources by tag |
| 70 | +resources = cache.list_resources(tag="data") |
| 71 | + |
| 72 | +# Search resources |
| 73 | +results = cache.search("myfile", field="rname") |
| 74 | + |
| 75 | +# Update resource |
| 76 | +cache.update("myfile", "path/to/new_file.txt") |
| 77 | + |
| 78 | +# Remove resource |
| 79 | +cache.remove("myfile") |
69 | 80 | ``` |
70 | 81 |
|
| 82 | +### Cache Statistics and Maintenance |
71 | 83 |
|
72 | | -<!-- pyscaffold-notes --> |
| 84 | +```python |
| 85 | +# Get cache statistics |
| 86 | +stats = cache.get_stats() |
| 87 | +print(stats) |
73 | 88 |
|
74 | | -## Note |
| 89 | +# Clean up expired resources |
| 90 | +removed_count = cache.cleanup() |
75 | 91 |
|
76 | | -This project has been set up using PyScaffold 4.1. For details and usage |
77 | | -information on PyScaffold see https://pyscaffold.org/. |
| 92 | +# Purge entire cache |
| 93 | +cache.purge() |
| 94 | +``` |
0 commit comments