mongo_store 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/mongo_store.rb +77 -0
- data/spec/mongo_store_spec.rb +120 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +10 -0
- metadata +126 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Stephen Eley
|
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.
|
data/README.rdoc
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= MongoStore
|
2
|
+
|
3
|
+
It's **ActiveSupport::Cache::MongoStore** -- if you know your Rails caching, _'nuff said._
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 Stephen Eley. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "mongo_store"
|
8
|
+
gem.summary = %Q{ActiveSupport::Cache implementation for MongoDB}
|
9
|
+
gem.description = %Q{10gen keeps bragging that MongoDB, with its fast writes and memory-mapped architecture, is ideal for caching. This gem puts it to use in Rails.}
|
10
|
+
gem.email = "sfeley@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/SFEley/mongo_store"
|
12
|
+
gem.authors = ["Stephen Eley"]
|
13
|
+
gem.add_dependency "mongo", ">= 0.18.3"
|
14
|
+
gem.add_dependency "activesupport", ">= 2.2"
|
15
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
16
|
+
gem.add_development_dependency "mocha", ">= 0.9"
|
17
|
+
|
18
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
19
|
+
end
|
20
|
+
Jeweler::GemcutterTasks.new
|
21
|
+
rescue LoadError
|
22
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
23
|
+
end
|
24
|
+
|
25
|
+
require 'spec/rake/spectask'
|
26
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
27
|
+
spec.libs << 'lib' << 'spec'
|
28
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
29
|
+
end
|
30
|
+
|
31
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
32
|
+
spec.libs << 'lib' << 'spec'
|
33
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
34
|
+
spec.rcov = true
|
35
|
+
end
|
36
|
+
|
37
|
+
task :spec => :check_dependencies
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rake/rdoctask'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "mongo_store #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/mongo_store.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'mongo'
|
3
|
+
|
4
|
+
module ActiveSupport
|
5
|
+
module Cache
|
6
|
+
class MongoStore < Store
|
7
|
+
attr_reader :collection
|
8
|
+
|
9
|
+
# Returns a MongoDB cache store. Takes several possible combinations of parameters. In order of
|
10
|
+
# escalating guesswork:
|
11
|
+
#
|
12
|
+
# 1. *a Mongo::Collection object* - No guessing. The collection is used as the cache store.
|
13
|
+
# 2. *a collection name and a database name* - Mongo objects are created for both. The default 'localhost:27017' connection is used.
|
14
|
+
# 3. *a collection name* - Uses either MongoMapper.database (if MongoMapper is defined in the app) or a DB with the same name.
|
15
|
+
# 4. *no parameters* - A collection named 'rails_cache' is created, using either MongoMapper.database (if MongoMapper is defined in the app) or a DB also named 'rails_cache'.
|
16
|
+
#
|
17
|
+
# Unless option 1 is used, indexes are created on the key and expiration fields. If you supply your own collection,
|
18
|
+
# you are also responsible for making your own indexes.
|
19
|
+
def initialize(collection = nil, db_name = nil)
|
20
|
+
@collection = case collection
|
21
|
+
when Mongo::Collection then collection
|
22
|
+
when String
|
23
|
+
make_collection(collection, db_name)
|
24
|
+
when nil
|
25
|
+
make_collection('rails_cache')
|
26
|
+
else
|
27
|
+
raise TypeError, "MongoStore parameters must be nil, a Mongo::Collection, or a collection name."
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Inserts the value into the cache collection or updates the existing value. The value must be a valid
|
32
|
+
# MongoDB type. An *:expires_in* option may be provided, as with MemCacheStore. If one is _not_
|
33
|
+
# provided, a default expiration of 1 year is used.
|
34
|
+
def write(key, value, options={})
|
35
|
+
super
|
36
|
+
expires = Time.now + (options[:expires_in] || 1.year)
|
37
|
+
collection.update({'key' => key}, {'$set' => {'value' => value, 'expires' => expires}}, :upsert => true)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Reads the value from the cache collection.
|
41
|
+
def read(key, options={})
|
42
|
+
super
|
43
|
+
if doc = collection.find_one('key' => key, 'expires' => {'$gt' => Time.now})
|
44
|
+
doc['value']
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Takes the specified value out of the collection.
|
49
|
+
def delete(key, options={})
|
50
|
+
super
|
51
|
+
collection.remove({'key' => key})
|
52
|
+
end
|
53
|
+
|
54
|
+
# With MongoDB, there's no difference between querying on an exact value or a regex. Beautiful, huh?
|
55
|
+
alias_method :delete_matched, :delete
|
56
|
+
|
57
|
+
private
|
58
|
+
def mongomapper?
|
59
|
+
Kernel.const_defined?(:MongoMapper) && MongoMapper.respond_to?(:database) && MongoMapper.database
|
60
|
+
end
|
61
|
+
|
62
|
+
def make_collection(collection, db_name=nil)
|
63
|
+
if db_name
|
64
|
+
db = Mongo::DB.new(db_name, Mongo::Connection.new)
|
65
|
+
elsif mongomapper?
|
66
|
+
db = MongoMapper.database
|
67
|
+
else
|
68
|
+
db = Mongo::DB.new(collection, Mongo::Connection.new)
|
69
|
+
end
|
70
|
+
coll = db.create_collection(collection)
|
71
|
+
coll.create_index('key' => Mongo::ASCENDING, 'expires' => Mongo::DESCENDING)
|
72
|
+
coll
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
# Stubbing MongoMapper out so that we don't have to have it installed for testing
|
4
|
+
class MongoMapper
|
5
|
+
end
|
6
|
+
|
7
|
+
describe "MongoStore" do
|
8
|
+
describe "collection" do
|
9
|
+
it "can be specified as a Mongo::Collection object" do
|
10
|
+
db = Mongo::DB.new('mongo_store_test', Mongo::Connection.new)
|
11
|
+
coll = Mongo::Collection.new(db, 'foostore')
|
12
|
+
store = ActiveSupport::Cache.lookup_store(:mongo_store, coll)
|
13
|
+
store.collection.should == coll
|
14
|
+
end
|
15
|
+
|
16
|
+
it "can take a collection name and a database name" do
|
17
|
+
store = ActiveSupport::Cache.lookup_store(:mongo_store, 'foo', 'bar')
|
18
|
+
store.collection.name.should == 'foo'
|
19
|
+
store.collection.db.name.should == 'bar'
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "with MongoMapper" do
|
23
|
+
before(:each) do
|
24
|
+
db = Mongo::DB.new('mappy', Mongo::Connection.new)
|
25
|
+
MongoMapper.expects(:database).twice.returns(db)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "can take a collection name" do
|
29
|
+
store = ActiveSupport::Cache.lookup_store(:mongo_store, 'happy')
|
30
|
+
store.collection.name.should == 'happy'
|
31
|
+
store.collection.db.name.should == 'mappy'
|
32
|
+
end
|
33
|
+
|
34
|
+
it "defaults to a 'rails_cache' collection" do
|
35
|
+
store = ActiveSupport::Cache.lookup_store(:mongo_store)
|
36
|
+
store.collection.name.should == 'rails_cache'
|
37
|
+
store.collection.db.name.should == 'mappy'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "without MongoMapper" do
|
42
|
+
it "can take a collection name" do
|
43
|
+
store = ActiveSupport::Cache.lookup_store(:mongo_store, 'yuna')
|
44
|
+
store.collection.name.should == 'yuna'
|
45
|
+
store.collection.db.name.should == 'yuna'
|
46
|
+
end
|
47
|
+
|
48
|
+
it "defaults to a 'rails_cache' collection" do
|
49
|
+
store = ActiveSupport::Cache.lookup_store(:mongo_store)
|
50
|
+
store.collection.name.should == 'rails_cache'
|
51
|
+
store.collection.db.name.should == 'rails_cache'
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
it "raises an exception if an unusable parameter is passed" do
|
56
|
+
lambda{ActiveSupport::Cache.lookup_store(:mongo_store, 5)}.should raise_error(TypeError)
|
57
|
+
end
|
58
|
+
|
59
|
+
after(:all) do
|
60
|
+
c = Mongo::Connection.new
|
61
|
+
%w(bar mappy rails_cache yuna).each do |db|
|
62
|
+
c.drop_database(db)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "caching" do
|
68
|
+
before(:all) do
|
69
|
+
@store = ActiveSupport::Cache.lookup_store(:mongo_store, 'mongo_store_test')
|
70
|
+
end
|
71
|
+
|
72
|
+
it "can write values" do
|
73
|
+
@store.write('fnord', 'I am vaguely disturbed.')
|
74
|
+
@store.collection.find_one(:key => 'fnord')['value'].should == "I am vaguely disturbed."
|
75
|
+
end
|
76
|
+
|
77
|
+
it "can read values" do
|
78
|
+
@store.collection.insert({:key => 'yoo', :value => 'yar', :expires => 1.year.from_now})
|
79
|
+
@store.read('yoo').should == 'yar'
|
80
|
+
end
|
81
|
+
|
82
|
+
it "can delete keys" do
|
83
|
+
@store.write('foo', 'bar')
|
84
|
+
@store.read('foo').should == 'bar'
|
85
|
+
@store.delete('foo')
|
86
|
+
@store.read('foo').should be_nil
|
87
|
+
end
|
88
|
+
|
89
|
+
it "can delete keys matching a regular expression" do
|
90
|
+
@store.write('foo', 'bar')
|
91
|
+
@store.write('fodder', 'bother')
|
92
|
+
@store.write('yoo', 'yar')
|
93
|
+
# Initial state
|
94
|
+
@store.read('foo').should == 'bar'
|
95
|
+
@store.read('fodder').should == 'bother'
|
96
|
+
@store.read('yoo').should == 'yar'
|
97
|
+
# The work
|
98
|
+
@store.delete_matched /oo/
|
99
|
+
# Post state
|
100
|
+
@store.read('foo').should be_nil
|
101
|
+
@store.read('fodder').should == 'bother'
|
102
|
+
@store.read('yoo').should be_nil
|
103
|
+
end
|
104
|
+
|
105
|
+
it "can expire a value with the :expires_in option" do
|
106
|
+
@store.write('ray', 'dar', :expires_in => 2.seconds)
|
107
|
+
@store.read('ray').should == 'dar'
|
108
|
+
sleep(3)
|
109
|
+
@store.read('ray').should be_nil
|
110
|
+
end
|
111
|
+
|
112
|
+
after(:each) do
|
113
|
+
@store.collection.remove # Clear our records
|
114
|
+
end
|
115
|
+
after(:all) do
|
116
|
+
c = Mongo::Connection.new
|
117
|
+
c.drop_database('mongo_store_test')
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'mongo_store'
|
4
|
+
require 'spec'
|
5
|
+
require 'spec/autorun'
|
6
|
+
require 'mocha'
|
7
|
+
|
8
|
+
Spec::Runner.configure do |config|
|
9
|
+
config.mock_with :mocha
|
10
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongo_store
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Stephen Eley
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-03-06 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: mongo
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
- 18
|
30
|
+
- 3
|
31
|
+
version: 0.18.3
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: activesupport
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 2
|
43
|
+
- 2
|
44
|
+
version: "2.2"
|
45
|
+
type: :runtime
|
46
|
+
version_requirements: *id002
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
prerelease: false
|
50
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
segments:
|
55
|
+
- 1
|
56
|
+
- 2
|
57
|
+
- 9
|
58
|
+
version: 1.2.9
|
59
|
+
type: :development
|
60
|
+
version_requirements: *id003
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: mocha
|
63
|
+
prerelease: false
|
64
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
- 9
|
71
|
+
version: "0.9"
|
72
|
+
type: :development
|
73
|
+
version_requirements: *id004
|
74
|
+
description: 10gen keeps bragging that MongoDB, with its fast writes and memory-mapped architecture, is ideal for caching. This gem puts it to use in Rails.
|
75
|
+
email: sfeley@gmail.com
|
76
|
+
executables: []
|
77
|
+
|
78
|
+
extensions: []
|
79
|
+
|
80
|
+
extra_rdoc_files:
|
81
|
+
- LICENSE
|
82
|
+
- README.rdoc
|
83
|
+
files:
|
84
|
+
- .document
|
85
|
+
- .gitignore
|
86
|
+
- LICENSE
|
87
|
+
- README.rdoc
|
88
|
+
- Rakefile
|
89
|
+
- VERSION
|
90
|
+
- lib/mongo_store.rb
|
91
|
+
- spec/mongo_store_spec.rb
|
92
|
+
- spec/spec.opts
|
93
|
+
- spec/spec_helper.rb
|
94
|
+
has_rdoc: true
|
95
|
+
homepage: http://github.com/SFEley/mongo_store
|
96
|
+
licenses: []
|
97
|
+
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options:
|
100
|
+
- --charset=UTF-8
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
segments:
|
108
|
+
- 0
|
109
|
+
version: "0"
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
segments:
|
115
|
+
- 0
|
116
|
+
version: "0"
|
117
|
+
requirements: []
|
118
|
+
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 1.3.6
|
121
|
+
signing_key:
|
122
|
+
specification_version: 3
|
123
|
+
summary: ActiveSupport::Cache implementation for MongoDB
|
124
|
+
test_files:
|
125
|
+
- spec/mongo_store_spec.rb
|
126
|
+
- spec/spec_helper.rb
|