blob: 7dfd375d92f580506725400ba85bc4693f8400d7 (
about) (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
;;; Guile-Debbugs --- Guile bindings for Debbugs
;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
;;;
;;; This file is part of Guile-Debbugs.
;;;
;;; Guile-Debbugs is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License as
;;; published by the Free Software Foundation; either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; Guile-Debbugs is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Guile-Debbugs. If not, see <http://www.gnu.org/licenses/>.
(define-module (debbugs cache)
#:use-module (debbugs config)
#:use-module (ice-9 match)
#:export (cached? cache! forget! forget-all!))
(define %cache (make-hash-table))
(define (cached? key)
"Return the value matching KEY from the cache if it has not yet
expired or return #F."
(let ((t (current-time)))
(match (hash-ref %cache key)
((#:expires time #:value value)
(if (< t time) value #f))
(_ #f))))
(define* (cache! key value
#:optional (ttl (config 'cache-ttl)))
"Store VALUE for the given KEY and mark it to expire after TTL
seconds."
(let ((t (current-time)))
(hash-set! %cache key `(#:expires ,(+ t ttl) #:value ,value))
value))
(define (forget! key)
"Delete KEY from the cache."
(hash-remove! %cache key))
(define (forget-all!)
"Reset the cache."
(set! %cache (make-hash-table)))
|