mobb-redis 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9f2f43a7fcf210c2fa427c84058f219cbc99c096d2d6a2a64e3ff91186f80edf
4
+ data.tar.gz: 58c8d4645b09b32ac82055d55ef6fe995b2717dd75d6a8b5875f961efd976e9b
5
+ SHA512:
6
+ metadata.gz: 97444f8f70b6bde96675251e394d070de0375f5f26e6fbd951572a84ac04e5dab2b518dfe13c4571ebd884cb1c54fdc209db10a347b09242fdb0a5fb0c20d997
7
+ data.tar.gz: efbb8a0c40125080ebfd07d17dc9b21e2a567d8574d7ffdee43c689fa7223b74ad80cdbe481be241b08bce6d506bbf2c4d1aa9f98f00e0c2e9cb92945010cf40
@@ -0,0 +1,3 @@
1
+ Gemfile.lock
2
+ *.gem
3
+ tmp/
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ script: 'bundle exec rake'
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - ruby-head
7
+ - jruby-head
8
+
9
+ matrix:
10
+ allow_failures:
11
+ - rvm: jruby-head
12
+ - rvm: ruby-head
@@ -0,0 +1,16 @@
1
+ redis-sinatra 1.4.1 (2017-12-15)
2
+ --------------------------------------------------------------------------------
3
+
4
+ * Remove Travis builds for Ruby 1.9 on jruby and rbx
5
+ Felipe Batista
6
+
7
+ * Remove dependency to Sinatra
8
+
9
+ let users require Sinatra the way it fits to their project
10
+ Felipe Batista
11
+
12
+ * Loosen gem dependencies
13
+ Tom Scott
14
+
15
+ * Initial implementation
16
+ Luca Guidi
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 - 2011 Luca Guidi
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,47 @@
1
+ # Redis stores for Mobb
2
+
3
+ __`mobb-redis`__ provides a Redis backed cache store for __Mobb__. See the main [redis-store readme](https://github.com/redis-store/redis-store) for general guidelines.
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ # Gemfile
9
+ gem 'mobb-redis'
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ ```ruby
15
+ require 'mobb'
16
+ require 'mobb-redis'
17
+
18
+ class MyApp < Mobb::Base
19
+ register Mobb::Cache
20
+
21
+ on 'hi' do
22
+ settings.cache.fetch('greet') { 'Hello, World!' }
23
+ end
24
+ end
25
+ ```
26
+
27
+ Keep in mind that the above fetch will return `"OK"` on success, not the return of the block.
28
+
29
+ ## Running tests
30
+
31
+ ```ruby
32
+ gem install bundler
33
+ git clone https://github.com/kinoppyd/mobb-redis
34
+ cd mobb-redis
35
+ bundle install
36
+ bundle exec rake
37
+ ```
38
+
39
+ If you are on **Snow Leopard** you have to run `env ARCHFLAGS="-arch x86_64" bundle exec rake`
40
+
41
+ ## Copyright
42
+
43
+ Original
44
+ 2009 - 2013 Luca Guidi - [http://lucaguidi.com](http://lucaguidi.com), released under the MIT license]
45
+
46
+ Modify
47
+ kinoppyd
@@ -0,0 +1,45 @@
1
+ require 'bundler/setup'
2
+ require 'rake'
3
+ require 'bundler/gem_tasks'
4
+ require 'redis-store/testing/tasks'
5
+ require 'mobb-redis/version'
6
+ require 'date'
7
+
8
+ desc 'Generate the changelog based on git history'
9
+ task :changelog, :from, :to do |_t, args|
10
+
11
+ #from = args[:from] || `git describe --tags --abbrev=0`.strip
12
+ #to = args[:to] || 'HEAD'
13
+ log = `git log 586af720daf323efeee358267752c9098c1fb407..HEAD --pretty=format:'%an|%B___'`
14
+
15
+ puts "mobb-redis #{Mobb::Redis::VERSION} (#{Date.today})"
16
+ puts '-' * 80
17
+ puts
18
+
19
+ log.split(/___/).each do |commit|
20
+ pieces = commit.split('|').reverse
21
+ author = pieces.pop.strip
22
+ message = pieces.join.strip
23
+
24
+ next if message =~ /^\s*Merge pull request/
25
+ next if message =~ /No changelog/
26
+ next if message =~ /^\s*Merge branch/
27
+
28
+ first_line = false
29
+
30
+ message.each_line do |line|
31
+ if !first_line
32
+ first_line = true
33
+ puts "* #{line}"
34
+ elsif line.strip.empty?
35
+ puts
36
+ else
37
+ puts " #{line}"
38
+ end
39
+ end
40
+
41
+ puts " #{author}"
42
+ puts
43
+ end
44
+ end
45
+
@@ -0,0 +1,9 @@
1
+ require 'redis-store'
2
+ require 'mobb-redis/version'
3
+ require 'mobb/cache/redis_store'
4
+
5
+ module Mobb
6
+ module Redis
7
+ # Your code goes here...
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module Mobb
2
+ module Redis
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
@@ -0,0 +1,131 @@
1
+ module Mobb
2
+ module Cache
3
+ class << self
4
+ def register(app)
5
+ app.set :cache, RedisStore.new
6
+ end
7
+ alias_method :registered, :register
8
+ end
9
+
10
+ class RedisStore
11
+ # Instantiate the store.
12
+ #
13
+ # Example:
14
+ # RedisStore.new
15
+ # # => host: localhost, port: 6379, db: 0
16
+ #
17
+ # RedisStore.new "example.com"
18
+ # # => host: example.com, port: 6379, db: 0
19
+ #
20
+ # RedisStore.new "example.com:23682"
21
+ # # => host: example.com, port: 23682, db: 0
22
+ #
23
+ # RedisStore.new "example.com:23682/1"
24
+ # # => host: example.com, port: 23682, db: 1
25
+ #
26
+ # RedisStore.new "example.com:23682/1/theplaylist"
27
+ # # => host: example.com, port: 23682, db: 1, namespace: theplaylist
28
+ #
29
+ # RedisStore.new "localhost:6379/0", "localhost:6380/0"
30
+ # # => instantiate a cluster
31
+ def initialize(*addresses)
32
+ @data = ::Redis::Store::Factory.create addresses
33
+ end
34
+
35
+ def write(key, value, options = nil)
36
+ if options && options[:unless_exist]
37
+ @data.setnx key, value, options
38
+ else
39
+ @data.set key, value, options
40
+ end
41
+ end
42
+
43
+ def read(key, options = nil)
44
+ @data.get(key, options)
45
+ end
46
+
47
+ def delete(key, options = nil)
48
+ @data.del key
49
+ end
50
+
51
+ def exist?(key, options = nil)
52
+ @data.exists key
53
+ end
54
+
55
+ # Increment a key in the store.
56
+ #
57
+ # If the key doesn't exist it will be initialized on 0.
58
+ # If the key exist but it isn't a Fixnum it will be initialized on 0.
59
+ #
60
+ # Example:
61
+ # We have two objects in cache:
62
+ # counter # => 23
63
+ # rabbit # => #<Rabbit:0x5eee6c>
64
+ #
65
+ # cache.increment "counter"
66
+ # cache.read "counter", :raw => true # => "24"
67
+ #
68
+ # cache.increment "counter", 6
69
+ # cache.read "counter", :raw => true # => "30"
70
+ #
71
+ # cache.increment "a counter"
72
+ # cache.read "a counter", :raw => true # => "1"
73
+ #
74
+ # cache.increment "rabbit"
75
+ # cache.read "rabbit", :raw => true # => "1"
76
+ def increment(key, amount = 1)
77
+ @data.incrby key, amount
78
+ end
79
+
80
+ # Decrement a key in the store
81
+ #
82
+ # If the key doesn't exist it will be initialized on 0.
83
+ # If the key exist but it isn't a Fixnum it will be initialized on 0.
84
+ #
85
+ # Example:
86
+ # We have two objects in cache:
87
+ # counter # => 23
88
+ # rabbit # => #<Rabbit:0x5eee6c>
89
+ #
90
+ # cache.decrement "counter"
91
+ # cache.read "counter", :raw => true # => "22"
92
+ #
93
+ # cache.decrement "counter", 2
94
+ # cache.read "counter", :raw => true # => "20"
95
+ #
96
+ # cache.decrement "a counter"
97
+ # cache.read "a counter", :raw => true # => "-1"
98
+ #
99
+ # cache.decrement "rabbit"
100
+ # cache.read "rabbit", :raw => true # => "-1"
101
+ def decrement(key, amount = 1)
102
+ @data.decrby key, amount
103
+ end
104
+
105
+ # Delete objects for matched keys.
106
+ #
107
+ # Example:
108
+ # cache.del_matched "rab*"
109
+ def delete_matched(matcher, options = nil)
110
+ @data.keys(matcher).each { |key| @data.del key }
111
+ end
112
+
113
+ def fetch(key, options = {})
114
+ (!options[:force] && data = read(key, options)) || block_given? && begin
115
+ data = yield
116
+ write(key, data, options)
117
+ end
118
+ data || nil
119
+ end
120
+
121
+ # Clear all the data from the store.
122
+ def clear
123
+ @data.flushdb
124
+ end
125
+
126
+ def stats
127
+ @data.info
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'mobb-redis/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'mobb-redis'
7
+ s.version = Mobb::Redis::VERSION
8
+ s.authors = ['kinoppyd', 'Luca Guidi']
9
+ s.email = ['whoisdissolvedgirl+github@gmail.com']
10
+ s.homepage = 'https://github.com/kinoppyd/mobb-redis'
11
+ s.summary = %q{Redis store for Mobb}
12
+ s.description = %q{Redis store for Mobb}
13
+ s.license = 'MIT'
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ['lib']
19
+
20
+ s.add_dependency 'redis-store', '>= 1.1', '< 2'
21
+ s.add_dependency 'mobb', '>= 0'
22
+
23
+ s.add_development_dependency 'rake', '~> 10'
24
+ s.add_development_dependency 'bundler', '~> 1.3'
25
+ s.add_development_dependency 'mocha', '~> 0.14.0'
26
+ s.add_development_dependency 'minitest', '~> 5'
27
+ s.add_development_dependency 'redis-store-testing'
28
+ end
@@ -0,0 +1,166 @@
1
+ require 'test_helper'
2
+
3
+ class App
4
+ def initialize
5
+ @values = {}
6
+ end
7
+
8
+ def set(key, value)
9
+ @values[key] = value
10
+ end
11
+
12
+ def get(key)
13
+ @values[key]
14
+ end
15
+ end
16
+
17
+ describe Mobb::Cache::RedisStore do
18
+ before do
19
+ @store = Mobb::Cache::RedisStore.new
20
+ @dstore = Mobb::Cache::RedisStore.new "redis://127.0.0.1:6380/1", "redis://127.0.0.1:6381/1"
21
+ @rabbit = OpenStruct.new :name => "bunny"
22
+ @white_rabbit = OpenStruct.new :color => "white"
23
+ with_store_management do |store|
24
+ store.write "rabbit", @rabbit
25
+ store.delete "counter"
26
+ store.delete "rub-a-dub"
27
+ end
28
+ end
29
+
30
+ it "should register as extension" do
31
+ app = App.new
32
+ Mobb::Cache.register(app)
33
+ store = app.get(:cache)
34
+ store.must_be_kind_of(Mobb::Cache::RedisStore)
35
+ end
36
+
37
+ it "should read the data" do
38
+ with_store_management do |store|
39
+ store.read("rabbit").must_equal(@rabbit)
40
+ end
41
+ end
42
+
43
+ it "should write the data" do
44
+ with_store_management do |store|
45
+ store.write "rabbit", @white_rabbit
46
+ store.read("rabbit").must_equal(@white_rabbit)
47
+ end
48
+ end
49
+
50
+ it "should write the data with expiration time" do
51
+ with_store_management do |store|
52
+ store.write "rabbit", @white_rabbit, :expires_in => 1 # second
53
+ # store.read("rabbit").must_equal(@white_rabbit)
54
+ sleep 2
55
+ store.read("rabbit").must_be_nil
56
+ end
57
+ end
58
+
59
+ it "should not write data if :unless_exist option is true" do
60
+ with_store_management do |store|
61
+ store.write "rabbit", @white_rabbit, :unless_exist => true
62
+ store.read("rabbit").must_equal(@rabbit)
63
+ end
64
+ end
65
+
66
+ it "should read raw data" do
67
+ with_store_management do |store|
68
+ store.read("rabbit", :raw => true).must_equal(Marshal.dump(@rabbit))
69
+ end
70
+ end
71
+
72
+ it "should write raw data" do
73
+ with_store_management do |store|
74
+ store.write "rabbit", @white_rabbit, :raw => true
75
+ store.read("rabbit", :raw => true).must_equal(%(#<OpenStruct color="white">))
76
+ end
77
+ end
78
+
79
+ it "should delete data" do
80
+ with_store_management do |store|
81
+ store.delete "rabbit"
82
+ store.read("rabbit").must_be_nil
83
+ end
84
+ end
85
+
86
+ it "should delete matched data" do
87
+ with_store_management do |store|
88
+ store.delete_matched "rabb*"
89
+ store.read("rabbit").must_be_nil
90
+ end
91
+ end
92
+
93
+ it "should verify existence of an object in the store" do
94
+ with_store_management do |store|
95
+ assert store.exist?("rabbit")
96
+ assert ! store.exist?("rab-a-dub")
97
+ end
98
+ end
99
+
100
+ it "should increment a key" do
101
+ with_store_management do |store|
102
+ 3.times { store.increment "counter" }
103
+ store.read("counter", :raw => true).to_i.must_equal(3)
104
+ end
105
+ end
106
+
107
+ it "should decrement a key" do
108
+ with_store_management do |store|
109
+ 3.times { store.increment "counter" }
110
+ 2.times { store.decrement "counter" }
111
+ store.read("counter", :raw => true).to_i.must_equal(1)
112
+ end
113
+ end
114
+
115
+ it "should increment a key by given value" do
116
+ with_store_management do |store|
117
+ store.increment "counter", 3
118
+ store.read("counter", :raw => true).to_i.must_equal(3)
119
+ end
120
+ end
121
+
122
+ it "should decrement a key by given value" do
123
+ with_store_management do |store|
124
+ 3.times { store.increment "counter" }
125
+ store.decrement "counter", 2
126
+ store.read("counter", :raw => true).to_i.must_equal(1)
127
+ end
128
+ end
129
+
130
+ it "should clear the store" do
131
+ with_store_management do |store|
132
+ store.clear
133
+ store.instance_variable_get(:@data).keys("*").flatten.must_be :empty?
134
+ end
135
+ end
136
+
137
+ it "should return store stats" do
138
+ with_store_management do |store|
139
+ store.stats.wont_be :empty?
140
+ end
141
+ end
142
+
143
+ it "should fetch data" do
144
+ with_store_management do |store|
145
+ store.fetch("rabbit").must_equal(@rabbit)
146
+ store.fetch("rub-a-dub").must_be_nil
147
+ store.fetch("rub-a-dub") { "Flora de Cana" }.must_equal("Flora de Cana")
148
+ store.fetch("rub-a-dub").must_equal("Flora de Cana")
149
+ store.fetch("rabbit", :force => true).must_be_nil # force cache miss
150
+ store.fetch("rabbit", :force => true, :expires_in => 1) { @white_rabbit }
151
+ # store.fetch("rabbit").must_equal(@white_rabbit)
152
+ sleep 2
153
+ store.fetch("rabbit").must_be_nil
154
+ end
155
+ end
156
+
157
+ private
158
+ def instantiate_store(addresses = nil)
159
+ Mobb::Cache::RedisStore.new(addresses).instance_variable_get(:@data)
160
+ end
161
+
162
+ def with_store_management
163
+ yield @store
164
+ yield @dstore
165
+ end
166
+ end
@@ -0,0 +1,4 @@
1
+ require 'bundler/setup'
2
+ require 'minitest/autorun'
3
+ require 'mocha/setup'
4
+ require 'mobb-redis'
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mobb-redis
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - kinoppyd
8
+ - Luca Guidi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2018-12-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redis-store
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '1.1'
21
+ - - "<"
22
+ - !ruby/object:Gem::Version
23
+ version: '2'
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ version: '1.1'
31
+ - - "<"
32
+ - !ruby/object:Gem::Version
33
+ version: '2'
34
+ - !ruby/object:Gem::Dependency
35
+ name: mobb
36
+ requirement: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ type: :runtime
42
+ prerelease: false
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ - !ruby/object:Gem::Dependency
49
+ name: rake
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ type: :development
70
+ prerelease: false
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ - !ruby/object:Gem::Dependency
77
+ name: mocha
78
+ requirement: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.14.0
83
+ type: :development
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.14.0
90
+ - !ruby/object:Gem::Dependency
91
+ name: minitest
92
+ requirement: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5'
97
+ type: :development
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '5'
104
+ - !ruby/object:Gem::Dependency
105
+ name: redis-store-testing
106
+ requirement: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ type: :development
112
+ prerelease: false
113
+ version_requirements: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ description: Redis store for Mobb
119
+ email:
120
+ - whoisdissolvedgirl+github@gmail.com
121
+ executables: []
122
+ extensions: []
123
+ extra_rdoc_files: []
124
+ files:
125
+ - ".gitignore"
126
+ - ".travis.yml"
127
+ - CHANGELOG.md
128
+ - Gemfile
129
+ - MIT-LICENSE
130
+ - README.md
131
+ - Rakefile
132
+ - lib/mobb-redis.rb
133
+ - lib/mobb-redis/version.rb
134
+ - lib/mobb/cache/redis_store.rb
135
+ - mobb-redis.gemspec
136
+ - test/sinatra/cache/redis_store_test.rb
137
+ - test/test_helper.rb
138
+ homepage: https://github.com/kinoppyd/mobb-redis
139
+ licenses:
140
+ - MIT
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 2.7.6
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: Redis store for Mobb
162
+ test_files:
163
+ - test/sinatra/cache/redis_store_test.rb
164
+ - test/test_helper.rb