Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

use xmemcached

  • Loading branch information...
commit f49e5520d0bf6542587f965f14ebdfbe49ec473c 1 parent ceaae20
@flyerhzm flyerhzm authored
View
1  .gitignore
@@ -22,3 +22,4 @@ target/maven-archiver
target/surefire
target/dependency-reduced-pom.xml
target/original-spymemcached-ext-0.0.1.jar
+target/original-xmemcached-ext-0.0.1.jar
View
35 lib/memcached.rb
@@ -1,15 +1,14 @@
require 'java'
require 'memcached/version'
require 'memcached/exceptions'
-require File.join(File.dirname(__FILE__), '../target/spymemcached-ext-0.0.1.jar')
+require File.join(File.dirname(__FILE__), '../target/xmemcached-ext-0.0.1.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'
- include_class 'net.spy.memcached.FailureMode'
+ include_class 'net.rubyeye.xmemcached.XMemcachedClientBuilder'
+ include_class 'net.rubyeye.xmemcached.utils.AddrUtil'
+ include_class 'net.rubyeye.xmemcached.impl.LibmemcachedMemcachedSessionLocator'
+ include_class 'net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator'
+ include_class 'net.rubyeye.xmemcached.HashAlgorithm'
include_class 'com.openfeint.memcached.transcoders.SimpleTranscoder'
FLAGS = 0x0
@@ -22,15 +21,11 @@ class Memcached
attr_reader :default_ttl
def initialize(addresses, options={})
+ builder = XMemcachedClientBuilder.new AddrUtil.getAddresses(Array(addresses).join(' '))
+ builder.setSessionLocator(LibmemcachedMemcachedSessionLocator.new(100, HashAlgorithm::FNV1_32_HASH))
+ @client = builder.build
+
@options = DEFAULTS.merge(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)
- @client = MemcachedClient.new builder.build, @servers
@default_ttl = @options[:default_ttl]
@flags = @options[:flags]
@@ -50,7 +45,7 @@ def add(key, value, ttl=@default_ttl, marshal=true, flags=FLAGS)
with_retry do
value = encode(value, marshal, flags)
@simple_transcoder.setFlags(flags)
- if @client.add(key, ttl, value.to_java_bytes, @simple_transcoder).get === false
+ if @client.add(key, ttl, value.to_java_bytes, @simple_transcoder) === false
raise Memcached::NotStored
end
end
@@ -60,7 +55,7 @@ def replace(key, value, ttl=@default_ttl, marshal=true, flags=FLAGS)
with_retry do
value = encode(value, marshal, flags)
@simple_transcoder.setFlags(flags)
- if @client.replace(key, ttl, value.to_java_bytes, @simple_transcoder).get === false
+ if @client.replace(key, ttl, value.to_java_bytes, @simple_transcoder) === false
raise Memcached::NotStored
end
end
@@ -68,7 +63,7 @@ def replace(key, value, ttl=@default_ttl, marshal=true, flags=FLAGS)
def delete(key)
with_retry do
- raise Memcached::NotFound if @client.delete(key).get === false
+ raise Memcached::NotFound if @client.delete(key) === false
end
end
@@ -82,10 +77,6 @@ def get(key, marshal=true)
end
end
- def servers
- @servers.map { |server| server.to_s[1..-1] }
- end
-
def close
@client.shutdown
end
View
33 pom.xml
@@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.openfeint</groupId>
- <artifactId>spymemcached-ext</artifactId>
+ <artifactId>xmemcached-ext</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
@@ -16,8 +16,8 @@
<repositories>
<repository>
- <id>spymemcached</id>
- <name>spymemcached extention</name>
+ <id>xmemcached</id>
+ <name>xmemcached extention</name>
<layout>default</layout>
<url>http://files.couchbase.com/maven2/</url>
<snapshots>
@@ -28,21 +28,30 @@
<dependencies>
<dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- <dependency>
<groupId>org.jruby</groupId>
<artifactId>jruby</artifactId>
<version>1.6.7.2</version>
</dependency>
<dependency>
- <groupId>spy</groupId>
- <artifactId>spymemcached</artifactId>
- <version>2.8.1</version>
+ <groupId>com.googlecode.xmemcached</groupId>
+ <artifactId>xmemcached</artifactId>
+ <version>1.3.8</version>
</dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>1.5.6</version>
+ </dependency>
+ <dependency>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ <version>1.2.16</version>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ <version>1.5.6</version>
+ </dependency>
</dependencies>
<build>
View
9 spec/memcached_spec.rb
@@ -1,13 +1,6 @@
require 'spec_helper'
describe Memcached do
- context "stub memcached" do
- before { Java::NetSpyMemcached::MemcachedClient.expects(: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")
@@ -54,7 +47,7 @@
end
it "should retry when set failure" do
- Java::NetSpyMemcached::MemcachedClient.any_instance.stubs(:set).raises(Memcached::NotStored)
+ Java::NetRubyeyeXMemcached::XMemcachedClient.any_instance.stubs(:set).raises(Memcached::NotStored)
Java::ComOpenfeintMemcachedTranscoders::SimpleTranscoder.any_instance.expects(:setFlags).times(6)
lambda { @memcached.set "key", "value" }.should raise_error(Memcached::NotStored)
end
View
36 src/main/java/com/openfeint/memcached/transcoders/SimpleTranscoder.java
@@ -1,7 +1,8 @@
package com.openfeint.memcached.transcoders;
-import net.spy.memcached.CachedData;
-import net.spy.memcached.transcoders.Transcoder;
+import net.rubyeye.xmemcached.transcoders.CachedData;
+import net.rubyeye.xmemcached.transcoders.CompressionMode;
+import net.rubyeye.xmemcached.transcoders.Transcoder;
import com.openfeint.memcached.ReturnData;
@@ -12,40 +13,25 @@
*
*/
public class SimpleTranscoder implements Transcoder<Object> {
- private final int maxSize;
private int flags;
- public SimpleTranscoder() {
- this(CachedData.MAX_SIZE, 0);
- }
-
- public SimpleTranscoder(int flags) {
- this(CachedData.MAX_SIZE, flags);
- }
-
- public SimpleTranscoder(int maxSize, int flags) {
- this.maxSize = maxSize;
- this.flags = flags;
- }
-
- public boolean asyncDecode(CachedData d) {
- return false;
- }
-
public CachedData encode(Object o) {
byte[] b = (byte[]) o;
- return new CachedData(getFlags(), b, getMaxSize());
+ return new CachedData(getFlags(), b);
}
public Object decode(CachedData d) {
byte[] data = d.getData();
- int flags = d.getFlags();
+ int flags = d.getFlag();
return new ReturnData(flags, data);
}
- public int getMaxSize() {
- return maxSize;
- }
+ public void setPrimitiveAsString(boolean primitiveAsString) { }
+ public void setPackZeros(boolean packZeros) { }
+ public void setCompressionThreshold(int to) { }
+ public void setCompressionMode(CompressionMode compressMode) { }
+ public boolean isPrimitiveAsString() { return false; }
+ public boolean isPackZeros() { return false; }
public int getFlags() {
return flags;
View
BIN  target/spymemcached-ext-0.0.1.jar
Binary file not shown
View
BIN  target/xmemcached-ext-0.0.1.jar
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.