▼ |  | File: Racket File and Format Libraries

1 | Convertible: Data-Conversion Protocol
2 | gzip Compression and File Creation
3 | gzip Decompression
4 | zip File Creation
5 | zip File Extraction
6 | tar File Creation
7 | tar File Extraction
8 | tar+gzip File Extraction
9 | MD5 Message Digest
10 | SHA1 Message Digest
11 | GIF File Writing
12 | ICO File Reading and Writing
13 | Windows Registry
14 | Caching
15 | Globbing
| Bibliography
| Index

On this page:

cache-file
cache-remove

top ← prev up next →

### 14`` Caching

(require file/cache) | package: base

The file/cache library provides utilities for managing a local cache of files, such as downloaded files. The cache is safe for concurrent use across processes, since it uses filesystem locks, and it isolates clients from filesystem failures.

> > procedure
> > (cache-file |  | dest-file |  |  |  |
> > | [ | #:exists-ok? exists-ok?] |  |  |  |
> > |  | key |  |  |  |
> > |  | cache-dir |  |  |  |
> > |  | fetch |  |  |  |
> > | [ | #:notify-cache-use notify-cache-use |  |  |  |
> > |  | #:max-cache-files max-files |  |  |  |
> > |  | #:max-cache-size max-size |  |  |  |
> > |  | #:evict-before? evict-before? |  |  |  |
> > |  | #:log-error-string log-error-string |  |  |  |
> > |  | #:log-debug-string log-debug-string]) |  | → |  | void?
> dest-file : path-string?
> exists-ok? : any/c = #f
> key : (not/c #f)
> cache-dir : path-string?
> fetch : (-> any)
> notify-cache-use : (string? . -> . any) = void
> max-files : real? = 1024
> max-size : real? = (* 64 1024 1024)
> | evict-before? |  | : |  | (hash? hash? . -> . boolean?)
> |  |  | = |  |
> (lambda (a b)
> (< (hash-ref a 'modify-seconds)
> (hash-ref b 'modify-seconds)))
> | log-error-string |  | : |  | (string? . -> . any)
> |  |  | = |  | (lambda (s) (log-error s))
> | log-debug-string |  | : |  | (string? . -> . any)
> |  |  | = |  | (lambda (s) (log-debug s))

Looks for a file in cache-dir previously cached with key, and copies it to dest-file (which must not exist already, unless exists-ok? is true) if a cached file is found. Otherwise, fetch is called; if dest-file exists after calling fetch, it is copied to cache-dir and recorded with key. When a cache entry is used, notify-cache-use is called with the name of the cache file.

When a new file is cached, max-files (as a file count) and max-size (in bytes) determine whether any previously cached files should be evicted from the cache. If so, evict-before? determines an order on existing cache entries for eviction; each argument to evict-before? is a hash table with at least the following keys:

- 'modify-seconds — the file’s modification date

- 'size — the file’s size in bytes

- 'key — the cache entry’s key

- 'name — the cache file’s name

The log-error-string and log-debug-string functions are used to record errors and debugging information.

> > procedure
> > (cache-remove |  | key |  |  |  |
> > |  | cache-dir |  |  |  |
> > | [ | #:log-error-string log-error-string |  |  |  |
> > |  | #:log-debug-string log-debug-string]) |  | → |  | void?
> key : any/c
> cache-dir : path-string?
> | log-error-string |  | : |  | (string? . -> . any)
> |  |  | = |  | (lambda (s) (log-error s))
> | log-debug-string |  | : |  | (string? . -> . any)
> |  |  | = |  | (lambda (s) (log-debug s))

Removes the cache entry matching key (if any) from the cache in cache-dir, or removes all cached files if key is #f.

The log-error-string and log-debug-string functions are used to record errors and debugging information.

top ← prev up next →