This commit is contained in:
2026-03-07 22:30:25 +01:00
parent 1fbd443dbf
commit 8e1752c7ad
2 changed files with 13 additions and 5 deletions

View File

@@ -13,6 +13,7 @@
lru-has? lru-has?
lru-add! lru-add!
lru-count lru-count
lru-empty?
lru->list lru->list
lru-clear lru-clear
) )
@@ -155,6 +156,10 @@
) )
) )
(define/contract (lru-empty? l)
(-> lru*? boolean?)
(= (lru-count l) 0))
(define/contract (lru-count lru) (define/contract (lru-count lru)
(-> lru*? larger-equal0?) (-> lru*? larger-equal0?)
(with-lock lru (with-lock lru

View File

@@ -2,7 +2,7 @@
@(require scribble/example) @(require scribble/example)
@title{LRU Cache (Least Recently Used) for Racket} @title{LRU Cache (Least Recently Used) for Racket}
@author{You} @author[@author+email["Hans Dijkema" "hans@dijkewijk.nl"]]
@defmodule[lru-cache]{ @defmodule[lru-cache]{
An in-memory LRU cache with optional item expiration. The cache keeps items in An in-memory LRU cache with optional item expiration. The cache keeps items in
@@ -87,6 +87,10 @@ Clear all items from the cache. Returns @racket[l].
Return the number of @emph{non-expired} items currently in the cache. Return the number of @emph{non-expired} items currently in the cache.
} }
@defproc[(lru-empty? [l lru?]) boolean?]{
Return @emph{#t} if the number of @emph{non-expired} items currently in the cache equals 0.
}
@defproc[(lru->list [l lru?] [#:with-expire with-expire? boolean? #f]) list?]{ @defproc[(lru->list [l lru?] [#:with-expire with-expire? boolean? #f]) list?]{
Return the cache contents in recency order (most recent first). Return the cache contents in recency order (most recent first).
When @racket[with-expire?] is @racket[#f] (default), returns just the list of items. When @racket[with-expire?] is @racket[#f] (default), returns just the list of items.
@@ -120,12 +124,11 @@ a positive integer), otherwise @racket[#f].
@section{Examples} @section{Examples}
@examples[ @#reader scribble/comment-reader
#:eval (make-base-eval) [racketblock
(require racket/base) (require racket/base)
;; Assuming your module is "lru.rkt" next to this file: (require lru-cache)
(require "../private/lru-cache.rkt")
(define L (make-lru 3)) ;; max 3 items, no expiration (define L (make-lru 3)) ;; max 3 items, no expiration
(lru-add! L 'a) (lru-add! L 'a)