rest_cache 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/README +57 -0
- data/lib/rest_cache/version.rb +1 -1
- data/lib/rest_cache.rb +12 -5
- data/rest_cache.gemspec +1 -1
- metadata +6 -6
- data/Rakefile +0 -1
data/.gitignore
CHANGED
data/README
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
RestCache by Dave Barbalato
|
2
|
+
---------------------------
|
3
|
+
|
4
|
+
When To Use This Gem:
|
5
|
+
|
6
|
+
You have built a RESTful service in a Rack-based frameword, such as Rails, and you want to cache requests intelligently behind the scenes. This gem does just that - Using your parameters or some sane defaults, you can eliminate the retrieval of the same data across GET requests in your application. This caching can be performed globally, where processing power allocated to one individual can be used to the benefit of the group, or alternatively, on a per-session basis.
|
7
|
+
|
8
|
+
|
9
|
+
The Cool Part:
|
10
|
+
|
11
|
+
RestCache is powerful in that, through reliance on the conventions of RESTful verbs, any cached request will be ignored if a PUT/POST/DELETE has been seen before the expiration time. This ensures that cached data is thrown out when it has become stale.
|
12
|
+
|
13
|
+
|
14
|
+
Instructions (Rails 3.1+):
|
15
|
+
|
16
|
+
1.) Get the RestCache gem
|
17
|
+
|
18
|
+
Install from the public repository using the following line:
|
19
|
+
|
20
|
+
gem install rest_cache
|
21
|
+
|
22
|
+
|
23
|
+
2.) Install the Rack middleware
|
24
|
+
|
25
|
+
In your application.rb file, add the following line*:
|
26
|
+
|
27
|
+
config.middleware.use 'RestCache::Middleware'#, {:global => true, :expiration_seconds => 300}
|
28
|
+
|
29
|
+
This tells your Rails application to insert RestCache into the Rack middleware stack.
|
30
|
+
|
31
|
+
*Note: supplying the hash is optional (hence why it is commented out above), and is set to "{:expiration_seconds => 300, :global => true}" by default in the gem.
|
32
|
+
|
33
|
+
|
34
|
+
3.) Configure RestCache
|
35
|
+
|
36
|
+
If you want your app to cache requests on a per-session basis, modify the option hash like so:
|
37
|
+
|
38
|
+
{ :global => false, :expiration_seconds => 300 }
|
39
|
+
|
40
|
+
Doing so will result in more personalized requests, but will yield higher memory usage, and thus fewer performance benefits, which is why it is not the default :)
|
41
|
+
|
42
|
+
If you want your app to cache requests for longer than 300 seconds (5 minutes), modify the option hash like so:
|
43
|
+
|
44
|
+
{ :global => true, :expiration_seconds => 86400 }
|
45
|
+
|
46
|
+
Using this configuration, requests will be cached for a day, UNLESS the service endpoint is accessed with a PUT/POST/DELETE request.
|
47
|
+
|
48
|
+
|
49
|
+
License:
|
50
|
+
|
51
|
+
Free to use for any entity without any warranty or guarantee - have at it! Give me credit if you wish, or better yet, a job with an awesome salary. Please forward constructive criticism to dbarbalato@gmail.com, and all complaints to /dev/null.
|
52
|
+
|
53
|
+
|
54
|
+
Comments:
|
55
|
+
|
56
|
+
Thanks for using the gem, I hope you find it useful!
|
57
|
+
|
data/lib/rest_cache/version.rb
CHANGED
data/lib/rest_cache.rb
CHANGED
@@ -1,12 +1,20 @@
|
|
1
1
|
require "rest_cache/version"
|
2
2
|
|
3
3
|
module RestCache
|
4
|
-
|
4
|
+
|
5
5
|
class Middleware
|
6
6
|
|
7
|
+
EXPIRATION_SECONDS_DEFAULT = 300
|
8
|
+
GLOBAL_CACHING_DEFAULT = true
|
9
|
+
|
7
10
|
@@cache = {}
|
8
11
|
|
9
|
-
def initialize(app, options
|
12
|
+
def initialize(app, options)
|
13
|
+
|
14
|
+
# assign defaults to potentially missing options
|
15
|
+
options[:expiration_seconds] ||= EXPIRATION_SECONDS_DEFAULT
|
16
|
+
options[:global] &&= GLOBAL_CACHING_DEFAULT
|
17
|
+
|
10
18
|
@app = app
|
11
19
|
@options = options
|
12
20
|
end
|
@@ -50,8 +58,7 @@ module RestCache
|
|
50
58
|
purge_expired
|
51
59
|
|
52
60
|
# build a request
|
53
|
-
key = @request.fullpath
|
54
|
-
key << @request.session[:session_id] unless @options[:global]
|
61
|
+
key = '' << (@request.session[:session_id] unless @options[:global]) << @request.fullpath
|
55
62
|
|
56
63
|
# return the cached result, if present
|
57
64
|
if @@cache.has_key? key
|
@@ -66,7 +73,7 @@ module RestCache
|
|
66
73
|
|
67
74
|
def purge_path(path)
|
68
75
|
@@cache.each do |key, value|
|
69
|
-
@@cache.delete(key) if key.gsub(/\.(.*)$/, '')
|
76
|
+
@@cache.delete(key) if key.gsub(/\.(.*)$/, '').end_with? path
|
70
77
|
end
|
71
78
|
end
|
72
79
|
|
data/rest_cache.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.email = ["dbarbalato@gmail.com"]
|
10
10
|
s.homepage = ""
|
11
11
|
s.summary = %q{Request caching for Rack-based web applications.}
|
12
|
-
s.description = %q{RestCache is Rack middleware that relies on HTTP verbs to cache requests to your server application. Every GET request will be cached according to your initialization parameters (see
|
12
|
+
s.description = %q{RestCache is Rack middleware that relies on HTTP verbs to cache requests to your server application. Every GET request will be cached according to your initialization parameters (see README for more information), and conversely, every PUT/POST/DELETE request will clear your cache to ensure that updates are always reflected. RestCache is designed to be a lightweight, simple, yet effective tool to enhance the performance of your application. }
|
13
13
|
|
14
14
|
s.rubyforge_project = "RestCache"
|
15
15
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rest_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-05-
|
12
|
+
date: 2012-05-06 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
16
|
-
requirement: &
|
16
|
+
requirement: &82828900 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *82828900
|
25
25
|
description: ! 'RestCache is Rack middleware that relies on HTTP verbs to cache requests
|
26
26
|
to your server application. Every GET request will be cached according to your initialization
|
27
|
-
parameters (see
|
27
|
+
parameters (see README for more information), and conversely, every PUT/POST/DELETE
|
28
28
|
request will clear your cache to ensure that updates are always reflected. RestCache
|
29
29
|
is designed to be a lightweight, simple, yet effective tool to enhance the performance
|
30
30
|
of your application. '
|
@@ -36,7 +36,7 @@ extra_rdoc_files: []
|
|
36
36
|
files:
|
37
37
|
- .gitignore
|
38
38
|
- Gemfile
|
39
|
-
-
|
39
|
+
- README
|
40
40
|
- lib/rest_cache.rb
|
41
41
|
- lib/rest_cache/version.rb
|
42
42
|
- rest_cache.gemspec
|
data/Rakefile
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require "bundler/gem_tasks"
|