dalli-extra 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ data.tar.gz: debb1eebd5ba7e5282ac3a92402b723fbbbbf09d
4
+ metadata.gz: 1107e6ad6ce604814f860a8f1681eb90af6f7424
5
+ SHA512:
6
+ data.tar.gz: 396f50dbfb98e61dddbe745399286124bf4f2ef2ce1bb5bee4ed4e7d95f90c99e043de677f5cdcdaf3d614f92f8250d1023843f7c10ad7de91d25c3c674cd302
7
+ metadata.gz: 40882ec150ae3ff34b05cd692b086c82740766163f0b242d815210f95a3c2c0ccb834d1be77a19a034cfe08ab382377065fdd020c22f70b07cc4399f3f1b2b5f
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,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dalli-extra.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Sumit Maheshwari
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,46 @@
1
+ # Dalli::Extra
2
+
3
+ A small gem which adds some much needed methods to Dalli. The idea of this gem came after reading this post http://www.darkcoding.net/software/memcached-list-all-keys/ and seeing a similar gem "https://github.com/seratch/dallish"
4
+
5
+ ## Note
6
+
7
+ As this gem creates telent connections, its slower than Dalli. So its good fit for non-production uses like debugging, development or in background jobs.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'dalli-extra'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install dalli-extra
22
+
23
+ ## Usage
24
+ ```ruby
25
+ require 'dalli'
26
+ require 'dalli-extra'
27
+ dc = Dalli::Client.new(['localhost:11211','localhost:11212'], :expires_in => 300)
28
+
29
+ all_keys = dc.keys
30
+ regex_matching_keys = dc.keys(/abc/)
31
+ string_containing_keys = dc.keys('abc')
32
+
33
+ all_pairs = dc.pairs
34
+ regex_matching_pairs = dc.pairs(/abc/)
35
+ string_containing_pairs = dc.pairs('abc')
36
+
37
+ count = dc.delete_matched(/abc/) or
38
+ count = dc.delete_macthed('abc')
39
+
40
+ ## Contributing
41
+
42
+ 1. Fork it
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dalli/extra/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dalli-extra"
8
+ spec.version = Dalli::Extra::VERSION
9
+ spec.authors = ["Sumit Maheshwari"]
10
+ spec.email = ["sumitm@qubole.com"]
11
+ spec.description = 'Adding some extra methods to Dalli'
12
+ spec.summary = 'Adding some extra methods to Dalli'
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_dependency "dalli", "~> 2.7"
24
+ end
@@ -0,0 +1,5 @@
1
+ module Dalli
2
+ module Extra
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,81 @@
1
+ require 'rubygems'
2
+ require 'net/telnet'
3
+
4
+ module Dalli
5
+ class Client
6
+
7
+ # Return keys in Array
8
+ def keys(reg_exp = nil)
9
+ keys = []
10
+ regex = (reg_exp.nil?) ? nil : (reg_exp.is_a?(Regexp)) ? reg_exp : Regexp.new(reg_exp)
11
+ @servers.each do |server|
12
+ connection = telnet(server)
13
+ begin
14
+ matches = connection.cmd("String" => "stats items").scan(/STAT items:(\d+):number (\d+)/)
15
+
16
+ slabs = matches.inject([]) { |items, item| items << Hash[*['id','items'].zip(item).flatten]; items }
17
+
18
+ slabs.each do |slab|
19
+ # Below written code is not kept DRY to avoid if-else compariosions on each key
20
+ if reg_exp.nil?
21
+ connection.cmd("String" => "stats cachedump #{slab['id']} #{slab['items']}").split("\n").each do |line|
22
+ if /ITEM (.+) \[\d+ b; \d+ s\]/ =~ line
23
+ keys << $1
24
+ end
25
+ end
26
+ elsif reg_exp.is_a?(Regexp)
27
+ connection.cmd("String" => "stats cachedump #{slab['id']} #{slab['items']}").split("\n").each do |line|
28
+ if /ITEM (.+) \[\d+ b; \d+ s\]/ =~ line
29
+ cache_key = $1
30
+ keys << cache_key if regex.match(cache_key)
31
+ end
32
+ end
33
+ else # return keys which have the given string as a substring
34
+ connection.cmd("String" => "stats cachedump #{slab['id']} #{slab['items']}").split("\n").each do |line|
35
+ if /ITEM (.+) \[\d+ b; \d+ s\]/ =~ line
36
+ keys << $1 if $1[reg_exp]
37
+ end
38
+ end
39
+ end
40
+ end
41
+ rescue Exception => e
42
+ Dalli.logger.error("Failed to get keys because #{e.to_s}")
43
+ ensure
44
+ connection.close() unless connection.nil?
45
+ end
46
+ end
47
+ keys
48
+ end
49
+
50
+ # Returns the key-value pairs as Array of Arrays
51
+ def pairs(reg_exp = nil)
52
+ _keys = keys(reg_exp)
53
+ _keys.zip(_keys.map{|k| get(k)})
54
+ end
55
+
56
+ # Returns a Int, representing number of deleted keys
57
+ def delete_matched(reg_exp)
58
+ _keys = keys(reg_exp)
59
+ _keys.map{|k| delete(k)}.reject{|y| !y}.count
60
+ end
61
+
62
+ private
63
+
64
+ def telnet(server)
65
+ (host, port) = server.split(':')
66
+ telnet = nil
67
+ Dalli.logger.info("target memcahced server: #{host}:#{port}")
68
+ begin
69
+ telnet = Net::Telnet::new(
70
+ 'Host' => host,
71
+ 'Port' => port,
72
+ 'Prompt' => /(^END$)/,
73
+ 'Timeout' => 3
74
+ )
75
+ rescue Errno::ECONNREFUSED
76
+ Dalli.logger.error("Error: " + $!)
77
+ end
78
+ telnet
79
+ end
80
+ end
81
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dalli-extra
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sumit Maheshwari
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2014-10-21 00:00:00 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ prerelease: false
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: "1.3"
22
+ type: :development
23
+ version_requirements: *id001
24
+ - !ruby/object:Gem::Dependency
25
+ name: rake
26
+ prerelease: false
27
+ requirement: &id002 !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - &id004
30
+ - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id002
35
+ - !ruby/object:Gem::Dependency
36
+ name: dalli
37
+ prerelease: false
38
+ requirement: &id003 !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ version: "2.7"
43
+ type: :runtime
44
+ version_requirements: *id003
45
+ description: Adding some extra methods to Dalli
46
+ email:
47
+ - sumitm@qubole.com
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ extra_rdoc_files: []
53
+
54
+ files:
55
+ - .gitignore
56
+ - Gemfile
57
+ - LICENSE.txt
58
+ - README.md
59
+ - Rakefile
60
+ - dalli-extra.gemspec
61
+ - lib/dalli/extra.rb
62
+ - lib/dalli/extra/version.rb
63
+ homepage: ""
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+
68
+ post_install_message:
69
+ rdoc_options: []
70
+
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - *id004
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - *id004
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 2.1.7
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Adding some extra methods to Dalli
86
+ test_files: []
87
+