mongo_cache_store 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,159 @@
1
+ = MongoCacheStore
2
+
3
+ A MongoDB cache store for ActiveSupport 3
4
+
5
+ == Description
6
+
7
+ MongoCacheStore uses pluggable backends to expose MongoDB
8
+ as a cache store to ActiveSupport applications. Each backend
9
+ allows the application to customize how the cache operates.
10
+ Support is available for standard, capped and TTL collections.
11
+
12
+ *WARNING* This gem is in the early stages of development and
13
+ should be treated as such. Checking the version of the gem will
14
+ help with what could be many changes to the backends, options, etc...
15
+
16
+ While in beta, the major version will always be 0. The minor version
17
+ will be increased for anything that breaks the current API. The patch
18
+ version will be increased for all changes within a minor revision that
19
+ add to or fix the current release without changing how the gem is
20
+ configured or used.
21
+
22
+ == Initialize the cache
23
+
24
+ === Usage
25
+
26
+ config.cache_store = :mongo_cache_store, :TTL, :db => Mongo::DB.new('db_name',Mongo::Connection.new)
27
+ config.cache_store = :mongo_cache_store, :Standard, :db_name => 'db_name', :connection => Mongo::Connection.new, :serialize => :on_fail
28
+
29
+ === Attributes
30
+
31
+ [+backend+ - Symbol representing the backend the cache should use]
32
+ :TTL:: ActiveSupport::Cache::MongoCacheStore::Backend::TTL
33
+ :Standard:: ActiveSupport::Cache::MongoCacheStore::Backend::Standard
34
+ :MultiTTL:: ActiveSupport::Cache::MongoCacheStore::Backend::MultiTTL
35
+ :Capped:: ActiveSupport::Cache::MongoCacheStore::Backend::Capped
36
+
37
+ [+options+ - Options for ActiveSupport::Cache and the backend]
38
+ Core options are listed here. See each backend for a list of additional optons.
39
+ [+:db+ - A Mongo::DB instance.]
40
+ [+:db_name+ - Name of database to create if no 'db' is given.]
41
+ [+:connection+ - A Mongo::Connection instance. Only used if no 'db' is given.]
42
+ [+:serialize+ - *:always* | :on_fail | :never]
43
+ [+:always+ - (default) - Serialize all entries]
44
+ *NOTE* Without serialization class structures and instances that cannot
45
+ be converted to a native MongoDB type will not be stored. Also,
46
+ without serialization MongoDB converts all symbols to strings.
47
+ Therefore a hash with symbols as keys will have strings as keys when read.
48
+ [+:on_fail+ - Serialize if native format fails]
49
+ Try to save the entry in a native MongoDB format. If that fails,
50
+ then serialize the entry.
51
+ [+:never+ - Never serialize]
52
+ Only save the entry if it can be saved natively by MongoDB.
53
+
54
+
55
+ == Backends
56
+
57
+ == TTL
58
+
59
+ TTL backend for MongoCacheStore
60
+
61
+ === Description
62
+
63
+ Entries are kept in a namespaced TTL collection that will
64
+ automatically flush any entries as they pass their expiration
65
+ time. This keeps the size of the cache in check over time.
66
+ <b>Requires MongoDB 2.0 or higher</b>
67
+
68
+ === Additional Options
69
+
70
+ No additional options at this time
71
+
72
+
73
+ == Standard
74
+ Standard backend for MongoCacheStore
75
+
76
+ === Description
77
+
78
+ Entreis are kept in a namespaced MongoDB collection. In a standard
79
+ collection entries are only flushed from the collection with an
80
+ explicit delete call or if auto_flush is enabled. If auto_flush is
81
+ enabled the cache will flush all expired entries when auto\_flush\_threshold
82
+ is reached. The threshold is based on a set number of cache instance writes.
83
+
84
+ === Additional Options
85
+
86
+ The following options can be added to a MongoCacheStore constructor
87
+ [+options+ - Standard backend options]
88
+ To see a list of core options see MongoCacheStore
89
+ [+:auto_flush+ - *true* | false]
90
+ Default: true
91
+
92
+ If auto_flush is enabled the cache will flush all
93
+ expired entries when auto\_flush\_threshold
94
+ is reached.
95
+ [+:auto_flush_threshold+ - *10_000*]
96
+ Default: 10_000
97
+
98
+ A number representing the number of writes the when the cache
99
+ should preform before flushing expired entries.
100
+
101
+
102
+ == MultiTTL
103
+
104
+ MultiTTL backend for MongoCacheStore
105
+
106
+ === Description
107
+
108
+ Entries are stored in multiple namespaced TTL collections.
109
+ A namespaced TTL collection is created for each unique expiration time.
110
+ For example all entries with an expiration time of 300 seconds will be
111
+ kept in the same collection while entries with a 900 second expiration
112
+ time will be kept in another. This requires the use of a *key index*
113
+ collection that keeps track of which TTL collection a entry resides in.
114
+
115
+ ==== Downsides
116
+ * Cache set operations require 2 MongoDB write calls.
117
+ One for the key index, one for the TTL collection.
118
+ (unless *use_index* is false, see below)
119
+ * Cache read operations will require 1 or 2 MongoDB calls
120
+ depending on whether the 'expires_in' option is set for the read.
121
+
122
+ ==== Benefits (future)
123
+ * Ability to flush cache based on expire time (TODO)
124
+
125
+
126
+ === Additional Options
127
+
128
+ The following options can be added to a MongoCacheStore constructor
129
+
130
+ [+options+ - MultiTTL backend options]
131
+ To see a list of core options see MongoCacheStore
132
+ [+:use_index+ - *true* | false]
133
+ Default: true
134
+
135
+ This should only be set to *false* if all fetch and/or read
136
+ operations are passed the *:expires_in* option. If so, this
137
+ will eliminate the need for the key index collection and only
138
+ one write and one read operation is necessary.
139
+
140
+
141
+ == Capped
142
+
143
+ *Experimental*
144
+
145
+ Capped backend for MongoCacheStore
146
+
147
+ === Description
148
+ *Experimental* do not use... yet.
149
+
150
+ This should only be used if limiting the size of the cache
151
+ is of great concern. Entries are flushed from the cache on
152
+ a FIFO basis, regardless of the entries expiration time.
153
+ Delete operations set an entry to expired, but it will not
154
+ be flushed until it is automatically removed by MongoDB.
155
+
156
+ === Options
157
+
158
+ TODO
159
+
@@ -8,6 +8,36 @@ module ActiveSupport
8
8
 
9
9
  class MongoCacheStore < Store
10
10
 
11
+ # Initialize the cache
12
+ #
13
+ # === Attributes
14
+ #
15
+ # [+backend+ - Symbol representing the backend the cache should use]
16
+ # :TTL | :Standard | :MultiTTL
17
+ #
18
+ # [+options+ - Options for ActiveSupport::Cache and the backend]
19
+ # Core options are listed here. See each backend for a list of additional optons.
20
+ # [+:db+ - A Mongo::DB instance.]
21
+ # [+:db_name+ - Name of database to create if no 'db' is given.]
22
+ # [+:connection+ - A Mongo::Connection instance. Only used if no 'db' is given.]
23
+ # [+:serialize+ - *:always* | :on_fail | :never]
24
+ # [+:always+ - (default) - Serialize all entries]
25
+ # *NOTE* Without serialization class structures and instances that cannot
26
+ # be converted to a native MongoDB type will not be stored. Also,
27
+ # without serialization MongoDB converts all symbols to strings.
28
+ # Therefore a hash with symbols as keys will have strings as keys when read.
29
+ #
30
+ # [+:on_fail+ - Serialize if native format fails]
31
+ # Try to save the entry in a native MongoDB format. If that fails,
32
+ # then serialize the entry.
33
+ # [+:never+ - Never serialize]
34
+ # Only save the entry if it can be saved natively by MongoDB.
35
+ #
36
+ # === Examples
37
+ # @store = ActiveSupport::Cache::MongoCacheStore.new(:TTL, :db => Mongo::DB.new('db_name',Mongo::Connection.new))
38
+ #
39
+ # @store = ActiveSupport::Cache::MongoCacheStore.new(:Standard, :db_name => 'db_name', :connection => Mongo::Connection.new)
40
+
11
41
  def initialize (backend=:Standard, options = {})
12
42
 
13
43
  options = {
@@ -6,10 +6,25 @@ module ActiveSupport
6
6
  module Cache
7
7
  class MongoCacheStore
8
8
  module Backend
9
- # MongoCacheStoreBackend for capped collections
9
+ # == Capped
10
+ #
11
+ # *Experimental*
12
+ #
13
+ # Capped backend for MongoCacheStore
14
+ #
15
+ # === Description
16
+ # *Experimental* do not use... yet.
17
+ #
18
+ # This should only be used if limiting the size of the cache
19
+ # is of great concern. Entries are flushed from the cache on
20
+ # a FIFO basis, regardless of the entries expiration time.
21
+ # Delete operations set an entry to expired, but it will not
22
+ # be flushed until it is automatically removed by MongoDB.
10
23
  #
11
- # == Capped Collections
24
+ # === Options
12
25
  #
26
+ # TODO
27
+ #
13
28
  module Capped
14
29
  include Base
15
30
 
@@ -7,9 +7,43 @@ module ActiveSupport
7
7
  module Cache
8
8
  class MongoCacheStore
9
9
  module Backend
10
- # MongoCacheStoreBackend for TTL collections
10
+ # == MultiTTL
11
+ #
12
+ # MultiTTL backend for MongoCacheStore
11
13
  #
12
- # == Time To Live (TTL) collections
14
+ # === Description
15
+ #
16
+ # Entries are stored in multiple namespaced TTL collections.
17
+ # A namespaced TTL collection is created for each unique expiration time.
18
+ # For example all entries with an expiration time of 300 seconds will be
19
+ # kept in the same collection while entries with a 900 second expiration
20
+ # time will be kept in another. This requires the use of a *key index*
21
+ # collection that keeps track of which TTL collection a entry resides in.
22
+ #
23
+ # ==== Downsides
24
+ # * Cache set operations require 2 MongoDB write calls.
25
+ # One for the key index, one for the TTL collection.
26
+ # (unless *use_index* is false, see below)
27
+ # * Cache read operations will require 1 or 2 MongoDB calls
28
+ # depending on whether the 'expires_in' option is set for the read.
29
+ #
30
+ # ==== Benefits (future)
31
+ # * Ability to flush cache based on expire time (TODO)
32
+ #
33
+ #
34
+ # === Additional Options
35
+ #
36
+ # The following options can be added to a MongoCacheStore constructor
37
+ #
38
+ # [+options+ - MultiTTL backend options]
39
+ # To see a list of core options see MongoCacheStore
40
+ # [+:use_index+ - *true* | false]
41
+ # Default: true
42
+ #
43
+ # This should only be set to *false* if all fetch and/or read
44
+ # operations are passed the *:expires_in* option. If so, this
45
+ # will eliminate the need for the key index collection and only
46
+ # one write and one read operation is necessary.
13
47
  #
14
48
  module MultiTTL
15
49
  include Base
@@ -7,9 +7,35 @@ module ActiveSupport
7
7
  module Cache
8
8
  class MongoCacheStore
9
9
  module Backend
10
- # MongoCacheStoreBackend for standard collections
10
+ # == Standard
11
+ #
12
+ # Standard backend for MongoCacheStore
13
+ #
14
+ # === Description
15
+ #
16
+ # Entreis are kept in a namespaced MongoDB collection. In a standard
17
+ # collection entries are only flushed from the collection with an
18
+ # explicit delete call or if auto_flush is enabled. If auto_flush is
19
+ # enabled the cache will flush all expired entries when auto\_flush\_threshold
20
+ # is reached. The threshold is based on a set number of cache instance writes.
21
+ #
22
+ # === Additional Options
11
23
  #
12
- # == Standard collections
24
+ # The following options can be added to a MongoCacheStore constructor
25
+ #
26
+ # [+options+ - Standard backend options]
27
+ # To see a list of core options see MongoCacheStore
28
+ # [+:auto_flush+ - *true* | false]
29
+ # Default: true
30
+ #
31
+ # If auto_flush is enabled the cache will flush all
32
+ # expired entries when auto\_flush\_threshold
33
+ # is reached.
34
+ # [+:auto_flush_threshold+ - *10_000*]
35
+ # Default: 10_000
36
+ #
37
+ # A number representing the number of writes the when the cache
38
+ # should preform before flushing expired entries.
13
39
  #
14
40
  module Standard
15
41
  include Base
@@ -6,10 +6,22 @@ module ActiveSupport
6
6
  module Cache
7
7
  class MongoCacheStore
8
8
  module Backend
9
- # MongoCacheStoreBackend for OneTTL collections
10
- #
11
- # == OneTTL collections
9
+ # == TTL
12
10
  #
11
+ # TTL backend for MongoCacheStore
12
+ #
13
+ # === Description
14
+ #
15
+ # Entries are kept in a namespaced TTL collection that will
16
+ # automatically flush any entries as they pass their expiration
17
+ # time. This keeps the size of the cache in check over time.
18
+ #
19
+ # <b>Requires MongoDB 2.0 or higher</b>
20
+ #
21
+ # === Additional Options
22
+ #
23
+ # No additional options at this time
24
+ #
13
25
  module TTL
14
26
  include Base
15
27
 
@@ -1,4 +1,4 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module MongoCacheStore
3
- VERSION = "0.2.0"
3
+ VERSION = "0.2.1"
4
4
  end
@@ -18,6 +18,6 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ["lib"]
19
19
 
20
20
  gem.add_dependency 'mongo'
21
- gem.add_dependency 'activesupport'
21
+ gem.add_dependency 'activesupport', '~>3'
22
22
  gem.add_development_dependency 'rspec'
23
23
  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.0
4
+ version: 0.2.1
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-13 00:00:00.000000000 Z
12
+ date: 2013-01-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongo
@@ -32,17 +32,17 @@ dependencies:
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
- - - ! '>='
35
+ - - ~>
36
36
  - !ruby/object:Gem::Version
37
- version: '0'
37
+ version: '3'
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - ! '>='
43
+ - - ~>
44
44
  - !ruby/object:Gem::Version
45
- version: '0'
45
+ version: '3'
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: rspec
48
48
  requirement: !ruby/object:Gem::Requirement
@@ -69,7 +69,7 @@ files:
69
69
  - .gitignore
70
70
  - Gemfile
71
71
  - LICENSE.txt
72
- - README.md
72
+ - README.rdoc
73
73
  - Rakefile
74
74
  - lib/active_support/cache/mongo_cache_store.rb
75
75
  - lib/active_support/cache/mongo_cache_store/backend/base.rb
data/README.md DELETED
@@ -1,102 +0,0 @@
1
- # MongoCacheStore
2
-
3
- A MongoDB cache store for ActiveSupport 3
4
-
5
- ## Description
6
-
7
- MongoCacheStore uses pluggable backends to expose MongoDB as a cache store to ActiveSupport applications. Each backend allows the application to customize how the cache operates. Support is available for standard, capped and TTL collections.
8
-
9
- **WARNING** This gem is in the early stages of development and should be treated as such. Checking the version of the gem will help with what could be many changes to the backends, options, etc...
10
-
11
- While in beta, the major version will always be 0. The minor version will be increased for anything that breaks the current API. The patch version will be increased for all changes within a minor revision that add to or fix the current release without changing how the gem is configured or used.
12
-
13
-
14
- ## Backends
15
-
16
- A Backend controls how MongoDB collections are used and manipulated as a cache store.
17
-
18
- MongoCacheStore ships with 4 backends, TTL, Capped, Standard and MultiTTL.
19
-
20
- Planned: GridFS
21
- * For very large entries. Ex: Generate a large report (PDF, DOC, etc...), keep it around for a limited amount of time and have it automatically flush upon expiration.
22
-
23
-
24
- The major difference between each backend involves how entries are flushed. The core driver will always respect ActiveSupport's *:expires_in* parameter for hits and misses whether the entry has actually been flushed from the backend or not.
25
-
26
- #### Core Options
27
- From ActiveSupport
28
- * :namespace
29
- * :expires_in
30
-
31
- For MongoCacheStore
32
- * :db - A Mongo::DB instance
33
- * :db_name - Name of database to be created. Only used if *db* is not set
34
- * :connection - A Mongo::Connection. Only used if *db* is not set. Defaults to Mongo::Connection.new()
35
- * serialize: (*:always*, :on\_fail, :never)
36
- * :always (default) - Serialize all entries
37
- * :on\_fail - Try to save the entry in a native MongoDB format. If that fails, then serialize the entry.
38
- * :never - only save the entry if it can be saved natively by MongoDB.
39
- * *NOTE:* Without serialization class structures and instances that cannot be converted to a native MongoDB type will not be stored. Also, without serialization MongoDB converts all symbols to strings. Therefore a hash with symbols as keys will have strings as keys when read.
40
-
41
-
42
- ### TTL
43
- Entries are kept in a namespaced TTL collection that will automatically flush any entries as they pass their expiration time. This keeps the size of the cache in check over time.
44
-
45
- * *Requires MongoDB 2.0 or higher*
46
-
47
- #### Options
48
- No Extra options
49
-
50
- ### Standard
51
- Entreis are kept in a namespaced MongoDB collection. In a standard collection entries are only flushed from the collection with an explicit delete call or if auto_flush is enabled. If auto_flush is enabled the cache will flush all expired entries when auto\_flush\_threshold is reached. The threshold is based on a set number of cache instance writes.
52
-
53
- #### Options
54
- * auto_flush: (*true*|false)
55
- * auto\_flush\_threshold: *10_000*
56
-
57
-
58
- ### Capped (TODO)
59
- This should only be used if limiting the size of the cache is the greatest concern. Entries are flushed from the cache on a FIFO basis, regardless of the entries expiration time. Delete operations set an entry to expired, but it will not be flushed until it is automatically removed by MongoDB.
60
-
61
-
62
- ### MultiTTL
63
- Entries are stored in multiple namespaced TTL collections. A namespaced TTL collection is created for each unique expiration time. For example all entries with an expiration time of 300 seconds will be kept in the same collection while entries with a 900 second expiration time will be kept in another. This requires the use of a *key index* collection that keeps track of which TTL collection a entry resides in.
64
-
65
- #### Downsides
66
- * Cache set operations require 2 MongoDB write calls. One for the key index, one for the TTL collection. (unless *use_index* is false, see below)
67
- * Cache read operations will require 1 or 2 MongoDB calls depending on whether the 'expires_in' option is set for the read.
68
-
69
- #### Benefits
70
- * Ability to flush cache based on expire time (TODO)
71
-
72
- #### Options
73
- use\_index: (*true*, false)
74
- * This should only be set to *false* if all fetch and/or read operations are passed the *:expires_in* option. If so, this will eliminate the need for the key index collection and only one write and one read operation is necessary.
75
-
76
-
77
-
78
- ## Installation
79
-
80
- Add this line to your application's Gemfile:
81
-
82
- gem 'mongo_cache_store'
83
-
84
- And then execute:
85
-
86
- $ bundle
87
-
88
- Or install it yourself as:
89
-
90
- $ gem install mongo_cache_store
91
-
92
- ## Usage
93
-
94
-
95
-
96
- ## Contributing
97
-
98
- 1. Fork it
99
- 2. Create your feature branch (`git checkout -b my-new-feature`)
100
- 3. Commit your changes (`git commit -am 'Add some feature'`)
101
- 4. Push to the branch (`git push origin my-new-feature`)
102
- 5. Create new Pull Request