mongo_rails_cache 0.1.0
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.
- data/.document +5 -0
- data/.gitignore +24 -0
- data/.rspec +2 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +32 -0
- data/VERSION +1 -0
- data/lib/mongo_store/cache.rb +48 -0
- data/lib/mongo_store.rb +1 -0
- data/mongo_rails_cache.gemspec +58 -0
- data/spec/cache/cache_spec.rb +121 -0
- data/spec/mongo_store_spec.rb +8 -0
- data/spec/spec_helper.rb +15 -0
- metadata +93 -0
data/.document
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Ashley Streb
|
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
|
+
= mongo_store
|
2
|
+
|
3
|
+
Description goes here.
|
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 Ashley Streb. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rspec/core'
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
require 'rspec'
|
6
|
+
|
7
|
+
|
8
|
+
begin
|
9
|
+
require 'jeweler'
|
10
|
+
Jeweler::Tasks.new do |gem|
|
11
|
+
gem.name = "mongo_rails_cache"
|
12
|
+
gem.summary = %Q{Rails3 ActiveSupport Cache built on MongoDB}
|
13
|
+
gem.description = %Q{Rails3 ActiveSupport Cache built on MongoDB}
|
14
|
+
gem.email = "astreb@gmail.com"
|
15
|
+
gem.homepage = "http://github.com/roark31337/mongo_rails_cache"
|
16
|
+
gem.authors = ["Ashley Streb"]
|
17
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
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
|
+
|
26
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
27
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
28
|
+
end
|
29
|
+
|
30
|
+
#task :spec => :check_dependencies
|
31
|
+
|
32
|
+
task :default => :spec
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,48 @@
|
|
1
|
+
begin
|
2
|
+
require 'mongo'
|
3
|
+
rescue LoadError => e
|
4
|
+
$stderr.puts "You don't have mongo installed in your application. Please add it to your Gemfile and run bundle install"
|
5
|
+
raise e
|
6
|
+
end
|
7
|
+
|
8
|
+
require 'active_support/all'
|
9
|
+
|
10
|
+
module MongoStore
|
11
|
+
class Cache < ActiveSupport::Cache::Store
|
12
|
+
attr_reader :collection, :options
|
13
|
+
|
14
|
+
def initialize(collection, options={})
|
15
|
+
@collection, @options = collection, options || {}
|
16
|
+
end
|
17
|
+
|
18
|
+
def expires_in
|
19
|
+
@expires_in ||= options[:expires_in] || 1.month
|
20
|
+
end
|
21
|
+
|
22
|
+
def write_entry(key, entry, options)
|
23
|
+
doc = {:_id => key.to_s, :value => BSON::Binary.new(Marshal.dump(entry))}
|
24
|
+
@collection.save(doc)
|
25
|
+
true
|
26
|
+
end
|
27
|
+
|
28
|
+
def read_entry(key, options)
|
29
|
+
entry = @collection.find_one(:_id => key.to_s)
|
30
|
+
if entry
|
31
|
+
Marshal.load(entry['value'].to_s)
|
32
|
+
else
|
33
|
+
nil
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# we don't get any info from mongo driver if delete actually deleted anything. this will always return true.
|
38
|
+
def delete_entry(key, options)
|
39
|
+
@collection.remove({:_id => key.to_s})
|
40
|
+
true
|
41
|
+
end
|
42
|
+
|
43
|
+
def clear(options=nil)
|
44
|
+
@collection.remove
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
data/lib/mongo_store.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'mongo_store/cache'
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{mongo_rails_cache}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Ashley Streb"]
|
12
|
+
s.date = %q{2010-09-28}
|
13
|
+
s.description = %q{Rails3 ActiveSupport Cache built on MongoDB}
|
14
|
+
s.email = %q{astreb@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
".rspec",
|
23
|
+
"LICENSE",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"lib/mongo_store.rb",
|
28
|
+
"lib/mongo_store/cache.rb",
|
29
|
+
"mongo_rails_cache.gemspec",
|
30
|
+
"spec/cache/cache_spec.rb",
|
31
|
+
"spec/mongo_store_spec.rb",
|
32
|
+
"spec/spec_helper.rb"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://github.com/roark31337/mongo_rails_cache}
|
35
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = %q{1.3.7}
|
38
|
+
s.summary = %q{Rails3 ActiveSupport Cache built on MongoDB}
|
39
|
+
s.test_files = [
|
40
|
+
"spec/cache/cache_spec.rb",
|
41
|
+
"spec/mongo_store_spec.rb",
|
42
|
+
"spec/spec_helper.rb"
|
43
|
+
]
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
|
+
s.specification_version = 3
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
50
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
53
|
+
end
|
54
|
+
else
|
55
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe MongoStore::Cache do
|
4
|
+
before(:each) do
|
5
|
+
DB.collections.each do |collection|
|
6
|
+
collection.remove
|
7
|
+
collection.drop_indexes
|
8
|
+
end
|
9
|
+
|
10
|
+
@collection = DB['mongo_store_cache']
|
11
|
+
@store = MongoStore::Cache.new(@collection)
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:collection) { DB['mongo_store_cache'] }
|
15
|
+
let(:store) { MongoStore::Cache.new(collection, nil) }
|
16
|
+
|
17
|
+
it "has a collection" do
|
18
|
+
store.collection.should == collection
|
19
|
+
end
|
20
|
+
|
21
|
+
it "defaults expires_in to 1.month" do
|
22
|
+
store.expires_in.should == 1.month
|
23
|
+
end
|
24
|
+
|
25
|
+
it "can set default expires_in" do
|
26
|
+
MongoStore::Cache.new(collection, :expires_in => 5.minutes).expires_in.should == 5.minutes
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#write" do
|
30
|
+
before(:each) do
|
31
|
+
store.write('foo', 'bar')
|
32
|
+
end
|
33
|
+
|
34
|
+
let(:document) { collection.find_one(:_id => 'foo') }
|
35
|
+
|
36
|
+
it "sets _id to key" do
|
37
|
+
document['_id'].should == 'foo'
|
38
|
+
end
|
39
|
+
|
40
|
+
it "sets value key to value" do
|
41
|
+
store.read('foo').should == 'bar'
|
42
|
+
end
|
43
|
+
|
44
|
+
it "always sets keys as strings" do
|
45
|
+
store.write(:foo, 'bar')
|
46
|
+
doc = collection.find_one(:_id => 'foo')
|
47
|
+
doc.should_not be_nil
|
48
|
+
doc['_id'].should be_instance_of(String)
|
49
|
+
|
50
|
+
store.write('foo', 'bar')
|
51
|
+
doc = collection.find_one(:_id => 'foo')
|
52
|
+
doc.should_not be_nil
|
53
|
+
doc['_id'].should be_instance_of(String)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should append a namespace to the keyname if one is provided" do
|
57
|
+
store.write('first', 'bar', {:expires_in => 1.year, :namespace => ns = 'myns'})
|
58
|
+
collection.find_one(:_id => "first").should be_nil
|
59
|
+
collection.find_one(:_id => "#{ns}:first").should_not be_nil
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should not append a namespace to the keyname if one is not provided" do
|
63
|
+
store.write('second', 'bar', {:expires_in => 1.year})
|
64
|
+
collection.find_one(:_id => "second").should_not be_nil
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe '#read' do
|
69
|
+
before(:each) do
|
70
|
+
store.write('foo', 'bar')
|
71
|
+
end
|
72
|
+
|
73
|
+
let(:document) { collection.find_one(:_id => 'foo') }
|
74
|
+
|
75
|
+
it "returns nil for key not found" do
|
76
|
+
store.read('non:existent:key').should be_nil
|
77
|
+
end
|
78
|
+
it "returns unmarshalled value key value for key found" do
|
79
|
+
store.read('foo').should == 'bar'
|
80
|
+
end
|
81
|
+
it "returns nil for existing but expired key" do
|
82
|
+
store.write('baz', 'bar', {:expires_in => 0.hours})
|
83
|
+
store.read('baz').should be_nil
|
84
|
+
end
|
85
|
+
it "should return an entry with an expiry date in the future" do
|
86
|
+
store.write('foo', 'bar', {:expires_in => 1.year})
|
87
|
+
store.read('foo').should == 'bar'
|
88
|
+
end
|
89
|
+
it "should work with symbols" do
|
90
|
+
store.read(:foo).should == 'bar'
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe '#delete' do
|
95
|
+
before(:each) do
|
96
|
+
store.write('foo', 'bar')
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should delete key from cache" do
|
100
|
+
store.read('foo').should_not be_nil
|
101
|
+
store.delete('foo')
|
102
|
+
store.read('foo').should be_nil
|
103
|
+
end
|
104
|
+
|
105
|
+
it "works with symbol" do
|
106
|
+
store.read(:foo).should_not be_nil
|
107
|
+
store.delete(:foo)
|
108
|
+
store.read(:foo).should be_nil
|
109
|
+
end
|
110
|
+
|
111
|
+
# mongo doesn't tell us if it deleted an item, true will always be returned
|
112
|
+
it "should always return true if an entry was deleted regardless of whether it is in the cache" do
|
113
|
+
store.read(:foo).should_not be_nil
|
114
|
+
store.delete(:foo).should be_true
|
115
|
+
|
116
|
+
store.read(bad_key = "nonexistend:key:name").should be_nil
|
117
|
+
store.delete(bad_key).should be_true
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
gem 'activesupport'
|
2
|
+
$:.unshift(File.expand_path('../../lib', __FILE__))
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'mongo_store'
|
6
|
+
require 'mongo'
|
7
|
+
|
8
|
+
require 'rspec'
|
9
|
+
|
10
|
+
Rspec.configure do |config|
|
11
|
+
config.mock_with :rspec
|
12
|
+
end
|
13
|
+
|
14
|
+
connection = Mongo::Connection.new
|
15
|
+
DB = connection.db('mongo-store-test')
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongo_rails_cache
|
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
|
+
- Ashley Streb
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-09-28 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 1
|
30
|
+
- 2
|
31
|
+
- 9
|
32
|
+
version: 1.2.9
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
description: Rails3 ActiveSupport Cache built on MongoDB
|
36
|
+
email: astreb@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- .rspec
|
48
|
+
- LICENSE
|
49
|
+
- README.rdoc
|
50
|
+
- Rakefile
|
51
|
+
- VERSION
|
52
|
+
- lib/mongo_store.rb
|
53
|
+
- lib/mongo_store/cache.rb
|
54
|
+
- mongo_rails_cache.gemspec
|
55
|
+
- spec/cache/cache_spec.rb
|
56
|
+
- spec/mongo_store_spec.rb
|
57
|
+
- spec/spec_helper.rb
|
58
|
+
has_rdoc: true
|
59
|
+
homepage: http://github.com/roark31337/mongo_rails_cache
|
60
|
+
licenses: []
|
61
|
+
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options:
|
64
|
+
- --charset=UTF-8
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
requirements: []
|
84
|
+
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.3.7
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: Rails3 ActiveSupport Cache built on MongoDB
|
90
|
+
test_files:
|
91
|
+
- spec/cache/cache_spec.rb
|
92
|
+
- spec/mongo_store_spec.rb
|
93
|
+
- spec/spec_helper.rb
|