perforated 0.5.0
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 +7 -0
- data/.gitignore +19 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +107 -0
- data/Rakefile +1 -0
- data/lib/perforated/cache.rb +51 -0
- data/lib/perforated/strategy/default.rb +11 -0
- data/lib/perforated/version.rb +3 -0
- data/lib/perforated.rb +19 -0
- data/perforated.gemspec +30 -0
- data/spec/perforated/cache_spec.rb +53 -0
- data/spec/perforated_spec.rb +23 -0
- data/spec/spec_helper.rb +7 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 683803eb90edeac727bca419d2f5ab5e4f4db280
|
4
|
+
data.tar.gz: 2d465ab7a114dd20db77d731003fba5933fb5428
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d3df769d45345f7d02d3653703c8da995954ea6686b51a7bc055b085861a554ab5a512d3cead7be248754c7b98f8403c8cb2da96ca654d934da93c6021a3182f
|
7
|
+
data.tar.gz: d5266599fb68f53c1d95e38f06cf7b4b000d3e98990c59de7326fca99a024a636d993246d0339f9febc224d2f46155d506776dbc1abc15face89652a53a3ab6b
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Parker Selbert
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
[](https://travis-ci.org/sorentwo/perforated)
|
2
|
+
[](https://codeclimate.com/github/sorentwo/perforated)
|
3
|
+
|
4
|
+
# Perforated
|
5
|
+
|
6
|
+
The most expensive part of serving a JSON request is converting the serialized
|
7
|
+
records into JSON. Perforated cache handles the messy task of storing and
|
8
|
+
retrieving all JSON for a particular set of models as effeciently as possible.
|
9
|
+
It achieves this several ways:
|
10
|
+
|
11
|
+
1. Storing final json output, not marshalled objects
|
12
|
+
2. Retrieving the json for as many objects at once as is possible, and then
|
13
|
+
filling in the remaining json as if it was always there. This is where the
|
14
|
+
term 'perforated' comes from.
|
15
|
+
|
16
|
+
## Configuration
|
17
|
+
|
18
|
+
Perforated is mildly configurable, mostly to allow for easy testing. However,
|
19
|
+
if you are so inclined you can customize the backing cache. The default cache
|
20
|
+
store is `ActiveSupport::Cache::MemoryStore`, which is fast but has no
|
21
|
+
persistence.
|
22
|
+
|
23
|
+
Within a Rails project the simplest option is to lock with the Rails cache.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
Perforated.configure do |config|
|
27
|
+
config.cache = Rails.cache
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
Outside of a rails project you may wish to use something like Dalli by itself:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
Perforated.configure do |config|
|
35
|
+
config.cache = ActiveSupport::Cache::DalliStore.new('localhost')
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
## Usage
|
40
|
+
|
41
|
+
Wrap any collection that you want to serialize in an a cache instance and then
|
42
|
+
call `as_json` or `to_json` on it. Not much to it!
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
perforated = Perforated::Cache.new(my_collection)
|
46
|
+
perforated.to_json
|
47
|
+
perforated.as_json
|
48
|
+
```
|
49
|
+
|
50
|
+
Any objects that have been cached will be retrieved unaltered. Any missing
|
51
|
+
objects (cache misses) will be serialized, inserted back into the collection,
|
52
|
+
and written into the cache.
|
53
|
+
|
54
|
+
### Custom Key Strategy
|
55
|
+
|
56
|
+
The default cache key strategy is to delegate back to each object to construct
|
57
|
+
its own cache key. This is useful for an object like a serializer that can
|
58
|
+
implement it's own `cache_key` method.
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
class MySerializer
|
62
|
+
attr_reader :object
|
63
|
+
|
64
|
+
def initialize(object)
|
65
|
+
@object = object
|
66
|
+
end
|
67
|
+
|
68
|
+
def cache_key
|
69
|
+
[object, scope]
|
70
|
+
end
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
However, if you are just serializing models or objects that don't have a custom
|
75
|
+
cache method you can provide a custom key caching strategy.
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
module CustomStrategy
|
79
|
+
def self.expand_cache_key(object, suffix)
|
80
|
+
[object.id, object.updated_at, suffix].join('/')
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
perforated = Perforated::Cache.new(array, CustomStrategy)
|
85
|
+
```
|
86
|
+
|
87
|
+
## Installation
|
88
|
+
|
89
|
+
Add this line to your application's Gemfile:
|
90
|
+
|
91
|
+
gem 'perforated-cache'
|
92
|
+
|
93
|
+
And then execute:
|
94
|
+
|
95
|
+
$ bundle
|
96
|
+
|
97
|
+
Or install it yourself as:
|
98
|
+
|
99
|
+
$ gem install perforated-cache
|
100
|
+
|
101
|
+
## Contributing
|
102
|
+
|
103
|
+
1. Fork it
|
104
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
105
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
106
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
107
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Perforated
|
2
|
+
class Cache
|
3
|
+
attr_reader :enumerable, :key_strategy
|
4
|
+
|
5
|
+
def initialize(enumerable, key_strategy = Perforated::Strategy::Default)
|
6
|
+
@enumerable = enumerable
|
7
|
+
@key_strategy = key_strategy
|
8
|
+
end
|
9
|
+
|
10
|
+
def as_json(*)
|
11
|
+
keyed = keyed_enumerable('as-json')
|
12
|
+
|
13
|
+
fetch_multi(*keyed.keys) do |key|
|
14
|
+
keyed[key].as_json
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_json(*)
|
19
|
+
keyed = keyed_enumerable('to-json')
|
20
|
+
|
21
|
+
json_objects = fetch_multi(*keyed.keys) do |key|
|
22
|
+
keyed[key].to_json
|
23
|
+
end
|
24
|
+
|
25
|
+
"[#{json_objects.join(',')}]"
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
# Backward compatible implementation of fetch multi.
|
31
|
+
def fetch_multi(*names)
|
32
|
+
options = {}
|
33
|
+
results = Perforated.cache.read_multi(*names, options)
|
34
|
+
|
35
|
+
names.map do |name|
|
36
|
+
results.fetch(name) do
|
37
|
+
value = yield name
|
38
|
+
Perforated.cache.write(name, value, options)
|
39
|
+
value
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def keyed_enumerable(suffix = '')
|
45
|
+
enumerable.inject({}) do |memo, object|
|
46
|
+
memo[key_strategy.expand_cache_key(object, suffix)] = object
|
47
|
+
memo
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/perforated.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'active_support/cache'
|
2
|
+
require 'json'
|
3
|
+
require 'perforated/cache'
|
4
|
+
require 'perforated/strategy/default'
|
5
|
+
require 'perforated/version'
|
6
|
+
|
7
|
+
module Perforated
|
8
|
+
def self.cache=(new_cache)
|
9
|
+
@cache = new_cache
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.cache
|
13
|
+
@cache ||= ActiveSupport::Cache::MemoryStore.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.configure
|
17
|
+
yield self
|
18
|
+
end
|
19
|
+
end
|
data/perforated.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'perforated/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'perforated'
|
8
|
+
spec.version = Perforated::VERSION
|
9
|
+
spec.authors = ['Parker Selbert']
|
10
|
+
spec.email = ['parker@sorentwo.com']
|
11
|
+
spec.homepage = 'https://github.com/sorentwo/perforated'
|
12
|
+
spec.license = 'MIT'
|
13
|
+
spec.description = %(Intellgent json collection caching)
|
14
|
+
spec.summary = <<-SUMMARY
|
15
|
+
The most expensive part of serving a JSON request is converting the
|
16
|
+
serialized records into JSON. Perforated cache handles the messy task of
|
17
|
+
storing and retrieving all JSON for a particular set of models as
|
18
|
+
effeciently as possible. It achieves this several ways:
|
19
|
+
SUMMARY
|
20
|
+
|
21
|
+
spec.files = `git ls-files`.split($/)
|
22
|
+
spec.test_files = spec.files.grep(%r{^spec/})
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
|
25
|
+
spec.add_dependency 'activesupport', '> 3.0.0'
|
26
|
+
|
27
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
28
|
+
spec.add_development_dependency 'rake', '~> 10.1.0'
|
29
|
+
spec.add_development_dependency 'rspec', '~> 2.14.0'
|
30
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'perforated'
|
2
|
+
|
3
|
+
describe Perforated::Cache do
|
4
|
+
after { Perforated.cache.clear }
|
5
|
+
|
6
|
+
Language = Struct.new(:name) do
|
7
|
+
def as_json
|
8
|
+
{ name: name }
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_json
|
12
|
+
as_json.to_json
|
13
|
+
end
|
14
|
+
|
15
|
+
def cache_key
|
16
|
+
['Language', name]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#as_json' do
|
21
|
+
it 'constructs an automatically cached serialized' do
|
22
|
+
ruby = Language.new('Ruby')
|
23
|
+
elixir = Language.new('Elixir')
|
24
|
+
array = [ruby, elixir]
|
25
|
+
cache = Perforated::Cache.new(array)
|
26
|
+
|
27
|
+
cache.as_json.should == [{ name: 'Ruby' }, { name: 'Elixir' }]
|
28
|
+
|
29
|
+
Perforated.cache.read('Language/Ruby/as-json').should == ruby.as_json
|
30
|
+
Perforated.cache.read('Language/Elixir/as-json').should == elixir.as_json
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'does not overwrite existing key values' do
|
34
|
+
erlang = Language.new('Erlang')
|
35
|
+
Perforated.cache.write('Language/Erlang/as-json', { name: 'Elixir' })
|
36
|
+
|
37
|
+
Perforated::Cache.new([erlang]).as_json
|
38
|
+
|
39
|
+
Perforated.cache.read('Language/Erlang/as-json').should == { name: 'Elixir' }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe '#to_json' do
|
44
|
+
it 'constructs a stringified json array of underlying values' do
|
45
|
+
cache = Perforated::Cache.new([Language.new('Ruby'), Language.new('Elixir')])
|
46
|
+
|
47
|
+
cache.to_json.should == %([{"name":"Ruby"},{"name":"Elixir"}])
|
48
|
+
|
49
|
+
Perforated.cache.exist?('Language/Ruby/to-json').should be_true
|
50
|
+
Perforated.cache.exist?('Language/Elixir/to-json').should be_true
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'perforated'
|
2
|
+
|
3
|
+
describe Perforated do
|
4
|
+
after { Perforated.cache = nil }
|
5
|
+
|
6
|
+
describe '#configuration' do
|
7
|
+
it 'stores an injected cache object' do
|
8
|
+
custom_cache = Object.new
|
9
|
+
|
10
|
+
Perforated.configure do |config|
|
11
|
+
config.cache = custom_cache
|
12
|
+
end
|
13
|
+
|
14
|
+
Perforated.cache.should be(custom_cache)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#cache' do
|
19
|
+
it 'falls back to ActiveSupport::Cache::MemoryStore' do
|
20
|
+
Perforated.cache.should be_instance_of(ActiveSupport::Cache::MemoryStore)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: perforated
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Parker Selbert
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>'
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>'
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 10.1.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 10.1.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.14.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.14.0
|
69
|
+
description: Intellgent json collection caching
|
70
|
+
email:
|
71
|
+
- parker@sorentwo.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .rspec
|
78
|
+
- .travis.yml
|
79
|
+
- CHANGELOG.md
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- lib/perforated.rb
|
85
|
+
- lib/perforated/cache.rb
|
86
|
+
- lib/perforated/strategy/default.rb
|
87
|
+
- lib/perforated/version.rb
|
88
|
+
- perforated.gemspec
|
89
|
+
- spec/perforated/cache_spec.rb
|
90
|
+
- spec/perforated_spec.rb
|
91
|
+
- spec/spec_helper.rb
|
92
|
+
homepage: https://github.com/sorentwo/perforated
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - '>='
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.0.0
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: 'The most expensive part of serving a JSON request is converting the serialized
|
116
|
+
records into JSON. Perforated cache handles the messy task of storing and retrieving
|
117
|
+
all JSON for a particular set of models as effeciently as possible. It achieves
|
118
|
+
this several ways:'
|
119
|
+
test_files:
|
120
|
+
- spec/perforated/cache_spec.rb
|
121
|
+
- spec/perforated_spec.rb
|
122
|
+
- spec/spec_helper.rb
|