-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemcache.el
221 lines (191 loc) · 8.36 KB
/
memcache.el
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
;;; memcache.el --- A toy implementation of the Memcache client protocol
;; Copyright (c) 2009 Thomas Munro
;; Author: Thomas Munro <[email protected]>
;; Keywords: memcache, network, protocol
;; This program 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.
;; 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 General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; This is a toy implementation of the Memcache protocol[1]. It is
;; very, very pre-alpha, and probably doesn't handle many situations
;; correctly, it's just a minimal implementation which helped me out
;; when I was doing some testing and wanted to interact with my Memcache
;; daemon from the IELM REPL. Might grow into something more solid
;; some future rainy day...
;; The following systems apparently support the Memcache protocol:
;;
;; * memcached (the original)
;; * Tokyo Tyrant
;; * LightCloud
;; * MemcacheDB
;;
;; and probably more.
;; Examples of interactive usage:
;;
;; ELISP> (memcache-connect-default "my-other-machine")
;; #<process stream>
;; ELISP> (memcache-set "test" "boo")
;; "STORED"
;; ELISP> (memcache-get "test")
;; "boo"
;; ELISP> (memcache-stats)
;; (("pid" "1404")
;; ("uptime" "1960")
;; ("time" "1263643028")
;; ("version" "1.2.2")
;; ("pointer_size" "64")
;; ... blah blah ...
;;
;; ELISP> (memcache-close-default)
;; nil
;;
;; Example of usage in a program that doesn't want to hold onto
;; a connection for a long time, using a scoped connection:
;;
;; (memcache-with-connection ("my-other-machine")
;; (memcache-set "foo" "bar"))
;;
;; You can also provide connection objects explicitly to MEMCACHE-SET
;; and MEMCACHE-GET, useful for talking to multiple Memcache daemons
;; at the same time.
;; TODO:
;; * fix the blocking reads! not really stable without this
;; * support various other operations
;; * proper error handling
;; * learn about bytes versus characters in modern Emacsen
;; * support 'flags'
;; * support non-blocking callback style operations (more Emacsy)
;;
;; [1] http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt
;;; History:
;;
;;; Code:
(require 'bio)
(require 'cl)
(defvar memcache-default-host "localhost"
"The default host used by connect functions if you don't provide one.")
(defvar memcache-default-port 11211
"The default port used by connect functions if you don't provide one.")
(defvar memcache-current-connection nil
"A dynamic variable for the default connection.
This is optional -- you can provide a CONNECTION argument to all
functions, or you can leave it off if you want to use this
default argument. The idea is to manage this variable are with
MEMCACHE-CONNECT-DEFAULT, MEMCACHE-CLOSE-DEFAULT and (for
programs) MEMCACHE-WITH-CONNECTION.")
(defconst memcache-key-pattern ".*" ;; TODO
"The regular expression for testing if keys are valid.")
(defvar memcache-timeout 2
"Time to wait for a response from server before signalling an error.")
(defconst memcache-delimiter "\r\n"
"The CP/M-style line delimiter string used by Memcache.")
(defun* memcache-connect (&optional (host memcache-default-host)
(port memcache-default-port))
"Create a connection to a Memcache server (or similar) on HOST and PORT."
;; TODO buffer name should be unique for each connection!
(let ((buffer-name (format "*memcache-stream*")))
(when (get-buffer buffer-name)
(kill-buffer buffer-name))
(with-current-buffer (get-buffer-create buffer-name)
(set-buffer-multibyte nil))
(make-network-process :host host
:service port
:coding 'binary
:name buffer-name
:buffer buffer-name)))
(defun memcache-close (connection)
"Close CONNECTION, opened with MEMCACHE-CONNECT."
(delete-process connection))
(defun* memcache-connect-default (&optional (host memcache-default-host)
(port memcache-default-port))
"Connect to HOST on port PORT, setting the default connection.
This is mainly intended for interactive use in the REPL, as programs
probably shouldn't be using this global connection."
(setq memcache-current-connection (memcache-connect host port)))
(defun memcache-close-default ()
"Close the default connection that was created by MEMCACHE-CONNECT-DEFAULT."
(memcache-close memcache-current-connection))
(defmacro* memcache-with-connection ((&optional (host memcache-default-host)
(port memcache-default-port))
&rest rest)
"A syntax for creating a lexically scoped default connection and making
it the default while executing the body."
`(progn
(let ((memcache-current-connection (memcache-connect ,host ,port)))
(unwind-protect
(progn ,@rest)
(memcache-close memcache-current-connection)))))
(defun memcache-read-line (connection)
"Read one line synchronously from CONNECTION."
(or (bio-read-record-blocking (process-buffer connection)
memcache-delimiter
memcache-timeout)
;; TODO disconnect?
(error "memcache-read-line -- timed out waiting for response")))
(defun memcache-read-bytes (connection bytes)
"Read from CONNECTION a block of BYTES contiguous bytes, into a string."
(or (bio-read-bytes-blocking (process-buffer connection)
bytes
memcache-timeout)
;; TODO disconnect?
(error "memcache-read-bytes -- timed out waiting for data")))
(defun memcache-check-key (key)
"Check if KEY is a valid key."
(unless (string-match memcache-key-pattern key)
(error "Memcache-check-key -- invalid key")))
(defun* memcache-set (key
data
&optional
(flags 0)
(expiry 0)
(connection memcache-current-connection))
"Stores KEY and DATA in the server."
(memcache-check-key key)
(process-send-string connection
(format "set %s %d %d %d\r\n"
key
flags
expiry
(length data)))
(process-send-string connection data)
(process-send-string connection "\r\n")
;; we return the raw line coming back from the daemon -- maybe we
;; should return T if the result is "STORED", or raise an error
;; if the result is anything else?
(memcache-read-line connection))
(defun* memcache-get (key &optional (connection memcache-current-connection))
"Retrieves the value associated with KEY from CONNECTION."
(memcache-check-key key)
(process-send-string (or connection memcache-current-connection)
(format "get %s\r\n" key))
(let ((response (memcache-read-line connection)))
(cond ((string-match "^VALUE \\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\)"
response)
(if (string= key (match-string 1 response))
(let* ((size (string-to-number (match-string 3 response)))
(data (memcache-read-bytes connection size)))
(memcache-read-line connection) ;; blank line
(let ((response (memcache-read-line connection)))
(unless (string= response "END")
(error "Expected END, got %s" response))
data))))
((string-match "^END$" response)
nil)
(t
(error "Unexpected response %s" response)))))
(defun* memcache-stats (&optional (connection memcache-current-connection))
"Retreive server statistics from CONNECTION."
(process-send-string connection "stats\r\n")
(loop for line = (memcache-read-line connection)
while (string-match "^STAT \\([^ ]+\\) \\([^ ]+\\)" line)
collect (list (match-string 1 line) (match-string 2 line))))
(provide 'memcache)
;;; memcache.el ends here