rack-per_request_cache 1.0.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.
- data/README.md +23 -0
- data/lib/rack/per_request_cache.rb +33 -0
- data/spec/per_request_cache_spec.rb +35 -0
- metadata +70 -0
data/README.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# Rack::PerRequestCache
|
2
|
+
|
3
|
+
Simple module for caching arbitrary objects / constructs during a request.
|
4
|
+
|
5
|
+
## Usage (Sinatra, Padrino, ...)
|
6
|
+
|
7
|
+
require 'rack/per_request_cache'
|
8
|
+
use Rack::PerRequestCache
|
9
|
+
|
10
|
+
## Rails
|
11
|
+
|
12
|
+
# Gemfile
|
13
|
+
gem 'rack-per_request_cache', require: 'rack/per_request_cache'
|
14
|
+
|
15
|
+
# config/application.rb
|
16
|
+
config.use 'Rack::PerRequestCache'
|
17
|
+
|
18
|
+
## Caveats
|
19
|
+
|
20
|
+
If Rack::PerRequestCache.clear_cache is not called, then no caching is performed.
|
21
|
+
When hooked up as a middleware (see above), then the cache is enable dy default during
|
22
|
+
the request lifecycle.
|
23
|
+
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Rack
|
2
|
+
class PerRequestCache
|
3
|
+
|
4
|
+
def initialize(app)
|
5
|
+
@app = app
|
6
|
+
end
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
self.class.initialize_cache
|
10
|
+
@app.call env
|
11
|
+
ensure
|
12
|
+
self.class.clear_cache
|
13
|
+
end
|
14
|
+
|
15
|
+
# Cache management and accessor methods
|
16
|
+
class << self
|
17
|
+
def initialize_cache
|
18
|
+
@cache = {}
|
19
|
+
end
|
20
|
+
|
21
|
+
def clear_cache
|
22
|
+
@cache = nil
|
23
|
+
end
|
24
|
+
|
25
|
+
def cached(key, &block)
|
26
|
+
return yield if @cache.nil?
|
27
|
+
return @cache[key] if @cache.has_key?(key)
|
28
|
+
|
29
|
+
@cache[key] = yield
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe PerRequestCache do
|
4
|
+
before do
|
5
|
+
PerRequestCache.clear_cache
|
6
|
+
@executor = mock("Executor", :stub! => "something")
|
7
|
+
end
|
8
|
+
|
9
|
+
it "doesn't not store the result if the cache wasn't initialized" do
|
10
|
+
@executor.should_receive(:something).twice.and_return 23
|
11
|
+
|
12
|
+
PerRequestCache.cached(:abc) { @executor.something }.should == 23
|
13
|
+
PerRequestCache.cached(:abc) { @executor.something }.should == 23
|
14
|
+
end
|
15
|
+
|
16
|
+
context "when cache enabled" do
|
17
|
+
before do
|
18
|
+
PerRequestCache.initialize_cache
|
19
|
+
end
|
20
|
+
|
21
|
+
it "uses the stored result if it exists" do
|
22
|
+
@executor.should_receive(:something).once.and_return 23
|
23
|
+
|
24
|
+
PerRequestCache.cached(:abc) { @executor.something }.should == 23
|
25
|
+
PerRequestCache.cached(:abc) { @executor.something }.should == 23
|
26
|
+
end
|
27
|
+
|
28
|
+
it "uses the stored result if it's nil" do
|
29
|
+
@executor.should_receive(:something).once.and_return nil
|
30
|
+
|
31
|
+
PerRequestCache.cached(:abc) { @executor.something }.should be_nil
|
32
|
+
PerRequestCache.cached(:abc) { @executor.something }.should be_nil
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-per_request_cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tom Sommer
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-16 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rack
|
16
|
+
requirement: &70254076172460 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70254076172460
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70254076171320 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '2.0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70254076171320
|
36
|
+
description: Module for caching arbitrary objects / constructs during a request.
|
37
|
+
email: tom@trikeapps.com
|
38
|
+
executables: []
|
39
|
+
extensions: []
|
40
|
+
extra_rdoc_files: []
|
41
|
+
files:
|
42
|
+
- README.md
|
43
|
+
- lib/rack/per_request_cache.rb
|
44
|
+
- spec/per_request_cache_spec.rb
|
45
|
+
homepage: https://github.com/tricycle/rack-per_request_cache
|
46
|
+
licenses: []
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.8.10
|
66
|
+
signing_key:
|
67
|
+
specification_version: 2
|
68
|
+
summary: Module for caching arbitrary objects / constructs during a request.
|
69
|
+
test_files:
|
70
|
+
- spec/per_request_cache_spec.rb
|