|
14 | 14 |
|
15 | 15 | // Package fscache implements a file system-based cache backend. |
16 | 16 | // |
17 | | -// Entries are stored as files in a directory under the user's OS cache |
18 | | -// directory by default. The cache location and behavior are configured via DSN |
19 | | -// query parameters. |
| 17 | +// Entries are stored as files in a structured directory hierarchy. Cache entries |
| 18 | +// are stored under the user's OS cache directory by default, with support for |
| 19 | +// custom base directories via the [WithBaseDir] option or DSN path specification. |
20 | 20 | // |
21 | | -// Supported DSN query parameters: |
22 | | -// - appname (required): Subdirectory name for cache isolation. |
23 | | -// - timeout (optional): Operation timeout (e.g., "2s", "500ms"). Default: 5m. |
24 | | -// - connect_timeout (optional): Timeout for establishing the cache |
25 | | -// connection (e.g., "2s"). Default: 5m. |
26 | | -// - encrypt (optional): Enable encryption for cache entries. Set to "on" or |
27 | | -// "aesgcm". |
28 | | -// - encrypt_key (optional): The AES key, base64-encoded (URL-safe variant |
29 | | -// with/without padding as per RFC 4648 §5). Must decode to the correct |
30 | | -// length as required by [crypto/aes]. |
31 | | -// - update_mtime (optional): Update file modification time on cache hits. |
32 | | -// Set to "on" to enable. Default: "off". |
| 21 | +// The cache supports advanced features including AES-GCM encryption, configurable |
| 22 | +// timeouts, and optional modification time tracking for LRU cleanup strategies. |
| 23 | +// All configuration can be specified through DSN query parameters or programmatic |
| 24 | +// [Option]s passed to [Open]. |
33 | 25 | // |
34 | | -// Example DSNs: |
| 26 | +// # Configuration Parameters |
35 | 27 | // |
36 | | -// fscache://?appname=myapp |
37 | | -// fscache:///tmp/cache?appname=myapp&timeout=2s |
38 | | -// fscache:///tmp/cache?appname=myapp&connect_timeout=2s&timeout=1m |
39 | | -// fscache://?appname=myapp&encrypt=on&encrypt_key=6S-Ks2YYOW0xMvTzKSv6QD30gZeOi1c6Ydr-As5csWk= |
40 | | -// fscache://?appname=myapp&update_mtime=on |
| 28 | +// The following DSN query parameters are supported: |
41 | 29 | // |
42 | | -// Example environment variable: |
| 30 | +// - appname (required): Application subdirectory name for cache isolation |
| 31 | +// - timeout (optional): Operation timeout duration (default: 5m) |
| 32 | +// - connect_timeout (optional): Cache initialization timeout (default: 5m) |
| 33 | +// - encrypt (optional): Enable AES-GCM encryption ("on" or "aesgcm") |
| 34 | +// - encrypt_key (optional): Base64-encoded AES key (URL-safe, RFC 4648 §5) |
| 35 | +// - update_mtime (optional): Update file mtime on cache hits ("on" to enable) |
| 36 | +// |
| 37 | +// # Usage Examples |
| 38 | +// |
| 39 | +// The DSN and equavalent programmatic usage are shown below. |
| 40 | +// |
| 41 | +// Basic usage: |
| 42 | +// |
| 43 | +// fscache://?appname=myapp |
| 44 | +// fscache.Open("myapp") |
| 45 | +// |
| 46 | +// Custom directory with timeouts: |
| 47 | +// |
| 48 | +// fscache:///tmp/cache?appname=myapp&timeout=2s&connect_timeout=10s |
| 49 | +// fscache.Open("myapp", fscache.WithBaseDir("/tmp/cache"), fscache.WithTimeout(2*time.Second), fscache.WithConnectTimeout(10*time.Second)) |
| 50 | +// |
| 51 | +// Encrypted cache: |
| 52 | +// |
| 53 | +// fscache://?appname=myapp&encrypt=on&encrypt_key=6S-Ks2YYOW0xMvTzKSv6QD30gZeOi1c6Ydr-As5csWk= |
| 54 | +// fscache.Open("myapp", fscache.WithEncryption("6S-Ks2YYOW0xMvTzKSv6QD30gZeOi1c6Ydr-As5csWk=")) |
| 55 | +// |
| 56 | +// Update modification time on cache hits: |
| 57 | +// |
| 58 | +// fscache://?appname=myapp&update_mtime=on |
| 59 | +// fscache.Open("myapp", fscache.WithUpdateMTime(true)) |
| 60 | +// |
| 61 | +// # Encryption Key Management |
| 62 | +// |
| 63 | +// Encryption keys can be provided via DSN parameter or environment variable: |
43 | 64 | // |
44 | 65 | // export FSCACHE_ENCRYPT_KEY="6S-Ks2YYOW0xMvTzKSv6QD30gZeOi1c6Ydr-As5csWk=" |
45 | 66 | // |
46 | | -// To generate a suitable encryption key (32 bytes, URL-safe, with padding): |
| 67 | +// Generate a secure 256-bit encryption key: |
47 | 68 | // |
48 | 69 | // openssl rand 32 | base64 | tr '+/' '-_' | tr -d '\n' |
| 70 | +// |
| 71 | +// # LRU Cleanup Integration |
| 72 | +// |
| 73 | +// When update_mtime is enabled, cache hits update file modification times, |
| 74 | +// enabling efficient LRU cleanup using standard Unix tools: |
| 75 | +// |
| 76 | +// # Remove files older than 7 days |
| 77 | +// find /cache/dir -type f -mtime +7 -delete |
| 78 | +// |
| 79 | +// # Keep only the 1000 most recently used files |
| 80 | +// find /cache/dir -type f -printf '%T@ %p\n' | sort -rn | tail -n +1001 | cut -d' ' -f2- | xargs rm |
49 | 81 | package fscache |
50 | 82 |
|
51 | 83 | import ( |
|
0 commit comments