redis-namespace 0.1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of redis-namespace might be problematic. Click here for more details.

@@ -0,0 +1,27 @@
1
+ redis-namespace
2
+ ---------------
3
+
4
+ Requires the redis gem.
5
+
6
+ Namespaces all Redis calls.
7
+
8
+ r = Redis::Namespace.new(:ns, :redis => @r)
9
+ r['foo'] = 1000
10
+
11
+ This will perform the equivalent of:
12
+
13
+ redis-cli set ns:foo 1000
14
+
15
+ Useful when you have multiple systems using Redis differently in your app.
16
+
17
+
18
+ Installation
19
+ ============
20
+
21
+ $ gem install redis-namespace --source=http://gemcutter.org
22
+
23
+
24
+ Author
25
+ =====
26
+
27
+ Chris Wanstrath :: chris@ozmm.org
@@ -0,0 +1,15 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "redis-namespace"
5
+ gemspec.summary = "Namespaces Redis commands."
6
+ gemspec.description = "Namespaces Redis commands."
7
+ gemspec.email = "chris@ozmm.org"
8
+ gemspec.homepage = "http://github.com/defunkt/redis-namespace"
9
+ gemspec.authors = ["Chris Wanstrath"]
10
+ gemspec.version = '0.1.0'
11
+ end
12
+ rescue LoadError
13
+ puts "Jeweler not available. Install it with:"
14
+ puts "gem install jeweler"
15
+ end
@@ -0,0 +1,102 @@
1
+ require 'redis'
2
+
3
+ class Redis
4
+ class Namespace
5
+ # Generated from http://code.google.com/p/redis/wiki/CommandReference
6
+ # using the following jQuery:
7
+ #
8
+ # $('.vt li a').map(function(){ return $(this).text().toLowerCase() }).sort()
9
+ COMMANDS = [
10
+ "auth",
11
+ "bgsave",
12
+ "dbsize",
13
+ "decr",
14
+ "decrby",
15
+ "del",
16
+ "exists",
17
+ "expire",
18
+ "flushall",
19
+ "flushdb",
20
+ "get",
21
+ "getset",
22
+ "incr",
23
+ "incrby",
24
+ "info",
25
+ "keys",
26
+ "lastsave",
27
+ "lindex",
28
+ "llen",
29
+ "lpop",
30
+ "lpush",
31
+ "lrange",
32
+ "lrem",
33
+ "lset",
34
+ "ltrim",
35
+ "mget",
36
+ "monitor",
37
+ "move",
38
+ "quit",
39
+ "randomkey",
40
+ "rename",
41
+ "renamenx",
42
+ "rpop",
43
+ "rpush",
44
+ "sadd",
45
+ "save",
46
+ "scard",
47
+ "sdiff",
48
+ "sdiffstore",
49
+ "select",
50
+ "set",
51
+ "setnx",
52
+ "shutdown",
53
+ "sinter",
54
+ "sinterstore",
55
+ "sismember",
56
+ "slaveof",
57
+ "smembers",
58
+ "smove",
59
+ "sort",
60
+ "srem",
61
+ "sunion",
62
+ "sunionstore",
63
+ "ttl",
64
+ "type",
65
+ "[]",
66
+ "[]="
67
+ ]
68
+
69
+ def initialize(namespace, options = {})
70
+ @namespace = namespace
71
+ @redis = options[:redis]
72
+ end
73
+
74
+ # Ruby defines a now deprecated type method so we need to override it here
75
+ # since it will never hit method_missing
76
+ def type(key)
77
+ call_command(['type', key])
78
+ end
79
+
80
+ def mapped_mget(*keys)
81
+ result = {}
82
+ mget(*keys).each do |value|
83
+ key = keys.shift
84
+ result.merge!(key => value) unless value.nil?
85
+ end
86
+ result
87
+ end
88
+
89
+ def mget(*keys)
90
+ keys = keys.map { |key| "#{@namespace}:#{key}"} if @namespace
91
+ call_command([:mget] + keys)
92
+ end
93
+
94
+ def method_missing(command, *args, &block)
95
+ if COMMANDS.include?(command.to_s) && args[0]
96
+ args[0] = "#{@namespace}:#{args[0]}"
97
+ end
98
+
99
+ @redis.send(command, *args, &block)
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,45 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'redis/namespace'
3
+ require 'logger'
4
+
5
+ describe "redis" do
6
+ before(:all) do
7
+ # use database 15 for testing so we dont accidentally step on you real data
8
+ @r = Redis.new :db => 15
9
+ end
10
+
11
+ before(:each) do
12
+ @r['foo'] = 'bar'
13
+ end
14
+
15
+ after(:each) do
16
+ @r.flushdb
17
+ end
18
+
19
+ after(:all) do
20
+ @r.quit
21
+ end
22
+ it "should be able to use a namespace" do
23
+ r = Redis::Namespace.new(:ns, :redis => @r)
24
+ r.flushdb
25
+
26
+ r['foo'].should == nil
27
+ r['foo'] = 'chris'
28
+ r['foo'].should == 'chris'
29
+ @r['foo'] = 'bob'
30
+ @r['foo'].should == 'bob'
31
+
32
+ r.incr('counter', 2)
33
+ r['counter'].to_i.should == 2
34
+ @r['counter'].should == nil
35
+ end
36
+
37
+ it "should be able to use a namespace with mget" do
38
+ r = Redis::Namespace.new(:ns, :redis => @r)
39
+
40
+ r['foo'] = 1000
41
+ r['bar'] = 2000
42
+ r.mapped_mget('foo', 'bar').should == { 'foo' => '1000', 'bar' => '2000' }
43
+ r.mapped_mget('foo', 'baz', 'bar').should == { 'foo' => '1000', 'bar' => '2000' }
44
+ end
45
+ end
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ $TESTING=true
3
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
4
+ require 'redis'
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis-namespace
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Wanstrath
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-13 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Namespaces Redis commands.
17
+ email: chris@ozmm.org
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.md
24
+ files:
25
+ - README.md
26
+ - Rakefile
27
+ - lib/redis/namespace.rb
28
+ - spec/redis_spec.rb
29
+ - spec/spec_helper.rb
30
+ has_rdoc: true
31
+ homepage: http://github.com/defunkt/redis-namespace
32
+ licenses: []
33
+
34
+ post_install_message:
35
+ rdoc_options:
36
+ - --charset=UTF-8
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.3.5
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Namespaces Redis commands.
58
+ test_files:
59
+ - spec/redis_spec.rb
60
+ - spec/spec_helper.rb