55This backend implements the wst.rate-limit.store protocol using Redis hash
66entries with fields:
77 - \" count\" : current request count
8- - \" start\" : window start time (universal-time)
9-
10- Redis access is delegated to COMMAND-FN, allowing integration with any Redis
11- client library. COMMAND-FN receives command name as a string followed by
12- command arguments." )
8+ - \" start\" : window start time (universal-time)" )
139 (:export
1410 # :redis-store))
1511
@@ -20,29 +16,27 @@ command arguments.")
2016 :initform " wst:rate-limit:"
2117 :reader redis-store-key-prefix)
2218 (window-seconds :initarg :window-seconds
23- :initform nil
24- :reader redis-store-window-seconds)
19+ :initform nil
20+ :reader redis-store-window-seconds)
2521 (connection :initarg :connection
2622 :initform nil
27- :reader redis-store-connection)
28- (connection-fn :initarg :connection-fn
29- :initform (lambda (store thunk)
30- (let ((connection (redis-store-connection store)))
31- (unless connection
32- (error " No Redis connection configured. Pass a CL-REDIS connection via :connection when creating the store." ))
33- (let ((redis :*connection* connection))
34- (funcall thunk))))
35- :reader redis-store-connection-fn))
23+ :reader redis-store-connection))
3624 (:documentation " Redis-backed implementation of the rate-limit store protocol.
3725
3826Slots:
3927- KEY-PREFIX: string prefix used to namespace Redis keys.
4028- WINDOW-SECONDS: optional TTL set on each saved key via EXPIRE.
41- - CONNECTION: CL-REDIS connection object used as REDIS:*CONNECTION*.
42- - CONNECTION-FN: function called as (connection-fn store thunk) to execute Redis calls." ))
29+ - CONNECTION: CL-REDIS connection object used as REDIS:*CONNECTION*." ))
4330
44- (defun redis-store--with-connection (store thunk)
45- (funcall (redis-store-connection-fn store) store thunk))
31+ (defmacro redis-store--with-connection ((store) &body body)
32+ (let ((store-var (gensym ))
33+ (connection-var (gensym )))
34+ ` (let* ((, store-var , store)
35+ (, connection-var (redis-store-connection , store-var)))
36+ (unless , connection-var
37+ (error " No Redis connection configured. Pass a CL-REDIS connection via :connection when creating the store." ))
38+ (let ((redis :*connection* , connection-var))
39+ ,@ body))))
4640
4741(defun redis-store--key (store key)
4842 (format nil " ~a~a " (redis-store-key-prefix store) (write-to-string key :readably t )))
5852
5953(defmethod io.github.cl-sdk.wst.rate-limit.store :fetch-window ((store redis-store) key)
6054 (let* ((redis-key (redis-store--key store key))
61- (reply (redis-store--with-connection store
62- ( lambda () (redis :hmget redis-key " count" " start" ) )))
55+ (reply (redis-store--with-connection ( store)
56+ (redis :red- hmget redis-key " count" " start" )))
6357 (count-raw (and (listp reply) (first reply)))
6458 (start-raw (and (listp reply) (second reply)))
6559 (count (redis-store--integer-or-nil count-raw))
@@ -71,16 +65,15 @@ Slots:
7165(defmethod io.github.cl-sdk.wst.rate-limit.store :save-window ((store redis-store) key count start-time)
7266 (let* ((redis-key (redis-store--key store key))
7367 (ttl (redis-store-window-seconds store)))
74- (redis-store--with-connection store
75- (lambda ()
76- (redis :hmset redis-key
77- " count" (write-to-string count )
78- " start" (write-to-string start-time))))
68+ (redis-store--with-connection (store)
69+ (redis :red-hmset redis-key
70+ " count" (write-to-string count )
71+ " start" (write-to-string start-time)))
7972 (when (and ttl (plusp ttl))
80- (redis-store--with-connection store
81- ( lambda () (redis :expire redis-key ttl) )))
73+ (redis-store--with-connection ( store)
74+ (redis :red- expire redis-key ttl)))
8275 t ))
8376
84- (defmethod wst.rate-limit.store :delete-window ((store redis-store) key)
85- (redis-store--with-connection store
86- ( lambda () (redis :del (redis-store--key store key) ))))
77+ (defmethod io.github.cl-sdk. wst.rate-limit.store:delete-window ((store redis-store) key)
78+ (redis-store--with-connection ( store)
79+ (redis :red- del (redis-store--key store key))))
0 commit comments