mongo_rails_cache 0.1.1 → 0.1.2
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/VERSION +1 -1
- data/lib/mongo_store.rb +1 -0
- data/lib/mongo_store/cache.rb +56 -0
- data/mongo_rails_cache.gemspec +15 -22
- data/spec/cache/cache_spec.rb +4 -4
- data/spec/mongo_store_spec.rb +8 -0
- data/spec/spec_helper.rb +1 -1
- metadata +10 -13
- data/.gitignore +0 -24
- data/lib/mongo_rails_cache.rb +0 -1
- data/lib/store/cache.rb +0 -50
- data/spec/mongo_rails_cache_spec.rb +0 -8
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
data/lib/mongo_store.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'mongo_store/cache'
|
@@ -0,0 +1,56 @@
|
|
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
|
+
def rescue_connection_failure(max_retries=15)
|
11
|
+
retries = 0
|
12
|
+
begin
|
13
|
+
yield
|
14
|
+
rescue Mongo::ConnectionFailure => ex
|
15
|
+
retries += 1
|
16
|
+
raise ex if retries > max_retries
|
17
|
+
sleep(0.5)
|
18
|
+
retry
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
module MongoStore
|
23
|
+
class Cache < ActiveSupport::Cache::Store
|
24
|
+
attr_reader :collection, :options
|
25
|
+
|
26
|
+
def initialize(collection, options={})
|
27
|
+
@collection, @options = collection, options || {}
|
28
|
+
end
|
29
|
+
|
30
|
+
def expires_in
|
31
|
+
@expires_in ||= options[:expires_in] || 1.month
|
32
|
+
end
|
33
|
+
|
34
|
+
def write_entry(key, entry, options)
|
35
|
+
doc = {:_id => key.to_s, :value => BSON::Binary.new(Marshal.dump(entry))}
|
36
|
+
rescue_connection_failure { @collection.save(doc) }
|
37
|
+
true
|
38
|
+
end
|
39
|
+
|
40
|
+
def read_entry(key, options)
|
41
|
+
entry = nil
|
42
|
+
rescue_connection_failure { entry = @collection.find_one(:_id => key.to_s) }
|
43
|
+
entry ? Marshal.load(entry['value'].to_s) : nil
|
44
|
+
end
|
45
|
+
|
46
|
+
# we don't get any info from mongo driver if delete actually deleted anything. this will always return true.
|
47
|
+
def delete_entry(key, options)
|
48
|
+
rescue_connection_failure { @collection.remove({:_id => key.to_s}) }
|
49
|
+
true
|
50
|
+
end
|
51
|
+
|
52
|
+
def clear(options=nil)
|
53
|
+
rescue_connection_failure { @collection.remove }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/mongo_rails_cache.gemspec
CHANGED
@@ -1,46 +1,39 @@
|
|
1
1
|
# Generated by jeweler
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongo_rails_cache}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Ashley Streb"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2011-10-18}
|
13
13
|
s.description = %q{Rails3 ActiveSupport Cache built on MongoDB}
|
14
14
|
s.email = %q{astreb@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
17
|
-
|
17
|
+
"README.rdoc"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
"spec/spec_helper.rb"
|
21
|
+
".rspec",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/mongo_store.rb",
|
27
|
+
"lib/mongo_store/cache.rb",
|
28
|
+
"mongo_rails_cache.gemspec",
|
29
|
+
"spec/cache/cache_spec.rb",
|
30
|
+
"spec/mongo_store_spec.rb",
|
31
|
+
"spec/spec_helper.rb"
|
33
32
|
]
|
34
33
|
s.homepage = %q{http://github.com/roark31337/mongo_rails_cache}
|
35
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
36
34
|
s.require_paths = ["lib"]
|
37
35
|
s.rubygems_version = %q{1.3.7}
|
38
36
|
s.summary = %q{Rails3 ActiveSupport Cache built on MongoDB}
|
39
|
-
s.test_files = [
|
40
|
-
"spec/cache/cache_spec.rb",
|
41
|
-
"spec/mongo_rails_cache_spec.rb",
|
42
|
-
"spec/spec_helper.rb"
|
43
|
-
]
|
44
37
|
|
45
38
|
if s.respond_to? :specification_version then
|
46
39
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
data/spec/cache/cache_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe MongoStore::Cache do
|
4
4
|
before(:each) do
|
5
5
|
DB.collections.each do |collection|
|
6
6
|
collection.remove
|
@@ -8,11 +8,11 @@ describe Mongo::Store::Cache do
|
|
8
8
|
end
|
9
9
|
|
10
10
|
@collection = DB['mongo_store_cache']
|
11
|
-
@store =
|
11
|
+
@store = MongoStore::Cache.new(@collection)
|
12
12
|
end
|
13
13
|
|
14
14
|
let(:collection) { DB['mongo_store_cache'] }
|
15
|
-
let(:store) {
|
15
|
+
let(:store) { MongoStore::Cache.new(collection, nil) }
|
16
16
|
|
17
17
|
it "has a collection" do
|
18
18
|
store.collection.should == collection
|
@@ -23,7 +23,7 @@ describe Mongo::Store::Cache do
|
|
23
23
|
end
|
24
24
|
|
25
25
|
it "can set default expires_in" do
|
26
|
-
|
26
|
+
MongoStore::Cache.new(collection, :expires_in => 5.minutes).expires_in.should == 5.minutes
|
27
27
|
end
|
28
28
|
|
29
29
|
describe "#write" do
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 2
|
9
|
+
version: 0.1.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Ashley Streb
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date:
|
17
|
+
date: 2011-10-18 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -43,25 +43,24 @@ extra_rdoc_files:
|
|
43
43
|
- README.rdoc
|
44
44
|
files:
|
45
45
|
- .document
|
46
|
-
- .gitignore
|
47
46
|
- .rspec
|
48
47
|
- LICENSE
|
49
48
|
- README.rdoc
|
50
49
|
- Rakefile
|
51
50
|
- VERSION
|
52
|
-
- lib/
|
53
|
-
- lib/
|
51
|
+
- lib/mongo_store.rb
|
52
|
+
- lib/mongo_store/cache.rb
|
54
53
|
- mongo_rails_cache.gemspec
|
55
54
|
- spec/cache/cache_spec.rb
|
56
|
-
- spec/
|
55
|
+
- spec/mongo_store_spec.rb
|
57
56
|
- spec/spec_helper.rb
|
58
57
|
has_rdoc: true
|
59
58
|
homepage: http://github.com/roark31337/mongo_rails_cache
|
60
59
|
licenses: []
|
61
60
|
|
62
61
|
post_install_message:
|
63
|
-
rdoc_options:
|
64
|
-
|
62
|
+
rdoc_options: []
|
63
|
+
|
65
64
|
require_paths:
|
66
65
|
- lib
|
67
66
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -87,7 +86,5 @@ rubygems_version: 1.3.7
|
|
87
86
|
signing_key:
|
88
87
|
specification_version: 3
|
89
88
|
summary: Rails3 ActiveSupport Cache built on MongoDB
|
90
|
-
test_files:
|
91
|
-
|
92
|
-
- spec/mongo_rails_cache_spec.rb
|
93
|
-
- spec/spec_helper.rb
|
89
|
+
test_files: []
|
90
|
+
|
data/.gitignore
DELETED
data/lib/mongo_rails_cache.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require 'store/cache'
|
data/lib/store/cache.rb
DELETED
@@ -1,50 +0,0 @@
|
|
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 Mongo
|
11
|
-
module Store
|
12
|
-
class Cache < ActiveSupport::Cache::Store
|
13
|
-
attr_reader :collection, :options
|
14
|
-
|
15
|
-
def initialize(collection, options={})
|
16
|
-
@collection, @options = collection, options || {}
|
17
|
-
end
|
18
|
-
|
19
|
-
def expires_in
|
20
|
-
@expires_in ||= options[:expires_in] || 1.month
|
21
|
-
end
|
22
|
-
|
23
|
-
def write_entry(key, entry, options)
|
24
|
-
doc = {:_id => key.to_s, :value => BSON::Binary.new(Marshal.dump(entry))}
|
25
|
-
@collection.save(doc)
|
26
|
-
true
|
27
|
-
end
|
28
|
-
|
29
|
-
def read_entry(key, options)
|
30
|
-
entry = @collection.find_one(:_id => key.to_s)
|
31
|
-
if entry
|
32
|
-
Marshal.load(entry['value'].to_s)
|
33
|
-
else
|
34
|
-
nil
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
# we don't get any info from mongo driver if delete actually deleted anything. this will always return true.
|
39
|
-
def delete_entry(key, options)
|
40
|
-
@collection.remove({:_id => key.to_s})
|
41
|
-
true
|
42
|
-
end
|
43
|
-
|
44
|
-
def clear(options=nil)
|
45
|
-
@collection.remove
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|