Permalink
Browse files

add spymemcached jar, add initialize/set/get wrappers

  • Loading branch information...
1 parent 3125b6e commit 9346e0c2d8a989eb45317c476f3265da83c569a2 @flyerhzm flyerhzm committed Jul 19, 2012
View
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in jruby-memcached.gemspec
+gemspec
View
@@ -0,0 +1,24 @@
+PATH
+ remote: .
+ specs:
+ jruby-memcached (0.0.1)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ diff-lcs (1.1.3)
+ rspec (2.11.0)
+ rspec-core (~> 2.11.0)
+ rspec-expectations (~> 2.11.0)
+ rspec-mocks (~> 2.11.0)
+ rspec-core (2.11.1)
+ rspec-expectations (2.11.1)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.11.1)
+
+PLATFORMS
+ java
+
+DEPENDENCIES
+ jruby-memcached!
+ rspec
@@ -0,0 +1,20 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "memcached/version"
+
+Gem::Specification.new do |s|
+ s.name = "jruby-memcached"
+ s.version = Memcached::VERSION
+ s.authors = ["Richard Huang"]
+ s.email = ["flyerhzm@gmail.com"]
+ s.homepage = ""
+ s.summary = %q{jruby compatible memcached client}
+ s.description = %q{jruby memcacached client which is compatible with memcached gem}
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ s.add_development_dependency 'rspec'
+end
Binary file not shown.
View
@@ -0,0 +1,45 @@
+require 'java'
+require 'memcached/version'
+require 'java/spymemcached-2.8.2-SNAPSHOT.jar'
+
+class Memcached
+ include_class 'java.net.InetSocketAddress'
+ include_class 'net.spy.memcached.MemcachedClient'
+ include_class 'net.spy.memcached.ConnectionFactoryBuilder'
+ include_class 'net.spy.memcached.ConnectionFactoryBuilder$Locator'
+ include_class 'net.spy.memcached.DefaultHashAlgorithm'
+
+ attr_reader :default_ttl
+
+ def initialize(addresses, options={})
+ @servers = Array(addresses).map do |address|
+ host, port = address.split(":")
+ InetSocketAddress.new host, port.to_i
+ end
+ builder = ConnectionFactoryBuilder.new.setLocatorType(Locator::CONSISTENT).setHashAlg(DefaultHashAlgorithm::FNV1_32_HASH)
+ # jruby is not smart enough to use MemcachedClient(ConnectionFactory cf, List<InetSocketAddress> addrs)
+ @client = MemcachedClient.new @servers
+ # MemcachedClient has no interface to set connFactory, has to do manually
+ @client.instance_variable_set :@connFactory, builder
+
+ @default_ttl = options[:default_ttl] || 7 * 3600 # 7 days
+ end
+
+ def set(key, value, ttl=@default_ttl, marshal=true)
+ value = marshal ? Marshal.dump(value) : value
+ @client.set(key, ttl, value)
+ end
+
+ def get(key, marshal=true)
+ value = @client.get(key)
+ marshal ? Marshal.load(value) : value
+ end
+
+ def servers
+ @servers.map { |server| server.to_s[1..-1] }
+ end
+
+ def close
+ @client.shutdown
+ end
+end
@@ -0,0 +1,3 @@
+class Memcached
+ VERSION = "0.0.1"
+end
@@ -0,0 +1,29 @@
+require 'spec_helper'
+
+describe Memcached do
+ context "stub memcached" do
+ before { Java::NetSpyMemcached::MemcachedClient.should_receive(:new) }
+ subject { Memcached.new(["10.0.0.1:11211", "10.0.0.2:11211"], :default_ttl => 100) }
+ its(:servers) { should == ["10.0.0.1:11211", "10.0.0.2:11211"] }
+ its(:default_ttl) { should == 100 }
+ end
+
+ context "localhost" do
+ before :all do
+ @memcached = Memcached.new("127.0.0.1:11211")
+ end
+
+ after :all do
+ @memcached.close
+ end
+
+ it "should initialize with options" do
+ @memcached.servers.should == ["127.0.0.1:11211"]
+ end
+
+ it "should set/get" do
+ @memcached.set("hello", "world")
+ @memcached.get("hello").should == "world"
+ end
+ end
+end
View
@@ -0,0 +1,6 @@
+require 'rubygems'
+require 'bundler/setup'
+require 'rspec'
+require 'rspec/autorun'
+
+require 'memcached'

0 comments on commit 9346e0c

Please sign in to comment.