jruby-memcached 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jruby-memcached (0.0.1)
4
+ jruby-memcached (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Richard Huang (flyerhzm@gmail.com)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,7 +1,45 @@
1
- jruby-memcached
2
- ===============
1
+ # jruby-memcached
3
2
 
4
- we used memcached gem before, but now we are migrating to jruby, but
5
- memcached didn't support jruby platform, and we can't find any
6
- ruby/jruby gem that compatible with memcached, have to write it by
7
- ourselves.
3
+ A jruby memcached gem which is compatible with evan's [memcached][0] gem
4
+
5
+ ## Install
6
+
7
+ gem install jruby-memcached
8
+
9
+ ## Usage
10
+
11
+ Now, in Ruby, require the library and instantiate a Memcached object at
12
+ a global level:
13
+
14
+ require 'memcached'
15
+ $cache = Memcached.new("localhost:11211")
16
+
17
+ Now you can set things and get things:
18
+
19
+ value = 'hello'
20
+ $cache.set 'test', value
21
+ $cache.get 'test' #=> "hello"
22
+
23
+ You can set with an expiration timeout:
24
+
25
+ value = 'hello'
26
+ $cache.set 'test', value, 1
27
+ sleep(2)
28
+ $cache.get 'test' #=> raises Memcached::NotFound
29
+
30
+ You can get multiple values at once:
31
+
32
+ value = 'hello'
33
+ $cache.set 'test', value
34
+ $cache.set 'test2', value
35
+ $cache.get ['test', 'test2', 'missing']
36
+ #=> {"test" => "hello", "test2" => "hello"}
37
+
38
+ ## Benchmarks
39
+
40
+ memcached.gem is the fastest memcached gem in MRI,
41
+ jruby-memcached is the fastest memcached gem in JRuby.
42
+ See [benchmark][1]
43
+
44
+ [0]:"https://github.com/evan/memcached"
45
+ [1]:"https://github.com/aurorafeint/jruby-memcached/blob/master/benchmark.rb"
data/benchmark.rb CHANGED
@@ -17,7 +17,7 @@ dalli = Dalli::Client.new(['localhost:11211'])
17
17
  Benchmark.bm(30) {|bm|
18
18
  if JRUBY
19
19
  bm.report("jruby-memcached set") {
20
- 100_000.times { memcached.set('foo', 'bar').get }
20
+ 100_000.times { memcached.set('foo', 'bar') }
21
21
  }
22
22
  bm.report("jruby-memcached get") {
23
23
  100_000.times { memcached.get('foo') }
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
7
7
  s.version = Memcached::VERSION
8
8
  s.authors = ["Richard Huang"]
9
9
  s.email = ["flyerhzm@gmail.com"]
10
- s.homepage = ""
10
+ s.homepage = "https://github.com/aurorafeint/jruby-memcached"
11
11
  s.summary = %q{jruby compatible memcached client}
12
12
  s.description = %q{jruby memcacached client which is compatible with memcached gem}
13
13
 
@@ -1,3 +1,4 @@
1
1
  class Memcached::Error < RuntimeError; end
2
2
  class Memcached::NotFound < Memcached::Error; end
3
3
  class Memcached::NotStored < Memcached::Error; end
4
+ class Memcached::NotSupport < Memcached::Error; end
@@ -1,3 +1,3 @@
1
1
  class Memcached
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/memcached.rb CHANGED
@@ -1,36 +1,55 @@
1
- require 'java'
1
+ require 'java'
2
2
  require 'memcached/version'
3
3
  require 'memcached/exceptions'
4
4
  require File.join(File.dirname(__FILE__), '../target/spymemcached-ext-0.0.1.jar')
5
5
 
6
+ java_import 'net.spy.memcached.MemcachedClient'
7
+ java_import 'net.spy.memcached.ConnectionFactoryBuilder'
8
+ java_import 'net.spy.memcached.ConnectionFactoryBuilder$Locator'
9
+ java_import 'net.spy.memcached.ConnectionFactoryBuilder$Protocol'
10
+ java_import 'net.spy.memcached.DefaultHashAlgorithm'
11
+ java_import 'net.spy.memcached.transcoders.SimpleTranscoder'
12
+ java_import 'net.spy.memcached.AddrUtil'
13
+
6
14
  class Memcached
7
- include_class 'java.net.InetSocketAddress'
8
- include_class 'net.spy.memcached.MemcachedClient'
9
- include_class 'net.spy.memcached.ConnectionFactoryBuilder'
10
- include_class 'net.spy.memcached.ConnectionFactoryBuilder$Locator'
11
- include_class 'net.spy.memcached.DefaultHashAlgorithm'
12
- include_class 'net.spy.memcached.FailureMode'
13
- include_class 'net.spy.memcached.transcoders.SimpleTranscoder'
14
- include_class 'net.spy.memcached.AddrUtil'
15
15
 
16
16
  FLAGS = 0x0
17
17
  DEFAULTS = {
18
+ :binary_protocol => false,
18
19
  :default_ttl => 604800,
19
- :exception_retry_limit => 5
20
+ :distribution => :consistent_ketama,
21
+ :exception_retry_limit => 5,
22
+ :hash => :fnv1_32,
20
23
  }
21
24
 
22
25
  attr_reader :options
23
26
  attr_reader :default_ttl
24
27
 
25
28
  def initialize(addresses, options={})
26
- builder = ConnectionFactoryBuilder.new.
27
- setLocatorType(Locator::CONSISTENT).
28
- setHashAlg(DefaultHashAlgorithm::FNV1_32_HASH)
29
- @client = MemcachedClient.new builder.build, AddrUtil.getAddresses(Array(addresses).join(' '))
30
-
31
29
  @options = DEFAULTS.merge(options)
32
30
  @default_ttl = @options[:default_ttl]
33
31
 
32
+ builder = ConnectionFactoryBuilder.new
33
+
34
+ @options[:distribution] = :consistent if @options[:distribution] == :ketama || @options[:distribution] == :consistent_ketama
35
+ unless [:array_mod, :consistent].include? @options[:distribution]
36
+ raise Memcached::NotSupport.new("#{@options[:distribution]} distribution algorithm doesn't support yet.")
37
+ end
38
+ distribution_algorithm = Locator.const_get @options[:distribution].to_s.upcase
39
+ builder.setLocatorType(distribution_algorithm)
40
+
41
+ if @options[:binary_protocol]
42
+ builder.setProtocol(Protocol::BINARY)
43
+ end
44
+
45
+ unless [:native, :crc, :fnv1_64, :fnv1a_64, :fnv1_32, :fnv1a_32, :ketama].include? @options[:hash]
46
+ raise Memcached::NotSupport.new("#{@options[:hash]} hash algorithm doesn't support yet.")
47
+ end
48
+ hash_algorithm = DefaultHashAlgorithm.const_get "#{@options[:hash]}_HASH".upcase
49
+ builder.setHashAlg hash_algorithm
50
+
51
+ @client = MemcachedClient.new builder.build, AddrUtil.getAddresses(Array(addresses).join(' '))
52
+
34
53
  @simple_transcoder = SimpleTranscoder.new
35
54
  end
36
55
 
@@ -38,7 +57,7 @@ class Memcached
38
57
  with_retry do
39
58
  value = encode(value, marshal, flags)
40
59
  @simple_transcoder.setFlags(flags)
41
- @client.set(key, ttl, value.to_java_bytes, @simple_transcoder)
60
+ @client.set(key, ttl, value.to_java_bytes, @simple_transcoder).get
42
61
  end
43
62
  end
44
63
 
@@ -2,16 +2,40 @@ require 'spec_helper'
2
2
 
3
3
  describe Memcached do
4
4
  context "localhost" do
5
- before :all do
6
- @memcached = Memcached.new("127.0.0.1:11211")
7
- end
5
+ before(:all) { @memcached = Memcached.new("127.0.0.1:11211") }
6
+ after(:all) { @memcached.close }
8
7
 
9
- after :all do
10
- @memcached.close
8
+ it "should get all servers" do
9
+ @memcached.servers.should == ["127.0.0.1:11211"]
11
10
  end
12
11
 
13
- it "should initialize with options" do
14
- @memcached.servers.should == ["127.0.0.1:11211"]
12
+ context "initialize" do
13
+ before(:all) do
14
+ @memcached_with_options = Memcached.new("127.0.0.1:11211", :hash => :fnv1_32,
15
+ :distribution => :ketama,
16
+ :default_ttl => 900)
17
+ end
18
+ after(:all) { @memcached_with_options.close }
19
+
20
+ it "should with option hash" do
21
+ @memcached_with_options.options[:hash].should == :fnv1_32
22
+ end
23
+
24
+ it "should with option distribution" do
25
+ @memcached_with_options.options[:distribution].should == :consistent
26
+ end
27
+
28
+ it "should raise error with unsupported option hash" do
29
+ lambda { Memcached.new("127.0.0.1:11211", :hash => :unknown) }.should raise_error(Memcached::NotSupport)
30
+ end
31
+
32
+ it "should raise error with unsupported option distribution" do
33
+ lambda { Memcached.new("127.0.0.1:11211", :distribution => :unknown) }.should raise_error(Memcached::NotSupport)
34
+ end
35
+
36
+ it "should get default ttl" do
37
+ @memcached_with_options.default_ttl.should == 900
38
+ end
15
39
  end
16
40
 
17
41
  context "set/get" do
@@ -32,14 +32,11 @@ public class SimpleTranscoder implements Transcoder<Object> {
32
32
  }
33
33
 
34
34
  public CachedData encode(Object o) {
35
- byte[] b = (byte[]) o;
36
- return new CachedData(getFlags(), b, getMaxSize());
35
+ return new CachedData(getFlags(), (byte[]) o, getMaxSize());
37
36
  }
38
37
 
39
38
  public Object decode(CachedData d) {
40
- byte[] data = d.getData();
41
- int flags = d.getFlags();
42
- return new ReturnData(flags, data);
39
+ return new ReturnData(d.getFlags(), d.getData());
43
40
  }
44
41
 
45
42
  public int getMaxSize() {
Binary file
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: jruby-memcached
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.0
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Richard Huang
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-07-24 00:00:00 Z
13
+ date: 2012-07-29 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
@@ -47,6 +47,7 @@ files:
47
47
  - .gitignore
48
48
  - Gemfile
49
49
  - Gemfile.lock
50
+ - MIT-LICENSE
50
51
  - README.md
51
52
  - Rakefile
52
53
  - benchmark.rb
@@ -62,7 +63,7 @@ files:
62
63
  - src/main/java/net/spy/memcached/transcoders/SimpleTranscoder.java
63
64
  - src/main/java/net/spy/memcached/util/DefaultKetamaNodeLocatorConfiguration.java
64
65
  - target/spymemcached-ext-0.0.1.jar
65
- homepage: ""
66
+ homepage: https://github.com/aurorafeint/jruby-memcached
66
67
  licenses: []
67
68
 
68
69
  post_install_message:
@@ -85,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
86
  requirements: []
86
87
 
87
88
  rubyforge_project:
88
- rubygems_version: 1.8.24
89
+ rubygems_version: 1.8.15
89
90
  signing_key:
90
91
  specification_version: 3
91
92
  summary: jruby compatible memcached client