mongo_cache_store 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +4 -0
- data/lib/active_support/cache/mongo_cache_store.rb +27 -1
- data/lib/active_support/cache/mongo_cache_store/backend/base.rb +5 -3
- data/lib/active_support/cache/mongo_cache_store/backend/multi_ttl.rb +21 -14
- data/lib/active_support/cache/mongo_cache_store/backend/standard.rb +0 -3
- data/lib/active_support/cache/mongo_cache_store/backend/ttl.rb +0 -3
- data/lib/mongo_cache_store/version.rb +1 -1
- data/spec/active_support/cache/mongo_cache_store_spec.rb +19 -0
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -50,6 +50,10 @@ configured or used.
|
|
50
50
|
then serialize the entry.
|
51
51
|
[+:never+ - Never serialize]
|
52
52
|
Only save the entry if it can be saved natively by MongoDB.
|
53
|
+
[+:collection_opts+]
|
54
|
+
Hash of options passed directly to MongoDB::Collection.
|
55
|
+
|
56
|
+
Useful for write conditions and read preferences
|
53
57
|
|
54
58
|
|
55
59
|
== Backends
|
@@ -2,6 +2,7 @@
|
|
2
2
|
require "mongo_cache_store/version"
|
3
3
|
require "mongo"
|
4
4
|
require "active_support/cache"
|
5
|
+
require "logger"
|
5
6
|
|
6
7
|
module ActiveSupport
|
7
8
|
module Cache
|
@@ -32,6 +33,10 @@ module ActiveSupport
|
|
32
33
|
# then serialize the entry.
|
33
34
|
# [+:never+ - Never serialize]
|
34
35
|
# Only save the entry if it can be saved natively by MongoDB.
|
36
|
+
# [+:collection_opts+ ]
|
37
|
+
# Hash of options passed directly to MongoDB::Collection.
|
38
|
+
#
|
39
|
+
# Useful for write conditions and read preferences
|
35
40
|
#
|
36
41
|
# === Examples
|
37
42
|
# @store = ActiveSupport::Cache::MongoCacheStore.new(:TTL, :db => Mongo::DB.new('db_name',Mongo::Connection.new))
|
@@ -45,15 +50,19 @@ module ActiveSupport
|
|
45
50
|
:db => nil,
|
46
51
|
:namespace => nil,
|
47
52
|
:connection => nil,
|
48
|
-
:serialize => :always
|
53
|
+
:serialize => :always,
|
54
|
+
:collection_opts => {}
|
49
55
|
}.merge(options)
|
50
56
|
|
51
57
|
@db = options.delete :db
|
58
|
+
@logger = options.delete :logger
|
52
59
|
|
53
60
|
if (@db.nil?)
|
54
61
|
@db = Mongo::DB.new(options[:db_name], options[:connection] || Mongo::Connection.new)
|
55
62
|
end
|
56
63
|
|
64
|
+
|
65
|
+
|
57
66
|
extend ActiveSupport::Cache::MongoCacheStore::Backend.const_get(backend)
|
58
67
|
|
59
68
|
build_backend(options)
|
@@ -61,6 +70,23 @@ module ActiveSupport
|
|
61
70
|
super(options)
|
62
71
|
|
63
72
|
end
|
73
|
+
|
74
|
+
def logger
|
75
|
+
return @logger unless @logger.nil?
|
76
|
+
|
77
|
+
slogger = super
|
78
|
+
case
|
79
|
+
when !slogger.nil?
|
80
|
+
@logger = slogger
|
81
|
+
when defined?(Rails) && Rails.logger
|
82
|
+
@logger = Rails.logger
|
83
|
+
else
|
84
|
+
@logger = Logger.new(STDOUT)
|
85
|
+
end
|
86
|
+
|
87
|
+
@logger
|
88
|
+
end
|
89
|
+
|
64
90
|
end
|
65
91
|
end
|
66
92
|
end
|
@@ -61,7 +61,6 @@ module ActiveSupport
|
|
61
61
|
:serialized => serialize,
|
62
62
|
:value => serialize ? BSON::Binary.new(entry.raw_value) : entry.value
|
63
63
|
}.merge(options[:xentry] || {})
|
64
|
-
#puts save_doc.inspect
|
65
64
|
|
66
65
|
safe_rescue do
|
67
66
|
begin
|
@@ -88,11 +87,14 @@ module ActiveSupport
|
|
88
87
|
name_parts = ['cache']
|
89
88
|
name_parts.push(backend_name)
|
90
89
|
name_parts.push options[:namespace] unless options[:namespace].nil?
|
91
|
-
|
90
|
+
name = name_parts.join('.')
|
91
|
+
return name
|
92
92
|
end
|
93
93
|
|
94
94
|
def get_collection(options)
|
95
95
|
return options[:collection] if options[:collection].is_a? Mongo::Collection
|
96
|
+
|
97
|
+
@db.collection(get_collection_name(options),options[:collection_opts])
|
96
98
|
end
|
97
99
|
|
98
100
|
def safe_rescue
|
@@ -100,7 +102,7 @@ module ActiveSupport
|
|
100
102
|
yield
|
101
103
|
rescue => e
|
102
104
|
warn e
|
103
|
-
logger.error("
|
105
|
+
logger.error("MongoCacheStoreError (#{e}): #{e.message}") if logger
|
104
106
|
false
|
105
107
|
end
|
106
108
|
end
|
@@ -48,8 +48,6 @@ module ActiveSupport
|
|
48
48
|
module MultiTTL
|
49
49
|
include Base
|
50
50
|
|
51
|
-
alias :get_collection_prefix :get_collection_name
|
52
|
-
|
53
51
|
def clear(options = {})
|
54
52
|
@db.collection_names.each do |cname|
|
55
53
|
prefix = get_collection_prefix
|
@@ -86,6 +84,7 @@ module ActiveSupport
|
|
86
84
|
ki.save({
|
87
85
|
:_id => key,
|
88
86
|
:collection => options[:collection].name,
|
87
|
+
:expires_in => options[:expires_in].nil? ? nil : options[:expires_in].to_i,
|
89
88
|
:expires_at => entry.expires_at
|
90
89
|
})
|
91
90
|
end
|
@@ -101,24 +100,36 @@ module ActiveSupport
|
|
101
100
|
|
102
101
|
private
|
103
102
|
|
103
|
+
alias :super_get_collection_name :get_collection_name
|
104
|
+
|
104
105
|
def backend_name
|
105
106
|
"multi_ttl"
|
106
107
|
end
|
107
108
|
|
109
|
+
|
108
110
|
def get_collection(options)
|
109
111
|
|
110
|
-
col =
|
112
|
+
col = @collection_map[get_collection_name(options)]
|
111
113
|
return col unless col.nil?
|
112
114
|
|
115
|
+
col = super
|
116
|
+
expires_in = options[:expires_in]
|
117
|
+
col.ensure_index('created_at',{ expireAfterSeconds: expires_in.to_i }) unless expires_in.nil?
|
118
|
+
@collection_map[col.name] = col
|
119
|
+
|
120
|
+
return col
|
121
|
+
end
|
122
|
+
|
123
|
+
def get_collection_name(options)
|
113
124
|
name_parts = [get_collection_prefix(options)]
|
114
125
|
expires_in = options[:expires_in]
|
115
126
|
name_parts.push expires_in.nil? ? 'forever' : expires_in.to_i
|
116
127
|
collection_name = name_parts.join('.')
|
128
|
+
return collection_name
|
129
|
+
end
|
117
130
|
|
118
|
-
|
119
|
-
|
120
|
-
collection ||= create_collection(collection_name, expires_in)
|
121
|
-
return collection
|
131
|
+
def get_collection_prefix(options = {})
|
132
|
+
super_get_collection_name(options)
|
122
133
|
end
|
123
134
|
|
124
135
|
|
@@ -132,6 +143,7 @@ module ActiveSupport
|
|
132
143
|
return col
|
133
144
|
end
|
134
145
|
|
146
|
+
|
135
147
|
def get_collection_from_index(key,options)
|
136
148
|
if (options[:use_index])
|
137
149
|
ki = get_key_index_collection(options)
|
@@ -142,19 +154,14 @@ module ActiveSupport
|
|
142
154
|
})
|
143
155
|
return nil if response.nil?
|
144
156
|
|
145
|
-
|
157
|
+
options[:expires_in] = response['expires_in']
|
158
|
+
return get_collection(options)
|
146
159
|
end
|
147
160
|
end
|
148
161
|
nil
|
149
162
|
end
|
150
163
|
|
151
164
|
|
152
|
-
def create_collection(name, expires_in)
|
153
|
-
collection = @db[name]
|
154
|
-
collection.ensure_index('created_at',{ expireAfterSeconds: expires_in.to_i }) unless expires_in.nil?
|
155
|
-
return collection
|
156
|
-
end
|
157
|
-
|
158
165
|
def build_backend(options)
|
159
166
|
options.replace({
|
160
167
|
:use_index => true
|
@@ -58,9 +58,6 @@ module ActiveSupport
|
|
58
58
|
def get_collection(options)
|
59
59
|
return @collection if @collection.is_a? Mongo::Collection
|
60
60
|
collection = super
|
61
|
-
return collection unless collection.nil?
|
62
|
-
|
63
|
-
collection = @db[get_collection_name(options)]
|
64
61
|
collection.ensure_index('expires_at',{ expireAfterSeconds: 0 })
|
65
62
|
@collection = collection
|
66
63
|
end
|
@@ -154,6 +154,25 @@ module ActiveSupport
|
|
154
154
|
@store = ActiveSupport::Cache::MongoCacheStore.new(:Standard, :db => db, :serialize => :on_fail)
|
155
155
|
end
|
156
156
|
end
|
157
|
+
|
158
|
+
describe "Pass Collection Options" do
|
159
|
+
before(:all) do
|
160
|
+
db = Mongo::DB.new('mongo_cache_store_test', Mongo::Connection.new)
|
161
|
+
@store = ActiveSupport::Cache::MongoCacheStore.new(
|
162
|
+
:Standard,
|
163
|
+
:db => db,
|
164
|
+
:collection_opts => {
|
165
|
+
:read => :nearest
|
166
|
+
}
|
167
|
+
)
|
168
|
+
end
|
169
|
+
|
170
|
+
it "can set collection opts" do
|
171
|
+
col = @store.send(:get_collection,{:collection_opts => {:read => :nearest}})
|
172
|
+
col.instance_eval { @read }.should == :nearest
|
173
|
+
end
|
174
|
+
|
175
|
+
end
|
157
176
|
end
|
158
177
|
end
|
159
178
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo_cache_store
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongo
|