mongodis 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/mongodis/backer.rb +21 -0
- data/lib/mongodis/base.rb +63 -0
- data/lib/mongodis/reader.rb +42 -0
- data/lib/mongodis/version.rb +11 -0
- data/mongodis.gemspec +25 -0
- data/spec/lib/backer_spec.rb +0 -0
- data/spec/lib/base_spec.rb +19 -0
- data/spec/lib/reader_spec.rb +54 -0
- data/spec/spec_helper.rb +44 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 490a57e7cf8860192618fc4d57050e00d55a9194
|
4
|
+
data.tar.gz: ece6bc0e12b81d4652dcaaa37d479fd7cbcd781e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 804e08c743202048ca108221ad7fcf896c189e018521085c3026230953d708de4f413418bc9c38217db2cd34e6672c6984c9e69271dacc18d3134cd4a43b4efa
|
7
|
+
data.tar.gz: ed618482cbff50cb5d0c4148b0d541f2716f350c9091ab8cd3fefb257ebf5aa6522bb0894d70af0d2bbb7803a2f4c10f3a4629f51870079ccc0317de2f99391e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Tom Caspy
|
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
|
+
# Mongodis
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'mongodis'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install mongodis
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( http://github.com/<my-github-username>/mongodis/fork )
|
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,21 @@
|
|
1
|
+
module Mongodis
|
2
|
+
class Backer
|
3
|
+
def initialize(mongoid_object, options)
|
4
|
+
@options = options
|
5
|
+
@mongoid_object = mongoid_object
|
6
|
+
end
|
7
|
+
|
8
|
+
def write
|
9
|
+
if @options[:persistance_types].include?(:key)
|
10
|
+
key = @mongoid_object.redis_backer_key
|
11
|
+
Mongodis.redis.setex(key, @options[:ttl], @mongoid_object.to_json)
|
12
|
+
end
|
13
|
+
|
14
|
+
if @options[:persistance_types].include?(:list)
|
15
|
+
key = @mongoid_object.redis_backer_list_key
|
16
|
+
Mongodis.redis.zadd(key, @mongoid_object.created_at.to_i, @mongoid_object.to_json)
|
17
|
+
#Mongodis.redis.zremrangebyrank(key, 0, @mongoid_object.created_at.to_i - @options[:ttl])
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require "mongodis/backer"
|
2
|
+
require "mongodis/reader"
|
3
|
+
|
4
|
+
module Mongodis
|
5
|
+
module Base
|
6
|
+
def self.included(base)
|
7
|
+
base.extend(ClassMethods)
|
8
|
+
class << base
|
9
|
+
attr_accessor :redis_backer_options
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def put_in_redis
|
14
|
+
Mongodis::Backer.new(self, self.class.redis_backer_options).write
|
15
|
+
end
|
16
|
+
|
17
|
+
def redis_backer_key
|
18
|
+
@redis_backer_key ||= begin
|
19
|
+
keys = self.class.redis_backer_options[:keys].map do |key|
|
20
|
+
if key.is_a?(Proc)
|
21
|
+
key.call(self).to_s
|
22
|
+
else
|
23
|
+
send(key).to_s
|
24
|
+
end
|
25
|
+
end
|
26
|
+
Mongodis.key(keys, self.class)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def redis_backer_list_key
|
31
|
+
@redis_backer_list_key ||= begin
|
32
|
+
keys = self.class.redis_backer_options[:list_keys].map do |key|
|
33
|
+
if key.is_a?(Proc)
|
34
|
+
key.call(self).to_s
|
35
|
+
else
|
36
|
+
send(key).to_s
|
37
|
+
end
|
38
|
+
end
|
39
|
+
keys << "list"
|
40
|
+
Mongodis.key(keys, self.class)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
module ClassMethods
|
45
|
+
def redis_backed(options = {})
|
46
|
+
defaults = {
|
47
|
+
keys: [:id],
|
48
|
+
list_keys: [],
|
49
|
+
persistance_types: [:key],
|
50
|
+
ttl: 30,
|
51
|
+
class_name: self.to_s
|
52
|
+
}
|
53
|
+
|
54
|
+
self.redis_backer_options = defaults.merge(options)
|
55
|
+
after_create(:put_in_redis)
|
56
|
+
end
|
57
|
+
|
58
|
+
def redis_getter(params={})
|
59
|
+
Mongodis::Reader.new(params, redis_backer_options)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require "json"
|
2
|
+
module Mongodis
|
3
|
+
class Reader
|
4
|
+
def initialize(params, options)
|
5
|
+
@options = options
|
6
|
+
@params = params
|
7
|
+
end
|
8
|
+
|
9
|
+
def get
|
10
|
+
raise "No simple get allowed" unless @options[:persistance_types].include?(:key)
|
11
|
+
|
12
|
+
JSON.parse(Mongodis.redis.get(redis_backer_key))
|
13
|
+
end
|
14
|
+
|
15
|
+
def get_all
|
16
|
+
Mongodis.redis.zrangebyscore(redis_backer_list_key, "-inf", "inf").map do |i|
|
17
|
+
JSON.parse i
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def redis_backer_key
|
24
|
+
@redis_backer_key ||= begin
|
25
|
+
keys = @options[:keys].map do |key|
|
26
|
+
@params[key]
|
27
|
+
end
|
28
|
+
Mongodis.key(keys, @options[:class_name])
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def redis_backer_list_key
|
33
|
+
@redis_backer_list_key ||= begin
|
34
|
+
keys = @options[:list_keys].map do |key|
|
35
|
+
@params[key]
|
36
|
+
end
|
37
|
+
keys.push("list")
|
38
|
+
Mongodis.key(keys, @options[:class_name])
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/mongodis.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mongodis/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mongodis"
|
8
|
+
spec.version = Mongodis::VERSION
|
9
|
+
spec.authors = ["Tom Caspy"]
|
10
|
+
spec.email = ["tcaspy@gmail.com"]
|
11
|
+
spec.summary = %q{Write to both redis and mongo, for atomicity and percistency.}
|
12
|
+
spec.description = %q{}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "redis"
|
25
|
+
end
|
File without changes
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "all your base" do
|
4
|
+
it "saves the corect options on the class" do
|
5
|
+
options = Dummy.redis_backer_options
|
6
|
+
options.should_not be_nil
|
7
|
+
options[:ttl].should == 30
|
8
|
+
end
|
9
|
+
|
10
|
+
it "calls the after_create method" do
|
11
|
+
Dummy.should_receive(:after_create).with(:put_in_redis)
|
12
|
+
Dummy.redis_backed
|
13
|
+
end
|
14
|
+
|
15
|
+
it "saves the item into redis" do
|
16
|
+
Mongodis.redis.should_receive :setex
|
17
|
+
Dummy.new.put_in_redis
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "redis"
|
3
|
+
|
4
|
+
Mongodis.redis = Redis.new
|
5
|
+
|
6
|
+
describe Mongodis::Reader do
|
7
|
+
before do
|
8
|
+
Mongodis.redis.flushdb
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "simple storage" do
|
12
|
+
let(:dummy){Dummy.new}
|
13
|
+
before do
|
14
|
+
dummy.put_in_redis
|
15
|
+
@reader = Dummy.redis_getter(id: "id")
|
16
|
+
end
|
17
|
+
|
18
|
+
it "reads the item" do
|
19
|
+
@reader.get.should == {}
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should read the item when to_json has info" do
|
23
|
+
dummy.stub(:to_json).and_return('{"foo": "bar"}')
|
24
|
+
dummy.put_in_redis
|
25
|
+
@reader.get.should == {"foo" => "bar"}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "lists" do
|
30
|
+
before do
|
31
|
+
@dummies = (1..4).map do |i|
|
32
|
+
dummy = ListDummy.new
|
33
|
+
dummy.stub(:id).and_return("id#{i}")
|
34
|
+
dummy.put_in_redis
|
35
|
+
dummy
|
36
|
+
end
|
37
|
+
@reader = ListDummy.redis_getter(name: "name")
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should get number of objects" do
|
41
|
+
@reader.get_all.map{|i| i["id"]}.should == ["id1", "id2", "id3", "id4"]
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should get only the objects it needs" do
|
45
|
+
another_dummy = ListDummy.new
|
46
|
+
another_dummy.stub(:id).and_return("id6")
|
47
|
+
another_dummy.stub(:name).and_return("name2")
|
48
|
+
another_dummy.put_in_redis
|
49
|
+
@reader.get_all.map{|i| i["id"]}.should == ["id1", "id2", "id3", "id4"]
|
50
|
+
ListDummy.redis_getter(name: "name2").get_all.first["id"].should == "id6"
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require "rspec"
|
2
|
+
|
3
|
+
require 'mongodis/base'
|
4
|
+
class Dummy
|
5
|
+
include Mongodis::Base
|
6
|
+
def self.after_create(foo); end
|
7
|
+
|
8
|
+
def id
|
9
|
+
"id"
|
10
|
+
end
|
11
|
+
|
12
|
+
def created_at
|
13
|
+
Time.now
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_json
|
17
|
+
"{}"
|
18
|
+
end
|
19
|
+
|
20
|
+
redis_backed
|
21
|
+
end
|
22
|
+
|
23
|
+
class ListDummy
|
24
|
+
include Mongodis::Base
|
25
|
+
def self.after_create(foo); end
|
26
|
+
|
27
|
+
def id
|
28
|
+
"id"
|
29
|
+
end
|
30
|
+
|
31
|
+
def name
|
32
|
+
"name"
|
33
|
+
end
|
34
|
+
|
35
|
+
def created_at
|
36
|
+
Time.now
|
37
|
+
end
|
38
|
+
|
39
|
+
def to_json
|
40
|
+
%{{"name": "#{name}", "id": "#{id}"}}
|
41
|
+
end
|
42
|
+
|
43
|
+
redis_backed persistance_types: [:key, :list], keys: [:id, :name], list_keys: [:name]
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongodis
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tom Caspy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: redis
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: ''
|
70
|
+
email:
|
71
|
+
- tcaspy@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/mongodis/backer.rb
|
82
|
+
- lib/mongodis/base.rb
|
83
|
+
- lib/mongodis/reader.rb
|
84
|
+
- lib/mongodis/version.rb
|
85
|
+
- mongodis.gemspec
|
86
|
+
- spec/lib/backer_spec.rb
|
87
|
+
- spec/lib/base_spec.rb
|
88
|
+
- spec/lib/reader_spec.rb
|
89
|
+
- spec/spec_helper.rb
|
90
|
+
homepage: ''
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.2.2
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Write to both redis and mongo, for atomicity and percistency.
|
114
|
+
test_files:
|
115
|
+
- spec/lib/backer_spec.rb
|
116
|
+
- spec/lib/base_spec.rb
|
117
|
+
- spec/lib/reader_spec.rb
|
118
|
+
- spec/spec_helper.rb
|