Skip to content

Commit 9331d30

Browse files
committed
Preparation to use (cache) for (net oauth)...
1 parent b9f466d commit 9331d30

File tree

4 files changed

+60
-7
lines changed

4 files changed

+60
-7
lines changed

sitelib/cache.scm

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
;;; -*- mode:scheme; coding: utf-8; -*-
2+
;;;
3+
;;; cache.scm - Cache
4+
;;;
5+
;;; Copyright (c) 2026 Takashi Kato <ktakashi@ymail.com>
6+
;;;
7+
;;; Redistribution and use in source and binary forms, with or without
8+
;;; modification, are permitted provided that the following conditions
9+
;;; are met:
10+
;;;
11+
;;; 1. Redistributions of source code must retain the above copyright
12+
;;; notice, this list of conditions and the following disclaimer.
13+
;;;
14+
;;; 2. Redistributions in binary form must reproduce the above copyright
15+
;;; notice, this list of conditions and the following disclaimer in the
16+
;;; documentation and/or other materials provided with the distribution.
17+
;;;
18+
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24+
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25+
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26+
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28+
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
;;;
30+
31+
#!nounbound
32+
(library (cache)
33+
(export cache?
34+
cache-put!
35+
cache-get
36+
cache-evict!
37+
cache-clear!
38+
cache-size
39+
cache-values
40+
<lru-cache> lru-cache? make-lru-cache
41+
make-simple-lru-cache)
42+
(import (cache apis)
43+
(cache lru)))

sitelib/cache/apis.scm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
#!nounbound
3232
(library (cache apis)
33-
(export <cache>
33+
(export <cache> cache?
3434
cache-put!
3535
cache-get
3636
cache-evict!
@@ -62,6 +62,8 @@
6262
(evict-strategy :init-keyword :evict-strategy :init-value #f)
6363
(on-evict :init-keyword :on-evict :init-value #f)))
6464

65+
(define (cache? o) (is-a? o <cache>))
66+
6567
(define-method initialize ((o <cache>) initargs)
6668
(call-next-method)
6769
(let ((c (slot-ref o 'comparator)))

sitelib/cache/lru.scm

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
#!nounbound
3232
(library (cache lru)
3333
(export make-simple-lru-cache
34-
<lru-cache>)
34+
<lru-cache>
35+
lru-cache?
36+
make-lru-cache)
3537
(import (rnrs)
3638
(clos user)
3739
(cache apis)
@@ -45,6 +47,10 @@
4547
(define-class <lru-cache> (<cache>)
4648
((queue :init-value '())
4749
equal?))
50+
(define (lru-cache? o) (is-a? o <lru-cache>))
51+
(define (make-lru-cache size :key (comparator default-comparator) (on-evict #f))
52+
(make <lru-cache> :evict-strategy (strategy size) :comparator comparator
53+
:on-evict on-evict))
4854

4955
(define-method initialize ((o <lru-cache>) initargs)
5056
(cond ((get-keyword :max-size initargs #f) =>
@@ -78,8 +84,7 @@
7884

7985
(define (make-simple-lru-cache size create
8086
:optional (comparator default-comparator))
81-
(define cache (make <lru-cache> :evict-strategy (strategy size)
82-
:comparator comparator))
87+
(define cache (make-lru-cache size :comparator comparator))
8388
(define mark (list 'not-found))
8489
(lambda (name)
8590
(let ((r (cache-get cache name mark)))

test/tests/cache/lru.scm

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
(import (rnrs)
2-
(cache lru)
3-
(cache apis)
2+
(cache)
43
(clos user)
54
(sagittarius regex)
65
(sagittarius comparators)
@@ -49,6 +48,8 @@
4948
(define cache (make <lru-cache> :max-size 1 :comparator eq-comparator))
5049
(define first (list 'ok))
5150
(define second (list 'ok))
51+
(test-assert (cache? cache))
52+
(test-assert "lru-cache?" (lru-cache? cache))
5253
(test-assert (cache-put! cache first 'ok))
5354
(test-equal 'ok (cache-get cache first))
5455

@@ -61,11 +62,13 @@
6162
)
6263

6364
(let ((count 0))
64-
(define cache (make <lru-cache> :max-size 2 :comparator eq-comparator
65+
(define cache (make-lru-cache 2 :comparator eq-comparator
6566
:on-evict (lambda (o) (set! count (+ count 1)))))
6667
(define first (list 'ok))
6768
(define second (list 'ok))
6869
(define third (list 'ok))
70+
(test-assert (cache? cache))
71+
(test-assert "lru-cache?" (lru-cache? cache))
6972
(cache-put! cache first '1st)
7073
(cache-put! cache second '2nd)
7174
(cache-put! cache third '3rd)

0 commit comments

Comments
 (0)