dallish 1.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/Gemfile +5 -0
- data/LICENSE +23 -0
- data/README.md +53 -0
- data/dallish.gemspec +26 -0
- data/lib/dallish.rb +24 -0
- data/lib/dallish/client.rb +101 -0
- data/lib/dallish/version.rb +6 -0
- data/spec/dallish_spec.rb +71 -0
- metadata +71 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
(The MIT License)
|
2
|
+
|
3
|
+
Copyright (c) 2012 Kazuhiro Sera <seratch__at__gmail.com>
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
'Software'), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
20
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
21
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
22
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
<<<<<<< HEAD
|
2
|
+
dallish
|
3
|
+
=======
|
4
|
+
|
5
|
+
An extended Dalli for memcached 1.4.x
|
6
|
+
=======
|
7
|
+
# Dallish
|
8
|
+
|
9
|
+
Dallish is an extended [Dalli](https://github.com/mperham/dalli) for memcached 1.4.x.
|
10
|
+
|
11
|
+
## Note
|
12
|
+
|
13
|
+
Dallish is slower than Dalli, so just use as a tool for debugging or management.
|
14
|
+
|
15
|
+
Unfortunately, the methods by Dallish won't work with memcached 1.6 or higher.
|
16
|
+
|
17
|
+
## How to use?
|
18
|
+
|
19
|
+
First, get Dallish.
|
20
|
+
|
21
|
+
```
|
22
|
+
gem install dallish
|
23
|
+
```
|
24
|
+
|
25
|
+
And then, try it as follows.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'dallish'
|
29
|
+
|
30
|
+
dallish = Dallish.new('localhost:11211')
|
31
|
+
|
32
|
+
# methods by Dalli
|
33
|
+
dallish.set('foo', 123)
|
34
|
+
dallish.set('fooo', 234)
|
35
|
+
dallish.set('bar', 345)
|
36
|
+
dallish.set('baz', 456)
|
37
|
+
|
38
|
+
dallish.get('foo') # => 123
|
39
|
+
|
40
|
+
# mehods by Dallish
|
41
|
+
dallish.all_keys # => [foo,fooo,bar,baz]
|
42
|
+
|
43
|
+
dallish.find_keys_by(/foo.+/) # => [foo,fooo]
|
44
|
+
|
45
|
+
dallish.find_all_by(/foo.+/) # =>
|
46
|
+
|
47
|
+
dallish.delete_all_by(/foo.+/)
|
48
|
+
```
|
49
|
+
|
50
|
+
## License
|
51
|
+
|
52
|
+
The MIT License
|
53
|
+
>>>>>>> 37ae587... Initial version
|
data/dallish.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
$:.push File.expand_path("../lib", __FILE__)
|
4
|
+
require "dallish/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "dallish"
|
8
|
+
s.version = Dallish::VERSION
|
9
|
+
s.authors = ["Kazuhiro Sera"]
|
10
|
+
s.email = ["seratch@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/seratch/dallish"
|
12
|
+
s.summary = %q{An extended Dalli for memcached 1.4.x}
|
13
|
+
s.description = %q{An extended Dalli for memcached 1.4.x}
|
14
|
+
|
15
|
+
s.rubyforge_project = "dallish"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.required_ruby_version = '>= 1.9.0'
|
23
|
+
s.add_dependency('dalli')
|
24
|
+
|
25
|
+
end
|
26
|
+
|
data/lib/dallish.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'dalli'
|
4
|
+
require 'dallish/client'
|
5
|
+
|
6
|
+
module Dallish
|
7
|
+
|
8
|
+
def self.new(params_or_dalli_servers = {}, dalli_options = {})
|
9
|
+
if is_the_arg_dalli_servers(params_or_dalli_servers)
|
10
|
+
params = {:servers => params_or_dalli_servers, :dalli_options => dalli_options}
|
11
|
+
else
|
12
|
+
params = params_or_dalli_servers
|
13
|
+
end
|
14
|
+
Dallish::Client.new(params)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def self.is_the_arg_dalli_servers(params_or_dalli_servers)
|
20
|
+
params_or_dalli_servers and
|
21
|
+
(params_or_dalli_servers.is_a?(String) or params_or_dalli_servers.is_a?(Array))
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'logger'
|
4
|
+
require 'dalli'
|
5
|
+
require 'net/telnet'
|
6
|
+
|
7
|
+
module Dallish
|
8
|
+
class Client
|
9
|
+
|
10
|
+
attr_accessor :servers, :log, :dalli
|
11
|
+
|
12
|
+
def initialize(params)
|
13
|
+
raise ArgumentError.new('`params` should be a Hash value.') unless params.is_a? Hash
|
14
|
+
|
15
|
+
# log settings
|
16
|
+
@log = Logger.new(params[:log_output] || STDOUT)
|
17
|
+
@log.level = params[:log_level] || Logger::INFO
|
18
|
+
|
19
|
+
# memcached server settings
|
20
|
+
if params[:servers] and params[:servers].is_a?(String)
|
21
|
+
@servers = [params[:servers]]
|
22
|
+
else
|
23
|
+
@servers = params[:servers] || ['127.0.0.1:11211']
|
24
|
+
end
|
25
|
+
@dalli_options = params[:options] || {}
|
26
|
+
@dalli = Dalli::Client.new(@servers, @dalli_options)
|
27
|
+
|
28
|
+
# cannot support memcached 1.6 or higher
|
29
|
+
self.servers.each { |server|
|
30
|
+
version = telnet(server, :prompt => /\n/).cmd('version')
|
31
|
+
self.log.debug "version: #{version}"
|
32
|
+
|
33
|
+
/VERSION (?<major_version>\d+\.\d+).*/ =~ version
|
34
|
+
if major_version and major_version.to_f >= 1.6
|
35
|
+
raise ArgumentError.new("Sorry, Dallish doesn't support memcached 1.6 or higher version.")
|
36
|
+
end
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
def telnet(server, options = {})
|
41
|
+
(host, port) = server.split(':')
|
42
|
+
self.log.debug "target memcahced server: #{host}:#{port}"
|
43
|
+
telnet = Net::Telnet::new(
|
44
|
+
'Host' => host,
|
45
|
+
'Port' => port,
|
46
|
+
'Prompt' => options[:prompt] || /(^END$)/,
|
47
|
+
'Timeout' => options[:timeout] || 3
|
48
|
+
)
|
49
|
+
end
|
50
|
+
|
51
|
+
def all_keys()
|
52
|
+
self.servers.flat_map { |server|
|
53
|
+
telnet = telnet(server)
|
54
|
+
begin
|
55
|
+
slab_ids = telnet.cmd("stats slabs").split("\n").map { |line|
|
56
|
+
/STAT (?<slab_id>\d+):.+/ =~ line
|
57
|
+
slab_id
|
58
|
+
}.reject { |e| e.nil? }.uniq
|
59
|
+
|
60
|
+
slab_ids.flat_map { |slab_id|
|
61
|
+
telnet.cmd("stats cachedump #{slab_id} 1000000").split("\n").map { |line|
|
62
|
+
/ITEM (?<key>.+) \[\d+ b; \d+ s\]/ =~ line
|
63
|
+
key
|
64
|
+
}
|
65
|
+
}
|
66
|
+
rescue Exception => e
|
67
|
+
self.log.info "Failed to get keys because #{e.to_s}"
|
68
|
+
[]
|
69
|
+
ensure
|
70
|
+
telnet.close()
|
71
|
+
end
|
72
|
+
}.reject { |e| e.nil? }.uniq
|
73
|
+
end
|
74
|
+
|
75
|
+
def find_keys_by(regexp)
|
76
|
+
all_keys.select { |key|
|
77
|
+
if key.is_a?(Array) then
|
78
|
+
puts "#{key},#{key.class}"
|
79
|
+
end
|
80
|
+
regexp.match(key) }
|
81
|
+
end
|
82
|
+
|
83
|
+
def delete_all_by(regexp)
|
84
|
+
find_keys_by(regexp).each do |key|
|
85
|
+
self.dalli.delete(key)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def find_all_by(regexp)
|
90
|
+
find_keys_by(regexp).inject({}) { |result, key|
|
91
|
+
result[key] = self.dalli.get(key)
|
92
|
+
result
|
93
|
+
}
|
94
|
+
end
|
95
|
+
|
96
|
+
def method_missing(name, *args, &block)
|
97
|
+
self.dalli.send(name, *args, &block)
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'dallish'
|
4
|
+
require 'logger'
|
5
|
+
|
6
|
+
describe Dallish do
|
7
|
+
|
8
|
+
before do
|
9
|
+
@dallish = Dallish.new(
|
10
|
+
:servers => ["localhost:11211", 'memcached:11211'],
|
11
|
+
:log_level => Logger::INFO
|
12
|
+
)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should be also created with Dalli style args" do
|
16
|
+
dallish = Dallish.new('localhost:11211')
|
17
|
+
dallish.should_not be(nil)
|
18
|
+
keys = dallish.all_keys
|
19
|
+
keys.should_not be(nil)
|
20
|
+
keys.size.should > 0
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should be created" do
|
24
|
+
dallish = Dallish.new(:servers => "localhost:11211")
|
25
|
+
dallish.should_not be(nil)
|
26
|
+
keys = dallish.all_keys
|
27
|
+
keys.should_not be(nil)
|
28
|
+
keys.size.should > 0
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should get all keys" do
|
32
|
+
keys = @dallish.all_keys
|
33
|
+
keys.should_not be(nil)
|
34
|
+
keys.size.should > 0
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should find_keys_by regexp" do
|
38
|
+
(1..100).each { |i| @dallish.set("foo_#{i}", 123) }
|
39
|
+
keys = @dallish.find_keys_by(/foo_\d+$/)
|
40
|
+
keys.size.should >= 100
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should delete_all_by regexp" do
|
44
|
+
(1..100).each { |i| @dallish.set("to_delete_#{i}", 123) }
|
45
|
+
keys = @dallish.find_keys_by(/to_delete_\d+$/)
|
46
|
+
keys.size.should >= 100
|
47
|
+
|
48
|
+
@dallish.delete_all_by(/to_delete_\d+$/)
|
49
|
+
|
50
|
+
keys = @dallish.find_keys_by(/to_delete_\d+$/)
|
51
|
+
keys.size.should == 0
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should find_all_by regexp" do
|
55
|
+
(1..100).each { |i| @dallish.set("to_get_#{i}", 123) }
|
56
|
+
keys = @dallish.find_keys_by(/to_get_\d+$/)
|
57
|
+
keys.size.should >= 100
|
58
|
+
|
59
|
+
results = @dallish.find_all_by(/to_get_\d+$/)
|
60
|
+
results.keys.size.should >= 100
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should have dalli's methods" do
|
64
|
+
@dallish.set("foo", 123)
|
65
|
+
@dallish.set("bar", 234)
|
66
|
+
@dallish.get("foo").should == 123
|
67
|
+
@dallish.get_multi("foo", "bar").size.should == 2
|
68
|
+
@dallish.fetch("foo").should == 123
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dallish
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.4.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kazuhiro Sera
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-31 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: dalli
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: An extended Dalli for memcached 1.4.x
|
31
|
+
email:
|
32
|
+
- seratch@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
- dallish.gemspec
|
42
|
+
- lib/dallish.rb
|
43
|
+
- lib/dallish/client.rb
|
44
|
+
- lib/dallish/version.rb
|
45
|
+
- spec/dallish_spec.rb
|
46
|
+
homepage: https://github.com/seratch/dallish
|
47
|
+
licenses: []
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: 1.9.0
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project: dallish
|
66
|
+
rubygems_version: 1.8.24
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: An extended Dalli for memcached 1.4.x
|
70
|
+
test_files:
|
71
|
+
- spec/dallish_spec.rb
|