mongoid-store 0.4.3 → 0.4.4
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.
- checksums.yaml +8 -8
- data/README.md +24 -0
- data/lib/active_support/cache/mongoid_store.rb +144 -0
- data/lib/mongoid-store/version.rb +1 -1
- metadata +3 -3
- data/pkg/mongoid-store-0.4.2.gem +0 -0
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZDRiNmQyNzFhZDU3YTcxNWVmODY1Zjc2YTczMzUyYTkzYjE3MTdiMA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YWE0ZjVkNDE4OWNiMWRiNmRmNzFmZTk4MGViN2RhMjdiNDE3NmE0ZA==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NGJlOTUyZGE0NzUyODQyYTQxNTNjYmU0NmM0MjQ1NzJjZjJhNjQxNzc0Y2Zl
|
10
|
+
OWZhNGIyMDAwNDJhNzY4ZmM2Yjg5NjE4ZmFhYjk4OWY0YzJjODM5NWM5OWE2
|
11
|
+
YzZiMzBmMmYxOTJkMzQzMmIyMWE0ODM5MDMzZjlkNjU4MWE4MTQ=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
Njk5MTY0NTVmNDQ4M2YzYzJjMWYzYWU0MWIwZDYxYzM3NmUzNTM5MWU2YTYz
|
14
|
+
OGZkMDQyODI5Y2M5OGJlMmI4YzBiNjk1MTllMTBiYmUzZmQzNzE5MzJmMjM1
|
15
|
+
YTQ0ZmJmMTZhNjJiZGUzNGQ5OGJmNGJiMjAxMDAyOWQzODcyZDI=
|
data/README.md
CHANGED
@@ -7,6 +7,23 @@ gem by Andre Meij (see his orginial LICENSE)
|
|
7
7
|
unlike the the original gem this version works with both rails3 and rails4,
|
8
8
|
and handles any object in any encoding (remember mongo is utf8 only)
|
9
9
|
|
10
|
+
Why?
|
11
|
+
----
|
12
|
+
|
13
|
+
mongoid-store is not as fast as redis-store. in my tests inserting 10_000
|
14
|
+
random strings takes about 6 seconds for mongoid-store and 2 for redis-store.
|
15
|
+
|
16
|
+
in our (@dojo4) applications we use mongoid-store to reduce the number of
|
17
|
+
requirements when applications are young: avoiding the need to setup *two*
|
18
|
+
highly avaiable RAM devouring db services (redis + mongo). instead we can
|
19
|
+
simply configure our applications to use a solid mongo service like
|
20
|
+
http://www.objectrocket.com/ - which provides a highly available and
|
21
|
+
automatically scalable mongo layer, two app servers, a load balancer, are
|
22
|
+
we're pretty set. we also like to put all our images in mongo using
|
23
|
+
mongoid-grid_fs (or s3) and even our background jobs so, at the end of the
|
24
|
+
day, mongoid-store is aimed at keeping new application deployment simple and
|
25
|
+
relatively dependency free.
|
26
|
+
|
10
27
|
|
11
28
|
|
12
29
|
Supports
|
@@ -58,3 +75,10 @@ Using MongoidStore with rails is as easy as:
|
|
58
75
|
|
59
76
|
Rails.cache.write(:key, :val)
|
60
77
|
```
|
78
|
+
|
79
|
+
References
|
80
|
+
----------
|
81
|
+
|
82
|
+
http://www.slideshare.net/mongodb/mongodb-as-a-fast-and-queryable-cache
|
83
|
+
http://stackoverflow.com/questions/10317732/why-use-redis-instead-of-mongodb-for-caching
|
84
|
+
|
@@ -0,0 +1,144 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
require 'mongoid'
|
4
|
+
require 'active_support'
|
5
|
+
|
6
|
+
module ActiveSupport
|
7
|
+
module Cache
|
8
|
+
class MongoidStore < Store
|
9
|
+
attr_reader :collection_name
|
10
|
+
|
11
|
+
def initialize(options = {})
|
12
|
+
@collection_name = options[:collection] || :rails_cache
|
13
|
+
options[:expires_in] ||= 1.hour
|
14
|
+
super(options)
|
15
|
+
end
|
16
|
+
|
17
|
+
def clear(options = nil)
|
18
|
+
collection.find.remove_all
|
19
|
+
end
|
20
|
+
|
21
|
+
def cleanup(options = nil)
|
22
|
+
options = merged_options(options)
|
23
|
+
collection.find(expires_at: {'$lt' => Time.now.utc.to_i}).remove_all
|
24
|
+
end
|
25
|
+
|
26
|
+
def delete_matched(matcher, options = nil)
|
27
|
+
options = merged_options(options)
|
28
|
+
collection.find(_id: key_matcher(matcher, options)).remove_all
|
29
|
+
end
|
30
|
+
|
31
|
+
def delete_entry(key, options = nil)
|
32
|
+
collection.find(_id: key).remove
|
33
|
+
end
|
34
|
+
|
35
|
+
protected
|
36
|
+
|
37
|
+
def write_entry(key, entry, options)
|
38
|
+
data = Entry.data_for(entry)
|
39
|
+
expires_at = entry.expires_at.to_i
|
40
|
+
created_at = Time.now.utc.to_i
|
41
|
+
|
42
|
+
collection.find(_id: key).upsert(_id: key, data: data, expires_at: expires_at, created_at: created_at)
|
43
|
+
|
44
|
+
entry
|
45
|
+
end
|
46
|
+
|
47
|
+
def read_entry(key, options = {})
|
48
|
+
expires_at = Time.now.utc.to_i
|
49
|
+
doc = collection.find(_id: key, expires_at: {'$gt' => expires_at}).first
|
50
|
+
|
51
|
+
Entry.for(doc) if doc
|
52
|
+
end
|
53
|
+
|
54
|
+
# this class exists to normalize between rails3 and rails4, but also to
|
55
|
+
# repair totally broken interfaces in rails - especially in rails3 - that
|
56
|
+
# result in lots of extra serialization/deserialzation in a class which is
|
57
|
+
# supposed to be FAST
|
58
|
+
#
|
59
|
+
class Entry < ::ActiveSupport::Cache::Entry
|
60
|
+
def Entry.is_rails3?
|
61
|
+
unless defined?(@is_rails3)
|
62
|
+
@is_rails3 = new(nil).instance_variable_defined?('@value')
|
63
|
+
end
|
64
|
+
|
65
|
+
@is_rails3
|
66
|
+
end
|
67
|
+
|
68
|
+
# extract marshaled data from a cache entry without doing unnecessary
|
69
|
+
# marshal round trips. rails3 will have either a nil or pre-marshaled
|
70
|
+
# @value whereas rails4 will have either a marshaled or un-marshaled @v.
|
71
|
+
# in both cases we want to avoid calling the silly 'value' accessor
|
72
|
+
# since this will cause a potential Marshal.load call and require us to
|
73
|
+
# make a subsequent Marshal.dump call which is SLOOOWWW.
|
74
|
+
#
|
75
|
+
if is_rails3?
|
76
|
+
|
77
|
+
def Entry.data_for(entry)
|
78
|
+
value = entry.instance_variable_get('@value')
|
79
|
+
marshaled = value.nil? ? Marshal.dump(value) : value
|
80
|
+
Moped::BSON::Binary.new(:generic, marshaled)
|
81
|
+
end
|
82
|
+
|
83
|
+
else
|
84
|
+
|
85
|
+
def Entry.data_for(entry)
|
86
|
+
v = entry.instance_variable_get('@v')
|
87
|
+
marshaled = entry.send('compressed?') ? v : entry.send('compress', v)
|
88
|
+
Moped::BSON::Binary.new(:generic, marshaled)
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
# the intializer for rails' default Entry class will go ahead and
|
94
|
+
# perform and extraneous Marshal.dump on the data we just got from the
|
95
|
+
# db even though we don't need it here. rails3 has a factory to avoid
|
96
|
+
# this but rails4 does not so we just build the object we want and
|
97
|
+
# ensure to avoid any unnecessary calls to Marshal.dump/load... sigh.
|
98
|
+
#
|
99
|
+
if is_rails3?
|
100
|
+
|
101
|
+
def Entry.for(doc)
|
102
|
+
data = doc['data'].to_s
|
103
|
+
value = Marshal.load(data)
|
104
|
+
created_at = doc['created_at'].to_f
|
105
|
+
|
106
|
+
allocate.tap do |entry|
|
107
|
+
entry.instance_variable_set(:@value, value)
|
108
|
+
entry.instance_variable_set(:@compressed, false)
|
109
|
+
entry.instance_variable_set(:@created_at, created_at)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
else
|
114
|
+
|
115
|
+
def Entry.for(doc)
|
116
|
+
data = doc['data'].to_s
|
117
|
+
value = Marshal.load(data)
|
118
|
+
created_at = doc['created_at'].to_f
|
119
|
+
|
120
|
+
allocate.tap do |entry|
|
121
|
+
entry.instance_variable_set(:@v, value)
|
122
|
+
entry.instance_variable_set(:@c, false)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
127
|
+
|
128
|
+
def value
|
129
|
+
Entry.is_rails3? ? @value : @v
|
130
|
+
end
|
131
|
+
|
132
|
+
def raw_value
|
133
|
+
Entry.is_rails3? ? @value : @v
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
private
|
138
|
+
|
139
|
+
def collection
|
140
|
+
Mongoid.session(:default)[collection_name]
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-store
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ara Howard
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
@@ -54,10 +54,10 @@ files:
|
|
54
54
|
- Rakefile
|
55
55
|
- a.rb
|
56
56
|
- lib/active_support/cache/mongoid-store.rb
|
57
|
+
- lib/active_support/cache/mongoid_store.rb
|
57
58
|
- lib/mongoid-store.rb
|
58
59
|
- lib/mongoid-store/version.rb
|
59
60
|
- mongoid-store.gemspec
|
60
|
-
- pkg/mongoid-store-0.4.2.gem
|
61
61
|
- spec/helper.rb
|
62
62
|
- spec/mongoid-store_spec.rb
|
63
63
|
- spec/mongoid.yml
|
data/pkg/mongoid-store-0.4.2.gem
DELETED
Binary file
|