Skip to content

Instantly share code, notes, and snippets.

@rcoder
Created July 9, 2013 10:42
Show Gist options
  • Save rcoder/5956445 to your computer and use it in GitHub Desktop.
Save rcoder/5956445 to your computer and use it in GitHub Desktop.
require 'java'
Dir['target/dependency/*.jar'].each {|jar| require jar }
import com.twitter.finagle.builder.ClientBuilder
import com.twitter.finagle.memcached.protocol.text.Memcached
import com.twitter.finagle.memcached.java.Client
import com.twitter.finagle.memcached.java.ClientBase
import java.net.InetSocketAddress
import java.nio.charset.Charset
require 'forwardable'
class MemcachedClient
def initialize(host='localhost', port=11211)
codec = Memcached.new
host_list = InetSocketAddress.new(host, port)
builder = ClientBuilder.get.codec(codec).hosts(host_list)
service = ClientBuilder.safeBuild(builder)
@client = ClientBase.newInstance(service)
end
def get(key)
@client.get(key).get.toString(Charset.defaultCharset)
end
def set(key, val)
@client.set(key, val)
end
def incr(key)
@client.incr(key).get
end
def decr(key)
@client.decr(key).get
end
def delete(key)
@client.delete(key).get
end
def close
@client.release
end
end
if __FILE__ == $0
mc = MemcachedClient.new
key = "mc#{rand(1000)}.#{rand(1000)}"
mc.set(key, "testing")
puts mc.get(key)
mc.delete(key)
mc.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment