memcache_viewer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Bachue Zhou
2
+
3
+ MIT License
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
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # MemcacheViewer
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'memcache_viewer'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install memcache_viewer
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,125 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'socket'
4
+ require 'pathname'
5
+ require 'commandline'
6
+
7
+ OptionParser.new do |opts|
8
+ opts.banner = "Usage: #{$0} [options] command [key] [options]"
9
+
10
+ opts.on('-e', '--environment Environment', String, 'Rails environment') do |env|
11
+ RAILS_ENV = env
12
+ end
13
+
14
+ RAILS_ENV = 'production' unless defined? RAILS_ENV
15
+ end.order!
16
+
17
+ ENV['RAILS_ENV'] = RAILS_ENV
18
+
19
+ if ARGV.empty?
20
+ $stderr.puts 'No command specified'
21
+ exit
22
+ end
23
+
24
+ # --------------- Import Rails ENV ---------------
25
+ def in_rails_application?(path = Pathname.new(Dir.pwd))
26
+ if File.file?("#{path}/config/boot.rb") && File.file?("#{path}/config/environment.rb")
27
+ path
28
+ elsif !path.root?
29
+ in_rails_application? path.parent
30
+ end
31
+ end
32
+
33
+ if (rails_path = in_rails_application? || in_rails_application?(Pathname.new(File.dirname(__FILE__))))
34
+ Dir.chdir rails_path
35
+ require "#{rails_path}/config/boot"
36
+ require "#{rails_path}/config/environment"
37
+ end
38
+
39
+ # --------------- Validate CACHE ---------------
40
+ unless defined?(CACHE) && defined?(MemCache) && CACHE.is_a?(MemCache)
41
+ $stderr.puts 'No MemCache found, You must assign a MemCache object to CACHE constant in Rails Env.'
42
+ exit
43
+ end
44
+
45
+ server = CACHE.servers.first
46
+
47
+ class EnumMemcacheKeys
48
+ MEMCACHE_STATS = ["END", "OK", "DELETE", "ERROR"]
49
+
50
+ class << self
51
+ def keys(host, port)
52
+ keys = []
53
+ open(host, port) do |s|
54
+ buff = items(host, port)
55
+
56
+ items = []
57
+ buff.each { |b| items << b.split(':')[1] }
58
+
59
+ items.each do |i|
60
+ s.send("stats cachedump #{i} 0\r\n", 0)
61
+ until MEMCACHE_STATS.include?(line = s.gets.strip) do
62
+ keys << line.split(' ')[1]
63
+ end
64
+ end
65
+ end
66
+ keys
67
+ end
68
+
69
+ def data(host, port)
70
+ data = []
71
+ keys(host, port).each do |key|
72
+ begin
73
+ value = CACHE.get(key.sub(/^#{CACHE.namespace}:/, ''))
74
+ rescue
75
+ value = CACHE.get(key.sub(/^#{CACHE.namespace}:/, ''), :raw => true)
76
+ end
77
+ data << [key, value]
78
+ end
79
+ data
80
+ end
81
+
82
+ def get(host, port, key)
83
+ CACHE.get(key.sub(/^#{CACHE.namespace}:/, ''))
84
+ rescue
85
+ CACHE.get(key.sub(/^#{CACHE.namespace}:/, ''), :raw => true)
86
+ end
87
+
88
+ private
89
+ def open(host, port)
90
+ s = TCPSocket.open host, port
91
+ yield s if block_given?
92
+ ensure
93
+ s.close if s
94
+ end
95
+
96
+ def items(host = 'localhost', port = '11211')
97
+ i = []
98
+ open(host, port) do |s|
99
+ s.send("stats items\r\n", 0)
100
+
101
+ until MEMCACHE_STATS.include?(line = s.gets.strip) do
102
+ i << line
103
+ end
104
+ end
105
+
106
+ i
107
+ end
108
+ end
109
+ end
110
+
111
+ command = ARGV.shift
112
+ case command
113
+ when 'keys'
114
+ EnumMemcacheKeys.keys(server.host, server.port).each {|key| puts key}
115
+ when 'data'
116
+ EnumMemcacheKeys.data(server.host, server.port).each {|key, value| puts "#{key}: #{value.inspect}"}
117
+ when 'get'
118
+ if ARGV.empty?
119
+ $stderr.puts 'No key specified'
120
+ exit
121
+ end
122
+ puts EnumMemcacheKeys.get(server.host, server.port, ARGV.first).inspect
123
+ else
124
+ $stderr.puts "No command: #{command}"
125
+ end
@@ -0,0 +1,3 @@
1
+ module MemcacheViewer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "memcache_viewer/version"
2
+
3
+ module MemcacheViewer
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'memcache_viewer/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "memcache_viewer"
8
+ gem.version = MemcacheViewer::VERSION
9
+ gem.authors = ["Bachue Zhou"]
10
+ gem.email = ["bachue.shu@gmail.com"]
11
+ gem.description = %q{A gem to query content in memcache}
12
+ gem.summary = %q{A gem to query content in memcache}
13
+ gem.homepage = "http://bachue.is-programmer.com"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'OptionParser', '~> 0.5'
21
+ gem.add_development_dependency 'rake'
22
+ gem.add_development_dependency 'memcache-client'
23
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: memcache_viewer
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Bachue Zhou
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2013-01-05 00:00:00 +08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ version_requirements: &id001 !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ~>
24
+ - !ruby/object:Gem::Version
25
+ segments:
26
+ - 0
27
+ - 5
28
+ version: "0.5"
29
+ requirement: *id001
30
+ prerelease: false
31
+ name: OptionParser
32
+ type: :runtime
33
+ - !ruby/object:Gem::Dependency
34
+ version_requirements: &id002 !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ segments:
39
+ - 0
40
+ version: "0"
41
+ requirement: *id002
42
+ prerelease: false
43
+ name: rake
44
+ type: :development
45
+ - !ruby/object:Gem::Dependency
46
+ version_requirements: &id003 !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ requirement: *id003
54
+ prerelease: false
55
+ name: memcache-client
56
+ type: :development
57
+ description: A gem to query content in memcache
58
+ email:
59
+ - bachue.shu@gmail.com
60
+ executables:
61
+ - memcache-viewer
62
+ extensions: []
63
+
64
+ extra_rdoc_files: []
65
+
66
+ files:
67
+ - .gitignore
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/memcache-viewer
73
+ - lib/memcache_viewer.rb
74
+ - lib/memcache_viewer/version.rb
75
+ - memcache_viewer.gemspec
76
+ has_rdoc: true
77
+ homepage: http://bachue.is-programmer.com
78
+ licenses: []
79
+
80
+ post_install_message:
81
+ rdoc_options: []
82
+
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ requirements: []
100
+
101
+ rubyforge_project:
102
+ rubygems_version: 1.3.6
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: A gem to query content in memcache
106
+ test_files: []
107
+