mongo_cache_store 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -1,14 +1,28 @@
1
1
  language: ruby
2
+ env:
3
+ - "ACTIVESUPPORT_VERSION=3.1"
4
+ - "ACTIVESUPPORT_VERSION=3.2"
5
+ - "ACTIVESUPPORT_VERSION=4"
2
6
  rvm:
3
7
  - 2.0.0
4
8
  - 1.9.3
5
9
  - 1.9.2
6
10
  - jruby-18mode
7
11
  - jruby-19mode
8
- - rbx-19mode
12
+ - rbx-19mode
9
13
  - ruby-head
10
14
  - jruby-head
11
15
  - 1.8.7
12
16
  - ree
13
17
  services:
14
18
  - mongodb
19
+ matrix:
20
+ exclude:
21
+ - rvm: 1.9.2
22
+ env: "ACTIVESUPPORT_VERSION=4"
23
+ - rvm: jruby-18mode
24
+ env: "ACTIVESUPPORT_VERSION=4"
25
+ - rvm: 1.8.7
26
+ env: "ACTIVESUPPORT_VERSION=4"
27
+ - rvm: ree
28
+ env: "ACTIVESUPPORT_VERSION=4"
data/Gemfile CHANGED
@@ -1,4 +1,18 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ as_version = ENV["ACTIVESUPPORT_VERSION"] || "default"
4
+
5
+ as = case as_version
6
+ when "master"
7
+ {:github => "rails/rails"}
8
+ when "default"
9
+ "~> 3.2.0"
10
+ else
11
+ "~> #{as_version}"
12
+ end
13
+
14
+ gem "activesupport", as
15
+
3
16
  # Specify your gem's dependencies in mongo_cache_store.gemspec
4
17
  gemspec
18
+
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # MongoCacheStore
2
2
 
3
- A MongoDB cache store for ActiveSupport 3
3
+ A MongoDB cache store for ActiveSupport
4
4
 
5
5
  ## Description
6
6
 
@@ -9,6 +9,16 @@ as a cache store to ActiveSupport applications. Each backend
9
9
  allows the application to customize how the cache operates.
10
10
  Support is available for standard, capped and TTL collections.
11
11
 
12
+ ## Upgrade Note
13
+
14
+ To accomidate ActiveSupport 4 underlying storage routines had
15
+ to change. After upgrading to 0.3.0, any cached entries from 0.2.x
16
+ will be considered a miss and will be overridden with a corresponding
17
+ write action or expire naturally.
18
+
19
+ MongoCacheStore::DATA\_STORE\_VERSION determines whether a cached entry
20
+ is compatible with the current version of the gem.
21
+
12
22
 
13
23
  ## Initialize the cache
14
24
 
@@ -190,9 +200,10 @@ be flushed until it is automatically removed by MongoDB.
190
200
 
191
201
  TODO
192
202
 
193
- ## Build Status
203
+ ## Build Health
194
204
  [![Build Status - Master](https://travis-ci.org/kmcgrath/mongo_cache_store.png?branch=master)](https://travis-ci.org/kmcgrath/mongo_cache_store)
195
- [![Build Status - Develop](https://travis-ci.org/kmcgrath/mongo_cache_store.png?branch=develop)](https://travis-ci.org/kmcgrath/mongo_cache_store)
205
+ [![Code Climate](https://codeclimate.com/github/kmcgrath/mongo_cache_store.png)](https://codeclimate.com/github/kmcgrath/mongo_cache_store)
206
+ [![Dependency Status](https://gemnasium.com/kmcgrath/mongo_cache_store.png)](https://gemnasium.com/kmcgrath/mongo_cache_store)
196
207
 
197
208
  Travis is used to build and test MongoCacheStore against:
198
209
  * 2.0.0
@@ -20,7 +20,7 @@ module ActiveSupport
20
20
  options = names.extract_options!
21
21
  options = merged_options(options)
22
22
  results = {}
23
-
23
+
24
24
  col = get_collection(options)
25
25
 
26
26
  key_map = names.inject({}) do |h, name|
@@ -28,18 +28,17 @@ module ActiveSupport
28
28
  h
29
29
  end
30
30
 
31
- safe_rescue do
31
+ safe_rescue do
32
32
  query = {
33
33
  :_id => { '$in' => key_map.keys},
34
34
  :expires_at => {
35
- '$gt' => Time.now
35
+ '$gt' => Time.now
36
36
  }
37
37
  }
38
38
 
39
39
  col.find(query) do |cursor|
40
40
  cursor.each do |r|
41
41
  results[key_map[r['_id']]] = inflate_entry(r).value
42
- puts results.inspect
43
42
  end
44
43
  end
45
44
  end
@@ -84,12 +83,13 @@ module ActiveSupport
84
83
  def read_entry(key,options)
85
84
  col = get_collection(options)
86
85
 
87
- safe_rescue do
86
+ safe_rescue do
88
87
  query = {
89
88
  :_id => key,
90
89
  :expires_at => {
91
- '$gt' => Time.now
92
- }
90
+ '$gt' => Time.now
91
+ },
92
+ :data_store_version => ::MongoCacheStore::DATA_STORE_VERSION
93
93
  }
94
94
 
95
95
  response = col.find_one(query)
@@ -102,16 +102,17 @@ module ActiveSupport
102
102
  def inflate_entry(from_mongo)
103
103
  return nil if from_mongo.nil?
104
104
 
105
- entry_options = {
106
- :compressed => from_mongo['compressed'],
107
- :expires_in => from_mongo['expires_in']
108
- }
109
- if from_mongo['serialized']
110
- r_value = from_mongo['value'].to_s
111
- else
112
- r_value = Marshal.dump(from_mongo['value'])
105
+ case from_mongo['value']
106
+ when BSON::Binary
107
+ fm = Marshal.load(from_mongo['value'].to_s)
108
+ case fm
109
+ when Entry
110
+ if from_mongo['raw_value'].nil? == false
111
+ fm.instance_eval { @value = from_mongo['raw_value'] }
112
+ end
113
+ fm
114
+ end
113
115
  end
114
- ActiveSupport::Cache::Entry.create(r_value,from_mongo['created_at'],entry_options)
115
116
  end
116
117
 
117
118
  def write_counter(name, amount, options)
@@ -121,17 +122,18 @@ module ActiveSupport
121
122
  safe_rescue do
122
123
  doc = col.find_and_modify(
123
124
  :query => {
124
- :_id => key
125
+ :_id => key,
126
+ :data_store_version => ::MongoCacheStore::DATA_STORE_VERSION
125
127
  },
126
128
  :update => {
127
129
  :$inc => {
128
- :value => amount
130
+ :raw_value => amount
129
131
  }
130
132
  }
131
133
  )
132
134
 
133
135
  return nil unless doc
134
- doc['value'] + amount
136
+ doc['raw_value'] + amount
135
137
  end
136
138
  end
137
139
 
@@ -140,37 +142,35 @@ module ActiveSupport
140
142
  serialize = options[:serialize] == :always ? true : false
141
143
  serialize = false if entry.value.is_a?(Integer) || entry.value.nil?
142
144
 
143
- value = begin
144
- if entry.compressed?
145
- BSON::Binary.new(entry.raw_value)
146
- elsif serialize
147
- BSON::Binary.new(entry.raw_value)
148
- else
149
- entry.value
150
- end
145
+ value = BSON::Binary.new(Marshal.dump(entry))
146
+ unless entry.send(:compressed?) || serialize
147
+ raw_value = entry.value
148
+ entry.instance_eval { @value = nil }
151
149
  end
152
150
 
153
151
  try_cnt = 0
154
152
 
155
153
  save_doc = {
156
154
  :_id => key,
157
- :created_at => Time.at(entry.created_at),
158
- :expires_in => entry.expires_in,
159
- :expires_at => entry.expires_in.nil? ? Time.utc(9999) : Time.at(entry.expires_at),
160
- :compressed => entry.compressed?,
155
+ :created_at => Time.at(entry.instance_eval { @created_at}),
156
+ :expires_in => entry.instance_eval { @expires_in },
157
+ :expires_at => entry.expires_at.nil? ? Time.utc(9999) : Time.at(entry.expires_at),
158
+ :compressed => entry.send( :compressed? ),
161
159
  :serialized => serialize,
162
- :value => value
160
+ :value => value,
161
+ :raw_value => raw_value,
162
+ :data_store_version => ::MongoCacheStore::DATA_STORE_VERSION
163
163
  }.merge(options[:xentry] || {})
164
164
 
165
165
  safe_rescue do
166
166
  begin
167
167
  col.save(save_doc)
168
- rescue BSON::InvalidDocument => ex
168
+ rescue BSON::InvalidDocument
169
169
  if (options[:serialize] == :on_fail and try_cnt < 2)
170
170
  save_doc[:serialized] = true
171
171
  save_doc[:value] = BSON::Binary.new(entry.raw_value)
172
172
  try_cnt += 1
173
- retry
173
+ retry
174
174
  end
175
175
  end
176
176
  end
@@ -186,7 +186,7 @@ module ActiveSupport
186
186
  end
187
187
 
188
188
  def get_collection_name(options = {})
189
- name_parts = ['cache']
189
+ name_parts = ['cache']
190
190
  name_parts.push(backend_name)
191
191
  name_parts.push options[:namespace] if !options[:namespace].nil?
192
192
  name = name_parts.join('.')
@@ -85,7 +85,7 @@ module ActiveSupport
85
85
  :_id => key,
86
86
  :collection => options[:collection].name,
87
87
  :expires_in => options[:expires_in].nil? ? nil : options[:expires_in].to_i,
88
- :expires_at => entry.expires_at
88
+ :expires_at => Time.at(entry.expires_at)
89
89
  })
90
90
  end
91
91
  end
@@ -43,22 +43,22 @@ module ActiveSupport
43
43
  now = Time.now
44
44
  options[:xentry] = {
45
45
  :created_at => now,
46
- :expires_at => entry.expires_in.nil? ? Time.utc(9999) : now + entry.expires_in.to_i
46
+ :expires_at => entry.expires_at.nil? ? Time.utc(9999) : Time.at(entry.expires_at)
47
47
  }
48
48
 
49
49
  super(key,entry,options)
50
50
  end
51
51
 
52
52
 
53
- private
53
+ private
54
54
 
55
55
  def backend_name
56
56
  "ttl"
57
57
  end
58
58
 
59
59
  def get_collection(options)
60
- return @collection if @collection.is_a? Mongo::Collection
61
- collection = super
60
+ return @collection if (@collection && @collection.is_a?(Mongo::Collection))
61
+ collection = super
62
62
  collection.ensure_index('expires_at',{ :expireAfterSeconds => 0 })
63
63
  @collection = collection
64
64
  end
@@ -1,4 +1,5 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module MongoCacheStore
3
- VERSION = "0.2.5"
3
+ VERSION = "0.3.0"
4
+ DATA_STORE_VERSION = "2"
4
5
  end
@@ -17,8 +17,8 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
 
20
- gem.add_dependency 'mongo', '~>1'
21
- gem.add_dependency 'activesupport', '~>3'
20
+ gem.add_dependency 'mongo', '~>1.8'
21
+ gem.add_dependency 'activesupport', '>= 3', '<4.1'
22
22
  gem.add_development_dependency 'mocha', '~>0.13'
23
23
  gem.add_development_dependency 'rake', '~>10.0'
24
24
  end
data/test/test_modules.rb CHANGED
@@ -198,19 +198,21 @@ module CacheStoreBehavior
198
198
  assert_equal false, @cache.read('foo')
199
199
  end
200
200
 
201
- def test_should_read_cached_numeric_from_previous_rails_versions
202
- @old_cache = ActiveSupport::Cache::Entry.create( 1, Time.now )
203
- assert_equal( 1, @old_cache.value )
204
- end
201
+ if ActiveSupport::VERSION::MAJOR == 3
202
+ def test_should_read_cached_numeric_from_previous_rails_versions
203
+ @old_cache = ActiveSupport::Cache::Entry.create( 1, Time.now )
204
+ assert_equal( 1, @old_cache.value )
205
+ end
205
206
 
206
- def test_should_read_cached_hash_from_previous_rails_versions
207
- @old_cache = ActiveSupport::Cache::Entry.create( {}, Time.now )
208
- assert_equal( {}, @old_cache.value )
209
- end
207
+ def test_should_read_cached_hash_from_previous_rails_versions
208
+ @old_cache = ActiveSupport::Cache::Entry.create( {}, Time.now )
209
+ assert_equal( {}, @old_cache.value )
210
+ end
210
211
 
211
- def test_should_read_cached_string_from_previous_rails_versions
212
- @old_cache = ActiveSupport::Cache::Entry.create( 'string', Time.now )
213
- assert_equal( 'string', @old_cache.value )
212
+ def test_should_read_cached_string_from_previous_rails_versions
213
+ @old_cache = ActiveSupport::Cache::Entry.create( 'string', Time.now )
214
+ assert_equal( 'string', @old_cache.value )
215
+ end
214
216
  end
215
217
 
216
218
  def test_read_multi
@@ -228,18 +230,20 @@ module CacheStoreBehavior
228
230
  assert_equal({"fu" => "baz"}, @cache.read_multi('foo', 'fu'))
229
231
  end
230
232
 
231
- def test_read_and_write_compressed_small_data
232
- @cache.write('foo', 'bar', :compress => true)
233
- raw_value = @cache.send(:read_entry, 'foo', {}).raw_value
234
- assert_equal 'bar', @cache.read('foo')
235
- assert_equal 'bar', Marshal.load(raw_value)
236
- end
233
+ if ActiveSupport::VERSION::MAJOR == 3
234
+ def test_read_and_write_compressed_small_data
235
+ @cache.write('foo', 'bar', :compress => true)
236
+ raw_value = @cache.send(:read_entry, 'foo', {}).raw_value
237
+ assert_equal 'bar', @cache.read('foo')
238
+ assert_equal 'bar', Marshal.load(raw_value)
239
+ end
237
240
 
238
- def test_read_and_write_compressed_large_data
239
- @cache.write('foo', 'bar', :compress => true, :compress_threshold => 2)
240
- raw_value = @cache.send(:read_entry, 'foo', {}).raw_value
241
- assert_equal 'bar', @cache.read('foo')
242
- assert_equal 'bar', Marshal.load(Zlib::Inflate.inflate(raw_value))
241
+ def test_read_and_write_compressed_large_data
242
+ @cache.write('foo', 'bar', :compress => true, :compress_threshold => 2)
243
+ raw_value = @cache.send(:read_entry, 'foo', {}).raw_value
244
+ assert_equal 'bar', @cache.read('foo')
245
+ assert_equal 'bar', Marshal.load(Zlib::Inflate.inflate(raw_value))
246
+ end
243
247
  end
244
248
 
245
249
  def test_read_and_write_compressed_nil
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.5
4
+ version: 0.3.0
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-04-14 00:00:00.000000000 Z
12
+ date: 2013-10-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongo
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '1'
21
+ version: '1.8'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,23 +26,29 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: '1'
29
+ version: '1.8'
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: activesupport
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
- - - ~>
35
+ - - ! '>='
36
36
  - !ruby/object:Gem::Version
37
37
  version: '3'
38
+ - - <
39
+ - !ruby/object:Gem::Version
40
+ version: '4.1'
38
41
  type: :runtime
39
42
  prerelease: false
40
43
  version_requirements: !ruby/object:Gem::Requirement
41
44
  none: false
42
45
  requirements:
43
- - - ~>
46
+ - - ! '>='
44
47
  - !ruby/object:Gem::Version
45
48
  version: '3'
49
+ - - <
50
+ - !ruby/object:Gem::Version
51
+ version: '4.1'
46
52
  - !ruby/object:Gem::Dependency
47
53
  name: mocha
48
54
  requirement: !ruby/object:Gem::Requirement