spymemcached.jruby 1.0.3-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +0 -0
- data/lib/spymemcached-2.11.4.jar +0 -0
- data/lib/spymemcached.rb +144 -0
- data/lib/spymemcached/rails23.rb +60 -0
- data/lib/spymemcached_adapter.jar +0 -0
- data/test/memcache.rb +4 -0
- data/test/spymemcached_binary_protocol_test.rb +121 -0
- data/test/spymemcached_plain_protocol_test.rb +121 -0
- data/test/spymemcached_rails23_test.rb +30 -0
- data/test/spymemcached_test.rb +175 -0
- data/test/test_helper.rb +19 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5780ea53b6d60b038125beb9dd617f8add8cbbc7
|
4
|
+
data.tar.gz: e55cd318622f535a5351ab76d02b666b2294dbab
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a78224ec0b00eb60041e3e0ccfaf2a97b37b79c139e6f7e766fafe9d0133fa292edd77b4b5e5e17bacd1d6bf2d2f880a37439c491f584d4e236da4bc96ce15d1
|
7
|
+
data.tar.gz: e022c6ef66b52a9da19958a9753dcda754d536050305a3c0e87e62095894e0f8c16e3bb103fcec378bbdfacb26af09e932644c1b0c7c75c6e90c27a64c9b3303
|
data/README.md
ADDED
File without changes
|
Binary file
|
data/lib/spymemcached.rb
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
require 'spymemcached-2.11.4.jar'
|
2
|
+
require 'spymemcached_adapter.jar'
|
3
|
+
require 'spymemcached_adapter'
|
4
|
+
|
5
|
+
#
|
6
|
+
# Memcached client Spymemcached JRuby extension
|
7
|
+
#
|
8
|
+
class Spymemcached
|
9
|
+
class Error < StandardError; end
|
10
|
+
class TimeoutError < Error; end
|
11
|
+
|
12
|
+
# default options for client
|
13
|
+
DEFAULT_OPTIONS = {
|
14
|
+
:timeout => 0.5, # second
|
15
|
+
:binary => true
|
16
|
+
}
|
17
|
+
|
18
|
+
# Accepts a list of +servers+ and a list of +options+.
|
19
|
+
# The +servers+ can be either strings of the form "hostname:port" or
|
20
|
+
# one string format as "<hostname>:<port>,<hostname>:<port>".
|
21
|
+
# Use KetamaConnectionFactory as Spymemcached MemcachedClient connection factory.
|
22
|
+
# See Spymemcached document for details.
|
23
|
+
# Different with Ruby memcache clients (e.g. Dalli), there is no raw option for operations.
|
24
|
+
#
|
25
|
+
# Valid +options+ are:
|
26
|
+
#
|
27
|
+
# [:namespace] Prepends this value to all keys added or retrieved.
|
28
|
+
# [:timeout] Time to use as the socket read timeout, seconds. Defaults to 0.5 sec.
|
29
|
+
# [:binary] Talks binary protocol with Memcached server. Default to true.
|
30
|
+
#
|
31
|
+
# Logger: see Spymemcached for how to turn on detail log
|
32
|
+
#
|
33
|
+
def initialize(servers=['localhost:11211'], options={})
|
34
|
+
@servers, @options = Array(servers).join(','), DEFAULT_OPTIONS.merge(options)
|
35
|
+
@client = SpymemcachedAdapter.new(@servers, @options)
|
36
|
+
@namespace = if @options[:namespace]
|
37
|
+
@options[:namespace].is_a?(Proc) ? @options[:namespace] : lambda { @options[:namespace] }
|
38
|
+
end
|
39
|
+
at_exit { shutdown }
|
40
|
+
end
|
41
|
+
|
42
|
+
def fetch(key, ttl=0, &block)
|
43
|
+
val = get(key)
|
44
|
+
if val.nil? && block_given?
|
45
|
+
val = yield
|
46
|
+
add(key, val, ttl)
|
47
|
+
end
|
48
|
+
val
|
49
|
+
end
|
50
|
+
|
51
|
+
def get(key)
|
52
|
+
@client.get(ns(key))
|
53
|
+
end
|
54
|
+
alias :[] :get
|
55
|
+
|
56
|
+
def get_multi(*keys)
|
57
|
+
Hash[@client.get_multi(keys.map(&method(:ns))).map {|k, v| [unns(k), v]}]
|
58
|
+
end
|
59
|
+
|
60
|
+
def add(key, value, ttl=0, opts={})
|
61
|
+
@client.add(ns(key), value, ttl)
|
62
|
+
end
|
63
|
+
|
64
|
+
def set(key, value, ttl=0, opts={})
|
65
|
+
@client.set(ns(key), value, ttl)
|
66
|
+
end
|
67
|
+
alias :[]= :set
|
68
|
+
|
69
|
+
def cas(key, ttl=0, &block)
|
70
|
+
@client.cas(ns(key), ttl, &block)
|
71
|
+
end
|
72
|
+
|
73
|
+
def replace(key, value, ttl=0)
|
74
|
+
@client.replace(ns(key), value, ttl)
|
75
|
+
end
|
76
|
+
|
77
|
+
def delete(key)
|
78
|
+
@client.delete(ns(key))
|
79
|
+
end
|
80
|
+
|
81
|
+
def incr(key, by=1)
|
82
|
+
@client.incr(ns(key), by)
|
83
|
+
end
|
84
|
+
|
85
|
+
def decr(key, by=1)
|
86
|
+
@client.decr(ns(key), by)
|
87
|
+
end
|
88
|
+
|
89
|
+
def append(key, value)
|
90
|
+
@client.append(ns(key), value)
|
91
|
+
end
|
92
|
+
|
93
|
+
def prepend(key, value)
|
94
|
+
@client.prepend(ns(key), value)
|
95
|
+
end
|
96
|
+
|
97
|
+
def touch(key, ttl=0)
|
98
|
+
@client.touch(ns(key), ttl)
|
99
|
+
end
|
100
|
+
|
101
|
+
def stats
|
102
|
+
@client.stats
|
103
|
+
end
|
104
|
+
|
105
|
+
def version
|
106
|
+
@client.version
|
107
|
+
end
|
108
|
+
|
109
|
+
def flush_all
|
110
|
+
@client.flush_all
|
111
|
+
end
|
112
|
+
alias :flush :flush_all
|
113
|
+
alias :clear :flush_all
|
114
|
+
|
115
|
+
def shutdown
|
116
|
+
@client.shutdown
|
117
|
+
end
|
118
|
+
|
119
|
+
# compatible api
|
120
|
+
def rails23
|
121
|
+
require 'spymemcached/rails23'
|
122
|
+
Rails23.new(self)
|
123
|
+
end
|
124
|
+
|
125
|
+
private
|
126
|
+
def raw?(opts)
|
127
|
+
opts.is_a?(Hash) ? opts[:raw] : opts
|
128
|
+
end
|
129
|
+
|
130
|
+
def ns(key)
|
131
|
+
return key unless namespace
|
132
|
+
"#{namespace.call}:#{key}"
|
133
|
+
end
|
134
|
+
|
135
|
+
def unns(k)
|
136
|
+
return k unless namespace
|
137
|
+
@ns_size ||= namespace.call.size + 1
|
138
|
+
k[@ns_size..-1]
|
139
|
+
end
|
140
|
+
|
141
|
+
def namespace
|
142
|
+
@namespace
|
143
|
+
end
|
144
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'memcache'
|
2
|
+
|
3
|
+
class Spymemcached
|
4
|
+
class Rails23
|
5
|
+
module Response # :nodoc:
|
6
|
+
STORED = "STORED\r\n"
|
7
|
+
NOT_STORED = "NOT_STORED\r\n"
|
8
|
+
EXISTS = "EXISTS\r\n"
|
9
|
+
NOT_FOUND = "NOT_FOUND\r\n"
|
10
|
+
DELETED = "DELETED\r\n"
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(client)
|
14
|
+
@client = client
|
15
|
+
end
|
16
|
+
|
17
|
+
def get_multi(*args)
|
18
|
+
op(:get_multi, *args)
|
19
|
+
end
|
20
|
+
|
21
|
+
def get(key, *args)
|
22
|
+
op(:get, key)
|
23
|
+
end
|
24
|
+
|
25
|
+
def add(key, value, ttl=0, *args)
|
26
|
+
op(:add, key, value, ttl) ? Response::STORED : Response::NOT_STORED
|
27
|
+
end
|
28
|
+
|
29
|
+
def set(key, value, ttl=0, *args)
|
30
|
+
op(:set, key, value, ttl) ? Response::STORED : Response::NOT_STORED
|
31
|
+
end
|
32
|
+
|
33
|
+
def delete(key, *args)
|
34
|
+
op(:delete, key) ? Response::DELETED : Response::NOT_FOUND
|
35
|
+
end
|
36
|
+
|
37
|
+
def incr(key, by=1)
|
38
|
+
op(:incr, key, by)
|
39
|
+
end
|
40
|
+
|
41
|
+
def decr(key, by=1)
|
42
|
+
op(:decr, key, by)
|
43
|
+
end
|
44
|
+
|
45
|
+
def flush_all
|
46
|
+
op(:flush_all)
|
47
|
+
end
|
48
|
+
|
49
|
+
def stats
|
50
|
+
op(:stats)
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
def op(name, *args, &block)
|
55
|
+
@client.send(name, *args, &block)
|
56
|
+
rescue Spymemcached::Error => e
|
57
|
+
raise MemCache::MemCacheError, e.message
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
Binary file
|
data/test/memcache.rb
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class SpymemcachedBinaryProtocolTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@client = binary_client
|
6
|
+
@client.flush_all
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_get_set
|
10
|
+
assert_equal nil, @client.get('key')
|
11
|
+
@client.set('key', 'value')
|
12
|
+
assert_equal 'value', @client.get('key')
|
13
|
+
assert_equal 'value', @client['key']
|
14
|
+
|
15
|
+
@client.set('key', nil)
|
16
|
+
assert_equal nil, @client.get('key')
|
17
|
+
@client['key'] = 'hello'
|
18
|
+
assert_equal 'hello', @client.get('key')
|
19
|
+
|
20
|
+
@client.set('key2', ['hello', 'world'])
|
21
|
+
assert_equal ['hello', 'world'], @client.get('key2')
|
22
|
+
|
23
|
+
@client.set('key2', Msg.new('hi'))
|
24
|
+
assert_equal Msg.new('hi'), @client.get('key2')
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_get_set_large_string
|
28
|
+
str = 'value' * 10_000_000
|
29
|
+
@client.set(name, str)
|
30
|
+
assert_equal str, @client.get(name)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_add
|
34
|
+
assert @client.add('key', 'value')
|
35
|
+
assert_equal "value", @client.get('key')
|
36
|
+
|
37
|
+
assert !@client.add('key', 'another')
|
38
|
+
assert_equal "value", @client.get('key')
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_incr_and_decr
|
42
|
+
@client.add('key', '0')
|
43
|
+
assert_equal 0, @client.incr('key', 0)
|
44
|
+
@client.incr('key')
|
45
|
+
assert_equal 1, @client.incr('key', 0)
|
46
|
+
@client.incr('key', 5)
|
47
|
+
assert_equal 6, @client.incr('key', 0)
|
48
|
+
assert_equal '6', @client.get('key')
|
49
|
+
|
50
|
+
@client.set('key', '6')
|
51
|
+
@client.decr('key')
|
52
|
+
assert_equal 5, @client.decr('key', 0)
|
53
|
+
@client.decr('key', 3)
|
54
|
+
assert_equal 2, @client.decr('key', 0)
|
55
|
+
|
56
|
+
@client.replace('key', '10')
|
57
|
+
assert_equal 11, @client.incr('key')
|
58
|
+
|
59
|
+
@client.cas('key') { '7' }
|
60
|
+
assert_equal 8, @client.incr('key')
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_fetch
|
64
|
+
ret = @client.fetch('key') { 'value' }
|
65
|
+
assert_equal 'value', ret
|
66
|
+
ret = @client.fetch('key') { 'hello' }
|
67
|
+
assert_equal 'value', ret
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_get_multi
|
71
|
+
@client.add('k1', 'v1')
|
72
|
+
@client.add('k2', Msg.new('v2'))
|
73
|
+
ret = @client.get_multi('k1', 'k2', 'k3')
|
74
|
+
assert_equal(2, ret.size)
|
75
|
+
assert_equal('v1', ret['k1'])
|
76
|
+
assert_equal(Msg.new('v2'), ret['k2'])
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_cas
|
80
|
+
assert_nil(@client.cas('k1') { 'v0' })
|
81
|
+
|
82
|
+
assert @client.add('k1', 'v1')
|
83
|
+
assert(@client.cas('k1') { 'v2' })
|
84
|
+
assert_equal 'v2', @client.get('k1')
|
85
|
+
|
86
|
+
ret = @client.cas('k1') do
|
87
|
+
@client.set('k1', 'v4')
|
88
|
+
'v3'
|
89
|
+
end
|
90
|
+
assert_equal(false, ret)
|
91
|
+
assert_equal('v4', @client.get('k1'))
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_replace
|
95
|
+
assert_equal false, @client.replace('k1', 'v1')
|
96
|
+
assert_nil @client['k1']
|
97
|
+
|
98
|
+
@client['k1'] = 'v0'
|
99
|
+
assert @client.replace('k1', 'v1')
|
100
|
+
assert_equal 'v1', @client['k1']
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_delete
|
104
|
+
@client['k1'] = 'v0'
|
105
|
+
assert @client.delete('k1')
|
106
|
+
assert_nil @client['k1']
|
107
|
+
assert_equal false, @client.delete('k1')
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_append_string
|
111
|
+
@client['k1'] = 'v'
|
112
|
+
assert @client.append('k1', '1')
|
113
|
+
assert_equal 'v1', @client['k1']
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_prepend_string
|
117
|
+
@client['k1'] = 'v'
|
118
|
+
assert @client.prepend('k1', '1')
|
119
|
+
assert_equal '1v', @client['k1']
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class SpymemcachedPlainProtocolTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@client = plain_client
|
6
|
+
@client.flush_all
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_get_set
|
10
|
+
assert_equal nil, @client.get('key')
|
11
|
+
@client.set('key', 'value')
|
12
|
+
assert_equal 'value', @client.get('key')
|
13
|
+
assert_equal 'value', @client['key']
|
14
|
+
|
15
|
+
@client.set('key', nil)
|
16
|
+
assert_equal nil, @client.get('key')
|
17
|
+
@client['key'] = 'hello'
|
18
|
+
assert_equal 'hello', @client.get('key')
|
19
|
+
|
20
|
+
@client.set('key2', ['hello', 'world'])
|
21
|
+
assert_equal ['hello', 'world'], @client.get('key2')
|
22
|
+
|
23
|
+
@client.set('key2', Msg.new('hi'))
|
24
|
+
assert_equal Msg.new('hi'), @client.get('key2')
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_get_set_large_string
|
28
|
+
str = 'value' * 10_000_000
|
29
|
+
@client.set(name, str)
|
30
|
+
assert_equal str, @client.get(name)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_add
|
34
|
+
assert @client.add('key', 'value')
|
35
|
+
assert_equal "value", @client.get('key')
|
36
|
+
|
37
|
+
assert !@client.add('key', 'another')
|
38
|
+
assert_equal "value", @client.get('key')
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_incr_and_decr
|
42
|
+
@client.add('key', '0')
|
43
|
+
assert_equal 0, @client.incr('key', 0)
|
44
|
+
@client.incr('key')
|
45
|
+
assert_equal 1, @client.incr('key', 0)
|
46
|
+
@client.incr('key', 5)
|
47
|
+
assert_equal 6, @client.incr('key', 0)
|
48
|
+
assert_equal '6', @client.get('key')
|
49
|
+
|
50
|
+
@client.set('key', '6')
|
51
|
+
@client.decr('key')
|
52
|
+
assert_equal 5, @client.decr('key', 0)
|
53
|
+
@client.decr('key', 3)
|
54
|
+
assert_equal 2, @client.decr('key', 0)
|
55
|
+
|
56
|
+
@client.replace('key', '10')
|
57
|
+
assert_equal 11, @client.incr('key')
|
58
|
+
|
59
|
+
@client.cas('key') { '7' }
|
60
|
+
assert_equal 8, @client.incr('key')
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_fetch
|
64
|
+
ret = @client.fetch('key') { 'value' }
|
65
|
+
assert_equal 'value', ret
|
66
|
+
ret = @client.fetch('key') { 'hello' }
|
67
|
+
assert_equal 'value', ret
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_get_multi
|
71
|
+
@client.add('k1', 'v1')
|
72
|
+
@client.add('k2', Msg.new('v2'))
|
73
|
+
ret = @client.get_multi('k1', 'k2', 'k3')
|
74
|
+
assert_equal(2, ret.size)
|
75
|
+
assert_equal('v1', ret['k1'])
|
76
|
+
assert_equal(Msg.new('v2'), ret['k2'])
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_cas
|
80
|
+
assert_nil(@client.cas('k1') { 'v0' })
|
81
|
+
|
82
|
+
assert @client.add('k1', 'v1')
|
83
|
+
assert(@client.cas('k1') { 'v2' })
|
84
|
+
assert_equal 'v2', @client.get('k1')
|
85
|
+
|
86
|
+
ret = @client.cas('k1') do
|
87
|
+
@client.set('k1', 'v4')
|
88
|
+
'v3'
|
89
|
+
end
|
90
|
+
assert_equal(false, ret)
|
91
|
+
assert_equal('v4', @client.get('k1'))
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_replace
|
95
|
+
assert_equal false, @client.replace('k1', 'v1')
|
96
|
+
assert_nil @client['k1']
|
97
|
+
|
98
|
+
@client['k1'] = 'v0'
|
99
|
+
assert @client.replace('k1', 'v1')
|
100
|
+
assert_equal 'v1', @client['k1']
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_delete
|
104
|
+
@client['k1'] = 'v0'
|
105
|
+
assert @client.delete('k1')
|
106
|
+
assert_nil @client['k1']
|
107
|
+
assert_equal false, @client.delete('k1')
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_append_string
|
111
|
+
@client['k1'] = 'v'
|
112
|
+
assert @client.append('k1', '1')
|
113
|
+
assert_equal 'v1', @client['k1']
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_prepend_string
|
117
|
+
@client['k1'] = 'v'
|
118
|
+
assert @client.prepend('k1', '1')
|
119
|
+
assert_equal '1v', @client['k1']
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class SpymemcachedRails23Test < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@client = binary_client.rails23
|
6
|
+
@client.flush_all
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_api_compatible
|
10
|
+
assert_equal response::STORED, @client.add("key", 'value')
|
11
|
+
assert_equal 'value', @client.get('key')
|
12
|
+
assert_equal response::STORED, @client.set('key', 'value2')
|
13
|
+
|
14
|
+
assert_equal response::DELETED, @client.delete('key')
|
15
|
+
assert_equal response::NOT_FOUND, @client.delete('key2')
|
16
|
+
|
17
|
+
@client.set("key1", '0')
|
18
|
+
@client.set("key2", '1')
|
19
|
+
assert_equal({"key1" => '0', 'key2' => '1'}, @client.get_multi('key1', 'key2'))
|
20
|
+
|
21
|
+
assert_equal 1, @client.incr('key1')
|
22
|
+
assert_equal 0, @client.decr('key2')
|
23
|
+
|
24
|
+
assert @client.stats.values.first
|
25
|
+
end
|
26
|
+
|
27
|
+
def response
|
28
|
+
Spymemcached::Rails23::Response
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,175 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class SpymemcachedTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@client = binary_client
|
6
|
+
@client.flush_all
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_expiry
|
10
|
+
@client.add('add_key1', 'v1', 0)
|
11
|
+
@client.add('add_key2', 'v2', 2)
|
12
|
+
|
13
|
+
@client.set('set_key1', 'v1', 0)
|
14
|
+
@client.set('set_key2', 'v2', 2)
|
15
|
+
|
16
|
+
@client.fetch('fetch_key1', 0) { 'v1' }
|
17
|
+
@client.fetch('fetch_key2', 2) { 'v2' }
|
18
|
+
|
19
|
+
@client.add('cas_key1', 'v0')
|
20
|
+
@client.add('cas_key2', 'v0')
|
21
|
+
@client.cas('cas_key1', 0) { 'v1' }
|
22
|
+
@client.cas('cas_key2', 2) { 'v2' }
|
23
|
+
|
24
|
+
@client.add('replace_key1', 'v0')
|
25
|
+
@client.add('replace_key2', 'v0')
|
26
|
+
@client.replace('replace_key1', 'v1', 0)
|
27
|
+
@client.replace('replace_key2', 'v2', 2)
|
28
|
+
|
29
|
+
@client.add('touch_key1', 'v1', 2)
|
30
|
+
|
31
|
+
sleep 0.1
|
32
|
+
|
33
|
+
assert_equal 'v2', @client.get('add_key2')
|
34
|
+
assert_equal 'v2', @client.get('set_key2')
|
35
|
+
assert_equal 'v2', @client.get('fetch_key2')
|
36
|
+
assert_equal 'v2', @client.get('cas_key2')
|
37
|
+
assert_equal 'v2', @client.get('replace_key2')
|
38
|
+
|
39
|
+
assert @client.touch('touch_key1')
|
40
|
+
|
41
|
+
sleep 2
|
42
|
+
assert_equal 'v1', @client['touch_key1']
|
43
|
+
|
44
|
+
assert_equal 'v1', @client.get('add_key1')
|
45
|
+
assert_nil @client.get('add_key2')
|
46
|
+
|
47
|
+
assert_equal 'v1', @client.get('set_key1')
|
48
|
+
assert_nil @client.get('set_key2')
|
49
|
+
|
50
|
+
assert_equal 'v1', @client.get('fetch_key1')
|
51
|
+
assert_nil @client.get('fetch_key2')
|
52
|
+
|
53
|
+
assert_equal 'v1', @client.get('cas_key1')
|
54
|
+
assert_nil @client.get('cas_key2')
|
55
|
+
|
56
|
+
assert_equal 'v1', @client.get('replace_key1')
|
57
|
+
assert_nil @client.get('replace_key2')
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_timeout
|
61
|
+
@client = Spymemcached.new('localhost:11111', :timeout => 0.01)
|
62
|
+
assert_timeout = lambda do |action, *args|
|
63
|
+
start_at = Time.now
|
64
|
+
assert_raise Spymemcached::TimeoutError do
|
65
|
+
@client.send(action, *args) { 'v1' }
|
66
|
+
end
|
67
|
+
time = Time.now - start_at
|
68
|
+
assert time < 0.025, "Timeout is 0.01, actual time of action #{action} when timeout: #{time}"
|
69
|
+
end
|
70
|
+
|
71
|
+
assert_timeout[:flush_all]
|
72
|
+
|
73
|
+
assert_timeout[:get, 'key']
|
74
|
+
assert_timeout[:get_multi, 'key']
|
75
|
+
assert_timeout[:incr, 'key']
|
76
|
+
assert_timeout[:decr, 'key']
|
77
|
+
assert_timeout[:fetch, 'key']
|
78
|
+
assert_timeout[:delete, 'key']
|
79
|
+
assert_timeout[:touch, 'key']
|
80
|
+
|
81
|
+
assert_timeout[:add, 'key', 'value']
|
82
|
+
assert_timeout[:set, 'key', 'value']
|
83
|
+
assert_timeout[:replace, 'key', 'value']
|
84
|
+
assert_timeout[:append, 'key', 'value']
|
85
|
+
assert_timeout[:prepend, 'key', 'value']
|
86
|
+
|
87
|
+
assert_equal({}, @client.version)
|
88
|
+
|
89
|
+
stats = @client.stats
|
90
|
+
assert_equal(1, stats.size)
|
91
|
+
assert_equal([{}], stats.values)
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_namespace
|
95
|
+
@ns_client = Spymemcached.new('localhost:11211', :namespace => 'ns')
|
96
|
+
assert_ns = lambda do |k, v|
|
97
|
+
assert_equal 'v1', @ns_client.get('key1')
|
98
|
+
assert_nil @client.get('key1')
|
99
|
+
assert_equal 'v1', @client.get('ns:key1')
|
100
|
+
end
|
101
|
+
|
102
|
+
@ns_client.set('key1', 'v1')
|
103
|
+
assert_ns.call('key1', 'v1')
|
104
|
+
|
105
|
+
@ns_client.add('key2', 'v2')
|
106
|
+
assert_ns.call('key2', 'v2')
|
107
|
+
|
108
|
+
assert_equal({'key1' => 'v1', 'key2' => 'v2'}, @ns_client.get_multi('key1', 'key2'))
|
109
|
+
assert_equal({}, @client.get_multi('key1', 'key2'))
|
110
|
+
|
111
|
+
@ns_client.add('key3', 'v0')
|
112
|
+
@ns_client.cas('key3') { 'v3' }
|
113
|
+
assert_equal 'v3', @ns_client['key3']
|
114
|
+
assert_nil @client['key3']
|
115
|
+
assert_equal 'v3', @client['ns:key3']
|
116
|
+
|
117
|
+
assert @ns_client.replace('key3', 'v4')
|
118
|
+
assert_equal 'v4', @ns_client['key3']
|
119
|
+
assert_nil @client['key3']
|
120
|
+
assert_equal 'v4', @client['ns:key3']
|
121
|
+
|
122
|
+
@client['key3'] = 'v3'
|
123
|
+
assert @ns_client.delete('key3')
|
124
|
+
assert_equal 'v3', @client['key3']
|
125
|
+
assert_nil @ns_client['key3']
|
126
|
+
|
127
|
+
@ns_client.append('key2', '4')
|
128
|
+
assert_equal 'v24', @ns_client.get('key2')
|
129
|
+
|
130
|
+
@ns_client.prepend('key2', '4')
|
131
|
+
assert_equal '4v24', @ns_client.get('key2')
|
132
|
+
|
133
|
+
@ns_client['key2'] = '1'
|
134
|
+
assert_equal 2, @ns_client.incr('key2')
|
135
|
+
assert_equal 1, @ns_client.decr('key2')
|
136
|
+
|
137
|
+
@ns_client.touch('key2', 1)
|
138
|
+
sleep 1.1
|
139
|
+
assert_nil @ns_client['key2']
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_lambda_as_namespace
|
143
|
+
@ns_client = Spymemcached.new('localhost:11211', :namespace => lambda { 'ns' })
|
144
|
+
@ns_client['key'] = 'value'
|
145
|
+
assert_equal 'value', @client['ns:key']
|
146
|
+
|
147
|
+
assert_equal({'key' => "value"}, @ns_client.get_multi('key'))
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_timeout_zero
|
151
|
+
assert_raise Spymemcached::Error do
|
152
|
+
Spymemcached.new('localhost:11111', :timeout => 0)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_stats
|
157
|
+
stats = @client.stats
|
158
|
+
assert_equal(Hash, stats.class)
|
159
|
+
assert_equal(1, stats.size)
|
160
|
+
assert_match(/localhost/, stats.keys.first)
|
161
|
+
assert_match(/\:11211/, stats.keys.first)
|
162
|
+
assert_equal(Hash, stats.values.first.class)
|
163
|
+
assert stats.values.first.size > 5
|
164
|
+
assert_match(@client.version.values.first, stats.values.first['version'])
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_version
|
168
|
+
v = @client.version
|
169
|
+
assert_equal(Hash, v.class)
|
170
|
+
assert_equal(1, v.size)
|
171
|
+
assert_match(/localhost/, v.keys.first)
|
172
|
+
assert_match(/\:11211/, v.keys.first)
|
173
|
+
assert_match(/\d\.\d+\.\d+/, v.values.first)
|
174
|
+
end
|
175
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require "spymemcached"
|
3
|
+
|
4
|
+
class Msg < Struct.new(:name)
|
5
|
+
end
|
6
|
+
|
7
|
+
class Test::Unit::TestCase
|
8
|
+
def plain_client
|
9
|
+
@@plain_client ||= spymemcached(:binary => false)
|
10
|
+
end
|
11
|
+
|
12
|
+
def binary_client
|
13
|
+
@@binary_client ||= spymemcached
|
14
|
+
end
|
15
|
+
|
16
|
+
def spymemcached(opts={})
|
17
|
+
Spymemcached.new('localhost:11211', opts)
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: spymemcached.jruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.3
|
5
|
+
platform: java
|
6
|
+
authors:
|
7
|
+
- Xiao Li
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
version_requirements: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
requirement: !ruby/object:Gem::Requirement
|
21
|
+
requirements:
|
22
|
+
- - '>='
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: '0'
|
25
|
+
prerelease: false
|
26
|
+
type: :development
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: test-unit
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
prerelease: false
|
40
|
+
type: :development
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake-compiler
|
43
|
+
version_requirements: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.9'
|
48
|
+
- - '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 0.9.2
|
51
|
+
requirement: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ~>
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0.9'
|
56
|
+
- - '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: 0.9.2
|
59
|
+
prerelease: false
|
60
|
+
type: :development
|
61
|
+
description: |
|
62
|
+
A JRuby extension wrapping the latest spymemcached client (v2.11.4).
|
63
|
+
email:
|
64
|
+
- swing1979@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- README.md
|
70
|
+
- lib/spymemcached-2.11.4.jar
|
71
|
+
- lib/spymemcached.rb
|
72
|
+
- lib/spymemcached_adapter.jar
|
73
|
+
- lib/spymemcached/rails23.rb
|
74
|
+
- test/memcache.rb
|
75
|
+
- test/spymemcached_binary_protocol_test.rb
|
76
|
+
- test/spymemcached_plain_protocol_test.rb
|
77
|
+
- test/spymemcached_rails23_test.rb
|
78
|
+
- test/spymemcached_test.rb
|
79
|
+
- test/test_helper.rb
|
80
|
+
homepage: https://github.com/xli/spymemcached.jruby
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.1.9
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: A JRuby extension wrapping the latest spymemcached client (v2.11.4).
|
104
|
+
test_files: []
|