dalli-keys-match 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2b6875a0ca5dc289edfd2ee3a9000a2ac338558b
4
- data.tar.gz: a89d71e02345a54730ff8d939f718ffe8320b626
3
+ metadata.gz: 3493c1811a60bdfae98deb7224fa1aca3a83f4d8
4
+ data.tar.gz: 0d8ddc214a7d12ebbb01c0c36b8b5e71cf980ea5
5
5
  SHA512:
6
- metadata.gz: 312ed9714d7ad958fba934410622a0702048b2e455948041235d4e32d25549e4f4aa2aba24c3f8a14666b034371275f100b257507fa28f34797f17eeee857116
7
- data.tar.gz: 6bcd5241247222d179a2c3113a6f52a7ad7733a9994ec682edc8aa497af6bb3f0f8c54f94a38470e02162ba75ec24e1df611c4fd89581573fa4c6927df321c2e
6
+ metadata.gz: e5493d8abc7690e992b3a1b3ff659fdf7573e19251963fb7691c73884af7ac79649f5294919abff2a3657a2a7a06d0d1a1adf7c164d7cedf62cbf72584038b87
7
+ data.tar.gz: ae209493e3fc5efba013ead8770b72a060287d66a9ebfa00abac2a5ebf82429856b21ef2776cb3e2f294f09adb33d3e7a8537301860f7cf62fe0f4af23167c7f
data/.gitignore CHANGED
@@ -1,3 +1,5 @@
1
+ *.gem
2
+ *.rbc
1
3
  /.bundle/
2
4
  /.yardoc
3
5
  /Gemfile.lock
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Marcos G. Zimmermann
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -25,11 +25,8 @@ Or install it yourself as:
25
25
  require 'dalli/keys_match'
26
26
 
27
27
  > client = Dalli::Client.new('localhost:11211')
28
- => #<Dalli::Client:0x007fcb14afe830 @servers=["localhost:11211"], @options={}, @ring=nil>
29
28
  > client.set('dalli-keys-match-1' , 1)
30
- => 10163216984091656192
31
- > client.set('dalli-keys-match-2' , 2)
32
- => 10235274578129584128
29
+ > client.set('dalli-keys-match-2' , 2)
33
30
  > client.keys(/dalli-keys-match-\d/)
34
31
  => ["dalli-keys-match-2", "dalli-keys-match-1"]
35
32
  > client.keys('dalli-keys-match-')
@@ -40,6 +37,25 @@ require 'dalli/keys_match'
40
37
  => ["dalli-keys-match-2"]
41
38
  ```
42
39
 
40
+ Gem also handles namespaces. Keys are normalized and filters are always optimized to only look into the namespace
41
+ ```
42
+ > dc1 = Dalli::Client.new('localhost:11211')
43
+ > dc2 = Dalli::Client.new('localhost:11211', namespace: 'marcosgz')
44
+ > dc1.set('zimmermann', 'last-name')
45
+ > dc2.set('zimmermann', 'last-name')
46
+ > dc1.keys(/zimmermann/)
47
+ => ["zimmermann", "marcosgz:zimmermann"]
48
+ > dc2.keys(/zimmermann/)
49
+ => ["zimmermann"]
50
+ > dc2.keys_with_namespace(/zimmermann/)
51
+ => ["marcosgz:zimmermann"]
52
+ > dc2.delete_matched(/^zimmermann/)
53
+ => 1
54
+ > dc1.keys(/zimmermann/)
55
+ => ["zimmermann"]
56
+ ```
57
+
58
+
43
59
  ### Optional Configuration
44
60
  ```
45
61
  Dalli::KeysMatch.config.telnet = {
@@ -19,14 +19,11 @@ module Dalli
19
19
  [].tap do |keys|
20
20
  telnet.cmd("String" => "stats cachedump #{id} #{size}").split("\n").each do |line|
21
21
  if /ITEM (.+) \[\d+ b; \d+ s\]/ =~ line
22
- case pattern.class.name
23
- when 'NilClass'
22
+ if pattern.nil?
24
23
  keys << $1
25
- when 'Regexp'
24
+ else
26
25
  cache_key = $1
27
26
  keys << cache_key if cache_key =~ pattern
28
- else
29
- keys << $1 if $1[pattern.to_s]
30
27
  end
31
28
  end
32
29
  end
@@ -39,6 +36,8 @@ module Dalli
39
36
  @telnet = nil
40
37
  end
41
38
 
39
+ protected
40
+
42
41
  def telnet
43
42
  @telnet ||= begin
44
43
  configs = Dalli::KeysMatch.config.telnet(
@@ -51,7 +50,8 @@ module Dalli
51
50
  end
52
51
 
53
52
  module Client
54
- def keys(pattern = nil)
53
+ def keys_with_namespace(pattern = nil)
54
+ re = stats_pattern_regexp(pattern)
55
55
  result = []
56
56
  ring.servers.each do |server|
57
57
  next unless server.alive?
@@ -61,19 +61,48 @@ module Dalli
61
61
  r
62
62
  end
63
63
  slabs.each do |id, size|
64
- result.push(*server.stats_cachedump(id, size, pattern))
64
+ result.push(*server.stats_cachedump(id, size, re))
65
65
  end
66
66
  server.close_telnet!
67
67
  end
68
68
  result
69
69
  end
70
70
 
71
+ def keys(pattern = nil)
72
+ keys_with_namespace(pattern).map do |key|
73
+ key_without_namespace(key)
74
+ end
75
+ end
76
+
71
77
  def delete_matched(pattern)
72
78
  return 0 if pattern.nil?
73
79
 
74
80
  matches = keys(pattern)
75
81
  matches.map { |k| delete(k) }.select { |r| r }.size
76
82
  end
83
+
84
+ protected
85
+
86
+ def stats_pattern_regexp(pattern)
87
+ return if namespace.nil? && pattern.nil?
88
+
89
+ if namespace
90
+ if pattern.is_a?(Regexp)
91
+ opts, source = pattern.to_s.scan(/^\(\?([a-z\-]{3,4})\:(.*)\)$/m).flatten
92
+ source_with_namespace = \
93
+ if source.sub!(/^\^/,'')
94
+ "^#{namespace}:#{source}"
95
+ else
96
+ "^#{namespace}:.*#{source}"
97
+ end
98
+ Regexp.new("(?#{opts}:#{source_with_namespace})")
99
+ else
100
+ Regexp.new("#{namespace}:.*#{pattern}")
101
+ end
102
+ else
103
+ Regexp.new(pattern.to_s)
104
+ end
105
+ end
77
106
  end
78
107
  end
79
108
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  module Dalli
3
3
  module KeysMatch
4
- VERSION = '0.2.0'
4
+ VERSION = '1.0.0'
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dalli-keys-match
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcos G. Zimmermann
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-05 00:00:00.000000000 Z
11
+ date: 2017-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-telnet
@@ -98,6 +98,7 @@ files:
98
98
  - ".rspec"
99
99
  - ".travis.yml"
100
100
  - Gemfile
101
+ - LICENSE
101
102
  - README.md
102
103
  - Rakefile
103
104
  - bin/console