summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile.am1
-rw-r--r--mumi/cache.scm48
-rw-r--r--mumi/config.scm.in1
-rw-r--r--mumi/debbugs.scm2
-rw-r--r--mumi/messages.scm2
5 files changed, 52 insertions, 2 deletions
diff --git a/Makefile.am b/Makefile.am
index f1319a1..0d68fe0 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -45,6 +45,7 @@ SOURCES = \
mumi/web/view/html.scm \
mumi/web/view/utils.scm \
mumi/bugs.scm \
+ mumi/cache.scm \
mumi/messages.scm \
mumi/jobs.scm \
mumi/send-email.scm \
diff --git a/mumi/cache.scm b/mumi/cache.scm
new file mode 100644
index 0000000..13b21f9
--- /dev/null
+++ b/mumi/cache.scm
@@ -0,0 +1,48 @@
+;;; mumi -- Mediocre, uh, mail interface
+;;; Copyright © 2020 Ricardo Wurmus <rekado@elephly.net>
+;;;
+;;; This program is free software: you can redistribute it and/or
+;;; modify it under the terms of the GNU Affero General Public License
+;;; as published by the Free Software Foundation, either version 3 of
+;;; the License, or (at your option) any later version.
+;;;
+;;; This program 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
+;;; Affero General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU Affero General Public
+;;; License along with this program. If not, see
+;;; <http://www.gnu.org/licenses/>.
+
+(define-module (mumi cache)
+ #:use-module (mumi 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)))
diff --git a/mumi/config.scm.in b/mumi/config.scm.in
index 1d16169..58b9a36 100644
--- a/mumi/config.scm.in
+++ b/mumi/config.scm.in
@@ -74,6 +74,7 @@
dir)))))
(host . "localhost")
(port . 1234)
+ (cache-ttl . 120)
(submission-email-address . "guix-patches@gnu.org")
(submission-bug-email-address . "bug-guix@gnu.org")
(lists . ("guix-patches@gnu.org" "bug-guix@gnu.org"))
diff --git a/mumi/debbugs.scm b/mumi/debbugs.scm
index 005d16b..1f6d430 100644
--- a/mumi/debbugs.scm
+++ b/mumi/debbugs.scm
@@ -17,7 +17,7 @@
(define-module (mumi debbugs)
#:use-module (mumi config)
- #:use-module (debbugs cache)
+ #:use-module (mumi cache)
#:use-module (email email)
#:use-module (email quoted-printable)
#:use-module (rnrs bytevectors)
diff --git a/mumi/messages.scm b/mumi/messages.scm
index f816cae..3648eb5 100644
--- a/mumi/messages.scm
+++ b/mumi/messages.scm
@@ -26,8 +26,8 @@
#:use-module (ice-9 textual-ports)
#:use-module (ice-9 binary-ports)
#:use-module (ice-9 threads)
- #:use-module (debbugs cache)
#:use-module (email email)
+ #:use-module (mumi cache)
#:use-module (mumi config)
#:use-module (mumi debbugs)
#:use-module (mumi xapian)