jruby-memcached 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,34 @@
1
+ package com.openfeint.memcached;
2
+
3
+ import org.jruby.Ruby;
4
+ import org.jruby.RubyClass;
5
+ import org.jruby.runtime.ObjectAllocator;
6
+ import org.jruby.runtime.builtin.IRubyObject;
7
+ import org.jruby.runtime.load.BasicLibraryService;
8
+
9
+ import java.io.IOException;
10
+
11
+ public class MemcachedService implements BasicLibraryService {
12
+ public boolean basicLoad(final Ruby ruby) throws IOException {
13
+ RubyClass memcached = ruby.defineClass("Memcached", ruby.getObject(), new ObjectAllocator() {
14
+ public IRubyObject allocate(Ruby ruby, RubyClass klazz) {
15
+ return new Memcached(ruby, klazz);
16
+ }
17
+ });
18
+ memcached.defineAnnotatedMethods(Memcached.class);
19
+
20
+ RubyClass rails = memcached.defineClassUnder("Rails", memcached, new ObjectAllocator() {
21
+ public IRubyObject allocate(Ruby ruby, RubyClass klazz) {
22
+ return new Rails(ruby, klazz);
23
+ }
24
+ });
25
+ rails.defineAnnotatedMethods(Rails.class);
26
+
27
+ RubyClass runtimeError = ruby.getRuntimeError();
28
+ RubyClass memcachedError = memcached.defineClassUnder("Error", runtimeError, runtimeError.getAllocator());
29
+ memcached.defineClassUnder("NotFound", memcachedError, memcachedError.getAllocator());
30
+ memcached.defineClassUnder("NotStored", memcachedError, memcachedError.getAllocator());
31
+ memcached.defineClassUnder("NotSupport", memcachedError, memcachedError.getAllocator());
32
+ return true;
33
+ }
34
+ }
@@ -0,0 +1,238 @@
1
+ package com.openfeint.memcached;
2
+
3
+ import org.jruby.Ruby;
4
+ import org.jruby.RubyArray;
5
+ import org.jruby.RubyBoolean;
6
+ import org.jruby.RubyClass;
7
+ import org.jruby.RubyHash;
8
+ import org.jruby.RubyFixnum;
9
+ import org.jruby.RubyObject;
10
+ import org.jruby.RubyString;
11
+ import org.jruby.anno.JRubyClass;
12
+ import org.jruby.anno.JRubyMethod;
13
+ import org.jruby.exceptions.RaiseException;
14
+ import org.jruby.runtime.Block;
15
+ import org.jruby.runtime.ThreadContext;
16
+ import org.jruby.runtime.builtin.IRubyObject;
17
+
18
+ import java.util.ArrayList;
19
+ import java.util.List;
20
+
21
+ @JRubyClass(name = "Memcached::Rails", parent = "Memcached")
22
+ public class Rails extends Memcached {
23
+ private boolean stringReturnTypes;
24
+
25
+ public Rails(final Ruby ruby, RubyClass rubyClass) {
26
+ super(ruby, rubyClass);
27
+
28
+ stringReturnTypes = false;
29
+ }
30
+
31
+ @JRubyMethod(name = "initialize", rest = true)
32
+ public IRubyObject initialize(ThreadContext context, IRubyObject[] args) {
33
+ Ruby ruby = context.getRuntime();
34
+ RubyHash opts;
35
+ if (args[args.length - 1] instanceof RubyHash) {
36
+ opts = (RubyHash) args[args.length - 1];
37
+ } else {
38
+ opts = new RubyHash(ruby);
39
+ }
40
+ List<String> servers = new ArrayList<String>();
41
+ for (IRubyObject arg : args) {
42
+ if (arg instanceof RubyString) {
43
+ servers.add(arg.toString());
44
+ } else if (arg instanceof RubyArray) {
45
+ servers.addAll((List<String>) arg.convertToArray());
46
+ }
47
+ }
48
+ if (servers.isEmpty()) {
49
+ IRubyObject serverNames = (IRubyObject) opts.get(ruby.newSymbol("servers"));
50
+ servers.addAll((List<String>) serverNames.convertToArray());
51
+ }
52
+ if (opts.containsKey(ruby.newSymbol("namespace"))) {
53
+ opts.put(ruby.newSymbol("prefix_key"), opts.get(ruby.newSymbol("namespace")));
54
+ }
55
+ if (opts.containsKey(ruby.newSymbol("namespace_separator"))) {
56
+ opts.put(ruby.newSymbol("prefix_delimiter"), opts.get(ruby.newSymbol("namespace_separator")));
57
+ }
58
+ if (opts.containsKey(ruby.newSymbol("string_return_types"))) {
59
+ stringReturnTypes = true;
60
+ }
61
+ return super.init(context, servers, opts);
62
+ }
63
+
64
+ @JRubyMethod(name = "active?")
65
+ public IRubyObject active_p(ThreadContext context) {
66
+ Ruby ruby = context.getRuntime();
67
+ if (((RubyArray) super.servers(context)).isEmpty()) {
68
+ return ruby.getFalse();
69
+ }
70
+ return ruby.getTrue();
71
+ }
72
+
73
+ @JRubyMethod(name = { "get", "[]" }, required = 1, optional = 1)
74
+ public IRubyObject get(ThreadContext context, IRubyObject[] args) {
75
+ Ruby ruby = context.getRuntime();
76
+ IRubyObject key = args[0];
77
+ RubyBoolean notRaw = notRaw(context, args, 1);
78
+ try {
79
+ return super.get(context, new IRubyObject[] { key, notRaw });
80
+ } catch (RaiseException e) {
81
+ if ("NotFound".equals(e.getException().getMetaClass().getBaseName())) {
82
+ return context.nil;
83
+ }
84
+ throw e;
85
+ }
86
+ }
87
+
88
+ @JRubyMethod(name = "read", required = 1, optional = 1)
89
+ public IRubyObject read(ThreadContext context, IRubyObject[] args) {
90
+ Ruby ruby = context.getRuntime();
91
+ RubyBoolean not_raw = ruby.getTrue();
92
+ IRubyObject key = args[0];
93
+ RubyBoolean notRaw = notRaw(context, args, 1);
94
+ return get(context, new IRubyObject[] { key, notRaw });
95
+ }
96
+
97
+ @JRubyMethod(name = "exist?", required = 1, optional = 1)
98
+ public IRubyObject exist_p(ThreadContext context, IRubyObject[] args) {
99
+ Ruby ruby = context.getRuntime();
100
+ try {
101
+ super.get(context, args);
102
+ return ruby.getTrue();
103
+ } catch (RaiseException e) {
104
+ return ruby.getFalse();
105
+ }
106
+ }
107
+
108
+ @JRubyMethod(name = "get_multi", required = 1, optional = 1)
109
+ public IRubyObject getMulti(ThreadContext context, IRubyObject[] args) {
110
+ Ruby ruby = context.getRuntime();
111
+ IRubyObject keys = args[0];
112
+ RubyBoolean notRaw = notRaw(context, args, 1);
113
+ return super.get(context, new IRubyObject[] { keys, notRaw });
114
+ }
115
+
116
+ @JRubyMethod(name = { "set", "[]=" }, required = 2, optional = 2)
117
+ public IRubyObject set(ThreadContext context, IRubyObject[] args) {
118
+ Ruby ruby = context.getRuntime();
119
+ IRubyObject key = args[0];
120
+ IRubyObject value = args[1];
121
+ RubyFixnum ttl = getTTL(context, args, 2);
122
+ RubyBoolean notRaw = notRaw(context, args, 3);
123
+ try {
124
+ super.set(context, new IRubyObject[] { key, value, ttl, notRaw });
125
+ return ruby.getTrue();
126
+ } catch (RaiseException e) {
127
+ return ruby.getFalse();
128
+ }
129
+ }
130
+
131
+ @JRubyMethod(name = "write", required = 2, optional = 1)
132
+ public IRubyObject write(ThreadContext context, IRubyObject[] args) {
133
+ Ruby ruby = context.getRuntime();
134
+ IRubyObject key = args[0];
135
+ IRubyObject value = args[1];
136
+ RubyFixnum ttl = getTTL(context, args, 2);
137
+ RubyBoolean notRaw = notRaw(context, args, 2);
138
+ return set(context, new IRubyObject[] { key, value, ttl, notRaw });
139
+ }
140
+
141
+ @JRubyMethod(name = "fetch", required = 1, optional = 1)
142
+ public IRubyObject fetch(ThreadContext context, IRubyObject[] args, Block block) {
143
+ Ruby ruby = context.getRuntime();
144
+ IRubyObject key = args[0];
145
+ RubyHash options;
146
+ if (args.length > 1) {
147
+ options = (RubyHash) args[1];
148
+ } else {
149
+ options = new RubyHash(ruby);
150
+ }
151
+ IRubyObject value = read(context, args);
152
+ if (value.isNil()) {
153
+ value = block.call(context);
154
+ write(context, new IRubyObject[] { key, value, options });
155
+ }
156
+ return value;
157
+ }
158
+
159
+ @JRubyMethod(name = "add", required = 2, optional = 2)
160
+ public IRubyObject add(ThreadContext context, IRubyObject[] args) {
161
+ Ruby ruby = context.getRuntime();
162
+ if (args.length > 3) {
163
+ if (ruby.getTrue() == args[3]) {
164
+ args[3] = ruby.getFalse();
165
+ } else {
166
+ args[3] = ruby.getTrue();
167
+ }
168
+ }
169
+ try {
170
+ super.add(context, args);
171
+ if (stringReturnTypes) {
172
+ return ruby.newString("STORED\r\n");
173
+ } else {
174
+ return ruby.getTrue();
175
+ }
176
+ } catch (RaiseException e) {
177
+ if (stringReturnTypes) {
178
+ return ruby.newString("NOT STORED\r\n");
179
+ } else {
180
+ return ruby.getFalse();
181
+ }
182
+ }
183
+ }
184
+
185
+ @JRubyMethod(name = "delete", required = 1, optional = 1)
186
+ public IRubyObject delete(ThreadContext context, IRubyObject[] args) {
187
+ try {
188
+ super.delete(context, args[0]);
189
+ } catch(RaiseException e) { }
190
+
191
+ return context.nil;
192
+ }
193
+
194
+ @JRubyMethod(name = { "flush", "flush_all", "clear" })
195
+ public IRubyObject flush(ThreadContext context) {
196
+ return super.flush(context);
197
+ }
198
+
199
+ @JRubyMethod(name = "read_multi", rest = true)
200
+ public IRubyObject readMulti(ThreadContext context, IRubyObject[] args) {
201
+ Ruby ruby = context.getRuntime();
202
+ if (args.length == 0) {
203
+ return new RubyHash(ruby);
204
+ } else {
205
+ return getMulti(context, args);
206
+ }
207
+ }
208
+
209
+ private RubyFixnum getTTL(ThreadContext context, IRubyObject[] args, int index) {
210
+ Ruby ruby = context.getRuntime();
211
+ if (args.length > index) {
212
+ if (args[index] instanceof RubyFixnum) {
213
+ return (RubyFixnum) args[index];
214
+ } else if (args[index] instanceof RubyHash) {
215
+ RubyHash options = (RubyHash) args[index];
216
+ if (options.containsKey(ruby.newSymbol("ttl"))) {
217
+ return (RubyFixnum) options.get(ruby.newSymbol("ttl"));
218
+ } else if (options.containsKey(ruby.newSymbol("expires_in"))) {
219
+ return (RubyFixnum) options.get(ruby.newSymbol("expires_in"));
220
+ }
221
+ }
222
+ }
223
+ return ruby.newFixnum(super.getDefaultTTL());
224
+ }
225
+
226
+ private RubyBoolean notRaw(ThreadContext context, IRubyObject[] args, int index) {
227
+ Ruby ruby = context.getRuntime();
228
+ RubyBoolean notRaw = ruby.getTrue();
229
+ if (args.length > index) {
230
+ if (args[index] instanceof RubyBoolean && ruby.getTrue() == (RubyBoolean) args[index]) {
231
+ notRaw = ruby.getFalse();
232
+ } else if (args[index] instanceof RubyHash && ruby.getTrue() == ((RubyHash) args[index]).get(ruby.newSymbol("raw"))) {
233
+ notRaw = ruby.getFalse();
234
+ }
235
+ }
236
+ return notRaw;
237
+ }
238
+ }
@@ -0,0 +1,27 @@
1
+ package com.openfeint.memcached.error;
2
+
3
+ import org.jruby.Ruby;
4
+ import org.jruby.RubyClass;
5
+ import org.jruby.RubyException;
6
+ import org.jruby.anno.JRubyClass;
7
+ import org.jruby.exceptions.RaiseException;
8
+
9
+ @JRubyClass(name = "Memcached::Error", parent = "RuntimeError")
10
+ public class Error {
11
+ public static RaiseException newNotFound(Ruby ruby, String message) {
12
+ return newMemcachedError(ruby, "NotFound", message);
13
+ }
14
+
15
+ public static RaiseException newNotStored(Ruby ruby, String message) {
16
+ return newMemcachedError(ruby, "NotStored", message);
17
+ }
18
+
19
+ public static RaiseException newNotSupport(Ruby ruby, String message) {
20
+ return newMemcachedError(ruby, "NotSupport", message);
21
+ }
22
+
23
+ private static RaiseException newMemcachedError(Ruby ruby, String klass, String message) {
24
+ RubyClass errorClass = ruby.getModule("Memcached").getClass(klass);
25
+ return new RaiseException(RubyException.newException(ruby, errorClass, message), true);
26
+ }
27
+ }
@@ -0,0 +1,7 @@
1
+ package com.openfeint.memcached.error;
2
+
3
+ import org.jruby.anno.JRubyClass;
4
+
5
+ @JRubyClass(name="Memcached::NotFound", parent="Memcached::Error")
6
+ public class NotFound extends Error {
7
+ }
@@ -0,0 +1,7 @@
1
+ package com.openfeint.memcached.error;
2
+
3
+ import org.jruby.anno.JRubyClass;
4
+
5
+ @JRubyClass(name="Memcached::NotStored", parent="Memcached::Error")
6
+ public class NotStored extends Error {
7
+ }
@@ -0,0 +1,7 @@
1
+ package com.openfeint.memcached.error;
2
+
3
+ import org.jruby.anno.JRubyClass;
4
+
5
+ @JRubyClass(name="Memcached::NotSupport", parent="Memcached::Error")
6
+ public class NotSupport extends Error {
7
+ }
@@ -1,8 +1,5 @@
1
- package com.openfeint.memcached;
1
+ package com.openfeint.memcached.transcoder;
2
2
 
3
- import java.io.ByteArrayInputStream;
4
- import java.io.ByteArrayOutputStream;
5
- import java.io.IOException;
6
3
  import net.spy.memcached.CachedData;
7
4
  import net.spy.memcached.transcoders.Transcoder;
8
5
  import org.jruby.Ruby;
@@ -10,6 +7,10 @@ import org.jruby.runtime.builtin.IRubyObject;
10
7
  import org.jruby.runtime.marshal.MarshalStream;
11
8
  import org.jruby.runtime.marshal.UnmarshalStream;
12
9
 
10
+ import java.io.ByteArrayInputStream;
11
+ import java.io.ByteArrayOutputStream;
12
+ import java.io.IOException;
13
+
13
14
  /**
14
15
  *
15
16
  * MarshalTranscoder does marshaling and unmarshaling.
@@ -1,10 +1,8 @@
1
- package com.openfeint.memcached;
1
+ package com.openfeint.memcached.transcoder;
2
2
 
3
- import java.io.ByteArrayInputStream;
4
- import java.io.ByteArrayOutputStream;
5
- import java.io.ObjectInputStream;
6
- import java.io.ObjectOutputStream;
7
- import java.io.IOException;
3
+ import com.jcraft.jzlib.ZOutputStream;
4
+ import com.jcraft.jzlib.ZInputStream;
5
+ import com.jcraft.jzlib.JZlib;
8
6
  import net.spy.memcached.CachedData;
9
7
  import net.spy.memcached.transcoders.Transcoder;
10
8
  import org.jruby.Ruby;
@@ -12,9 +10,12 @@ import org.jruby.runtime.builtin.IRubyObject;
12
10
  import org.jruby.runtime.marshal.MarshalStream;
13
11
  import org.jruby.runtime.marshal.UnmarshalStream;
14
12
  import org.jruby.util.ByteList;
15
- import com.jcraft.jzlib.ZOutputStream;
16
- import com.jcraft.jzlib.ZInputStream;
17
- import com.jcraft.jzlib.JZlib;
13
+
14
+ import java.io.ByteArrayInputStream;
15
+ import java.io.ByteArrayOutputStream;
16
+ import java.io.ObjectInputStream;
17
+ import java.io.ObjectOutputStream;
18
+ import java.io.IOException;
18
19
 
19
20
  /**
20
21
  *
metadata CHANGED
@@ -1,97 +1,113 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: jruby-memcached
3
- version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.3.0
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.4.0
6
6
  platform: ruby
7
- authors:
8
- - Richard Huang
9
- autorequire:
7
+ authors:
8
+ - Richard Huang
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2012-08-07 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: rspec
17
- prerelease: false
18
- requirement: &id001 !ruby/object:Gem::Requirement
19
- none: false
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: "0"
24
- type: :development
25
- version_requirements: *id001
26
- - !ruby/object:Gem::Dependency
27
- name: mocha
28
- prerelease: false
29
- requirement: &id002 !ruby/object:Gem::Requirement
30
- none: false
31
- requirements:
32
- - - ">="
33
- - !ruby/object:Gem::Version
34
- version: "0"
35
- type: :development
36
- version_requirements: *id002
12
+ date: 2012-08-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ! '>='
19
+ - !ruby/object:Gem::Version
20
+ version: !binary |-
21
+ MA==
22
+ none: false
23
+ requirement: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ! '>='
26
+ - !ruby/object:Gem::Version
27
+ version: !binary |-
28
+ MA==
29
+ none: false
30
+ prerelease: false
31
+ type: :development
32
+ - !ruby/object:Gem::Dependency
33
+ name: mocha
34
+ version_requirements: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: !binary |-
39
+ MA==
40
+ none: false
41
+ requirement: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: !binary |-
46
+ MA==
47
+ none: false
48
+ prerelease: false
49
+ type: :development
37
50
  description: jruby memcacached client which is compatible with memcached gem
38
- email:
39
- - flyerhzm@gmail.com
51
+ email:
52
+ - flyerhzm@gmail.com
40
53
  executables: []
41
-
42
54
  extensions: []
43
-
44
55
  extra_rdoc_files: []
45
-
46
- files:
47
- - .gitignore
48
- - Gemfile
49
- - Gemfile.lock
50
- - MIT-LICENSE
51
- - README.md
52
- - Rakefile
53
- - benchmark.rb
54
- - jruby_memcached.gemspec
55
- - lib/memcached.rb
56
- - lib/memcached/exceptions.rb
57
- - lib/memcached/version.rb
58
- - pom.xml
59
- - spec/memcached_spec.rb
60
- - spec/spec_helper.rb
61
- - src/main/java/com/openfeint/memcached/MarshalTranscoder.java
62
- - src/main/java/com/openfeint/memcached/MarshalZlibTranscoder.java
63
- - src/main/java/com/openfeint/memcached/Memcached.java
64
- - src/main/java/com/openfeint/memcached/MemcachedLibrary.java
65
- - src/main/java/net/spy/memcached/KetamaNodeLocator.java
66
- - src/main/java/net/spy/memcached/util/DefaultKetamaNodeLocatorConfiguration.java
67
- - target/spymemcached-ext-0.0.1.jar
56
+ files:
57
+ - .gitignore
58
+ - CHANGELOG.md
59
+ - Gemfile
60
+ - Gemfile.lock
61
+ - MIT-LICENSE
62
+ - README.md
63
+ - Rakefile
64
+ - benchmark.rb
65
+ - jruby_memcached.gemspec
66
+ - lib/memcached.rb
67
+ - lib/memcached/version.rb
68
+ - pom.xml
69
+ - spec/memcached_spec.rb
70
+ - spec/rails_spec.rb
71
+ - spec/spec_helper.rb
72
+ - src/main/java/com/openfeint/memcached/Memcached.java
73
+ - src/main/java/com/openfeint/memcached/MemcachedService.java
74
+ - src/main/java/com/openfeint/memcached/Rails.java
75
+ - src/main/java/com/openfeint/memcached/error/Error.java
76
+ - src/main/java/com/openfeint/memcached/error/NotFound.java
77
+ - src/main/java/com/openfeint/memcached/error/NotStored.java
78
+ - src/main/java/com/openfeint/memcached/error/NotSupport.java
79
+ - src/main/java/com/openfeint/memcached/transcoder/MarshalTranscoder.java
80
+ - src/main/java/com/openfeint/memcached/transcoder/MarshalZlibTranscoder.java
81
+ - src/main/java/net/spy/memcached/KetamaNodeLocator.java
82
+ - src/main/java/net/spy/memcached/util/DefaultKetamaNodeLocatorConfiguration.java
83
+ - target/spymemcached-ext-0.0.1.jar
68
84
  homepage: https://github.com/aurorafeint/jruby-memcached
69
85
  licenses: []
70
-
71
- post_install_message:
86
+ post_install_message:
72
87
  rdoc_options: []
73
-
74
- require_paths:
75
- - lib
76
- required_ruby_version: !ruby/object:Gem::Requirement
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: !binary |-
95
+ MA==
77
96
  none: false
78
- requirements:
79
- - - ">="
80
- - !ruby/object:Gem::Version
81
- version: "0"
82
- required_rubygems_version: !ruby/object:Gem::Requirement
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: !binary |-
102
+ MA==
83
103
  none: false
84
- requirements:
85
- - - ">="
86
- - !ruby/object:Gem::Version
87
- version: "0"
88
104
  requirements: []
89
-
90
- rubyforge_project:
105
+ rubyforge_project:
91
106
  rubygems_version: 1.8.24
92
- signing_key:
107
+ signing_key:
93
108
  specification_version: 3
94
109
  summary: jruby compatible memcached client
95
- test_files:
96
- - spec/memcached_spec.rb
97
- - spec/spec_helper.rb
110
+ test_files:
111
+ - spec/memcached_spec.rb
112
+ - spec/rails_spec.rb
113
+ - spec/spec_helper.rb