<commit_before>
#lang scribble/doc

@(require "base.ss")

@title[#:tag "cache"]{Write-through cache}

@defmodule[(planet untyped/unlib/cache)]{

A @italic{write-through cache} is essentially a hash table that calls user supplied functions to load and store data from a @italic{back-end store} (a database, file, network resource or other). This is useful for, for example, caching database values so that writes are always sent to the database (so it is always up-to-date) but data is only loaded as needed.

The cache associates a fixed life-span to all stored data, so data in the cache will eventually be removed.  This ensures data is the cache does not get too old, and additionally that memory consumption is constrained.

@defproc[(make-cache [load-proc  (-> key value)]
                     [store-proc (-> key value void?)]
                     [#:expire expire-proc (-> cache? key value void?) void]
                     [#:lifetime lifetime natural? 3600]) cache?]{

Creates a write-through cache using the given @scheme[load-proc] and @scheme[store-proc] and @scheme[equal?] has a key comparison function.

@scheme[load-proc] is called by @scheme[cache-ref] when there is no cached value for a given key. It is passed the key and must return the corresponding value from the back-end store. @scheme[cache-ref] updates the cache with the loaded value: subsequent requests for the same key will not trigger @scheme[load-proc] until the cached value expires.

@scheme[store-proc] is called by @scheme[cache-set!]. It is passed a key and value and must update the back-end store. @scheme[cache-set!] updates the cache automatically.

The optional @scheme[expire-proc] is called immediately after a value is removed from the cache (either manually or through natural expiry). The function is passed a reference to the cache, the key and the expiring value. The default function does nothing but user-supplied functions can, for example, add the data back to the cache or re-query the back-end store.

The optional @scheme[lifetime] specifies the number of seconds that each item is stored in the cache.
<commit_msg>Fixed bug in cache.scrbl<commit_after>#lang scribble/doc

@(require "base.ss")

@title[#:tag "cache"]{Write-through cache}

@defmodule[(planet untyped/unlib/cache)]{

A @italic{write-through cache} is essentially a hash table that calls user supplied functions to load and store data from a @italic{back-end store} (a database, file, network resource or other). This is useful for, for example, caching database values so that writes are always sent to the database (so it is always up-to-date) but data is only loaded as needed.

The cache associates a fixed life-span to all stored data, so data in the cache will eventually be removed.  This ensures data is the cache does not get too old, and additionally that memory consumption is constrained.

@defproc[(make-cache [load-proc  (-> key value)]
                     [store-proc (-> key value void?)]
                     [#:expire expire-proc (-> cache? key value void?) void]
                     [#:lifetime lifetime natural? 3600]) cache?]{

Creates a write-through cache using the given @scheme[load-proc] and @scheme[store-proc] and @scheme[equal?] has a key comparison function.

@scheme[load-proc] is called by @scheme[cache-ref] when there is no cached value for a given key. It is passed the key and must return the corresponding value from the back-end store. @scheme[cache-ref] updates the cache with the loaded value: subsequent requests for the same key will not trigger @scheme[load-proc] until the cached value expires.

@scheme[store-proc] is called by @scheme[cache-set!]. It is passed a key and value and must update the back-end store. @scheme[cache-set!] updates the cache automatically.

The optional @scheme[expire-proc] is called immediately after a value is removed from the cache (either manually or through natural expiry). The function is passed a reference to the cache, the key and the expiring value. The default function does nothing but user-supplied functions can, for example, add the data back to the cache or re-query the back-end store.

The optional @scheme[lifetime] specifies the number of seconds that each item is stored in the cache.

{'fraction_non_alphanumeric': 0.07780912082745652, 'fraction_numerical': 0.0018805829807240243, 'mean_word_length': 4.041469194312796, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '886430', 'n_tokens_mistral': 1124, 'n_tokens_neox': 1087, 'n_words': 588}