cache_pipe 0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 252433b1973bebce21b6a17a8e07fea894f7623d
4
+ data.tar.gz: fbe5c6e875b7628b784632926329166b7bd316e1
5
+ SHA512:
6
+ metadata.gz: 59c03b130ff5c054e265f36d16517b26848d5c6a7ef476b2cdffdaa538dd3c522e4ee0e1e1710894ca8fef2f4f8707a7ca1b66c20f5410f53556c8b8e85b006a
7
+ data.tar.gz: 15b6addd0ceaf7b9f12ab7149fd625620b6e59280642c5268e4b96fc30d6fc85a73a537d169fe74683dc491e3ae06a087dc6802061b763ec624803f62b311570
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Amitree
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # cache\_pipe
2
+
3
+ Provides a wrapper around a Rails cache store, allowing values to be
4
+ transformed before storage and after retrieval.
5
+
6
+ Currently it provides a single transformation, `:wrap_nil`, which allows
7
+ caching engines like [Dalli](https://github.com/mperham/dalli) to store `nil`
8
+ values.
9
+
10
+ ## Usage
11
+
12
+ Add to your Gemfile:
13
+
14
+ ```ruby
15
+ gem 'cache_pipe'
16
+ ```
17
+
18
+ Then change your Rails configuration, e.g. `config/environments/production.rb`:
19
+
20
+ ```ruby
21
+ config.cache_store = :cache_pipe, :wrap_nil, :dalli_store, { value_max_bytes: 10485760, expires_in: 86400 }
22
+ # Instead of config.cache_store = :dalli_store, { value_max_bytes: 10485760, expires_in: 86400 }
23
+ ```
24
+
25
+ ## TODO
26
+
27
+ In the future, we may want to support additional transformations. Let me know
28
+ if you have other applications that aren't covered by the current
29
+ implementation!
@@ -0,0 +1,71 @@
1
+ module ActiveSupport
2
+ module Cache
3
+ class CachePipe < SimpleDelegator
4
+ class WrappedNil ; end
5
+
6
+ NIL_VALUE = WrappedNil.new
7
+
8
+ #
9
+ # In your environment configruation:
10
+ # config.cache_store = :cache_pipe, :wrap_nil, :dalli_store, { value_max_bytes: 10485760, expires_in: 86400 }
11
+ #
12
+ # will cause a call to:
13
+ # ActiveSupport::Cache::CachePipe.new(:wrap_nil, :dalli_store, { value_max_bytes: 10485760, expires_in: 86400 })
14
+ #
15
+ def initialize transformation, *store_options
16
+ super ActiveSupport::Cache.lookup_store(*store_options)
17
+ case transformation
18
+ when :wrap_nil
19
+ @transform_read = :unwrap_nil
20
+ @transform_write = :wrap_nil
21
+ else
22
+ raise "Invalid transformation #{transformation}. Valid values are: :wrap_nil"
23
+ end
24
+ end
25
+
26
+ def fetch name, options=nil
27
+ if block_given?
28
+ transform_read __getobj__.fetch(name, options) { transform_write(yield) }
29
+ else
30
+ transform_read __getobj__.fetch(name, options)
31
+ end
32
+ end
33
+
34
+ def read name, options=nil
35
+ transform_read __getobj__.read(name, options)
36
+ end
37
+
38
+ def write name, value, options=nil
39
+ __getobj__.write name, transform_write(value), options
40
+ end
41
+
42
+ private
43
+
44
+ def transform_read value
45
+ send(@transform_read, value)
46
+ end
47
+
48
+ def transform_write value
49
+ send(@transform_write, value)
50
+ end
51
+
52
+ def wrap_nil value
53
+ case value
54
+ when nil
55
+ NIL_VALUE
56
+ else
57
+ value
58
+ end
59
+ end
60
+
61
+ def unwrap_nil(value)
62
+ case value
63
+ when NIL_VALUE
64
+ nil
65
+ else
66
+ value
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cache_pipe
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Tony Novak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ description: Provides a wrapper around a Rails cache store, allowing values to be
28
+ transformed before storage and after retrieval.
29
+ email: engineering@amitree.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - lib/active_support/cache/cache_pipe.rb
37
+ homepage: https://github.com/amitree/cache_pipe
38
+ licenses:
39
+ - MIT
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '2.0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.2.2
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Provides a wrapper around a Rails cache store, allowing values to be transformed
61
+ before storage and after retrieval.
62
+ test_files: []