{"size": 3413, "ext": "scrbl", "max_stars_repo_path": "pkgs/racket-doc/file/scribblings/cache.scrbl", "max_stars_repo_name": "lkh01/racket", "max_stars_repo_stars_event_min_datetime": "2018-07-29T04:13:52.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-14T16:11:17.000Z", "max_issues_repo_path": "pkgs/racket-doc/file/scribblings/cache.scrbl", "max_issues_repo_name": "lkh01/racket", "max_issues_repo_issues_event_min_datetime": "2017-08-25T20:26:37.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T17:48:40.000Z", "max_forks_repo_path": "pkgs/racket-doc/file/scribblings/cache.scrbl", "max_forks_repo_name": "lkh01/racket", "max_forks_repo_forks_event_min_datetime": "2016-04-25T11:47:57.000Z", "max_forks_repo_forks_event_max_datetime": "2017-05-09T14:52:48.000Z"}
{"max_stars_count": 10, "max_issues_count": 15, "max_forks_count": 3, "avg_line_length": 44.3246753247, "max_line_length": 85, "alphanum_fraction": 0.5651919133}
#lang scribble/doc
@(require "common.rkt"
          (for-label file/cache))

@title[#:tag "cache"]{Caching}

@defmodule[file/cache]{The @racketmodname[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.}

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

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

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

@itemlist[

 @item{@racket['modify-seconds] --- the file's modification date}

 @item{@racket['size] --- the file's size in bytes}

 @item{@racket['key] --- the cache entry's key}

 @item{@racket['name] --- the cache file's name}

]

The @racket[log-error-string] and @racket[log-debug-string] functions
are used to record errors and debugging information.}


@defproc[(cache-remove [key any/c]
                       [cache-dir path-string?]
                       [#:log-error-string log-error-string (string? . -> . any)
                                           (lambda (s) (log-error s))]
                       [#:log-debug-string log-debug-string (string? . -> . any)
                                           (lambda (s) (log-debug s))])
          void?]{

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

The @racket[log-error-string] and @racket[log-debug-string] functions
are used to record errors and debugging information.}
