Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit 18b996790de346dad1ddf29d9e063a4986239a4e @watsonian committed Jul 25, 2010
Showing with 40 additions and 0 deletions.
  1. +15 −0 README.md
  2. +25 −0 memcache_do.rb
@@ -0,0 +1,15 @@
+# MemcacheDo
+
+This is a lightweight library that lets you execute ad hoc memcache commands to a designated host and port.
+
+## Usage
+
+In its most simple form, you can use it like this:
+
+ MemcacheDo.exec('stats')
+
+By default, it uses 'localhost:11211' as the host and port to connect to, but if you need to specify a host or port:
+
+ MemcacheDo.exec('stats', '1.2.3.4', '11222')
+
+The command is executed exactly as you put it in except that a '\r\n' is appended to the end.
@@ -0,0 +1,25 @@
+require 'socket'
+
+class MemcacheDo
+ def self.exec(command, hostname="localhost", port="11211")
+ begin
+ data = ''
+ sock = TCPSocket.new(hostname, port)
+ sock.print("#{command}\r\n")
+ sock.flush
+ # memcached does not close the socket once it is done writing
+ # the stats data. We need to read line by line until we detect
+ # the END line and then stop/close on our side.
+ stats = sock.gets
+ while true
+ data += stats
+ break if stats.strip == 'END'
+ stats = sock.gets
+ end
+ sock.close
+ data
+ rescue => e
+ puts e
+ end
+ end
+end

0 comments on commit 18b9967

Please sign in to comment.