rest_cache 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rest_cache.gemspec
4
+ gemspec
5
+ rack
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module RestCache
2
+ VERSION = "0.0.1"
3
+ end
data/lib/rest_cache.rb ADDED
@@ -0,0 +1,86 @@
1
+ require "rest_cache/version"
2
+
3
+ module RestCache
4
+
5
+ class Middleware
6
+
7
+ @@cache = {}
8
+
9
+ def initialize(app, options = { :expiration_seconds => 300, :global => true })
10
+ @app = app
11
+ @options = options
12
+ end
13
+
14
+ def call(env)
15
+
16
+ @request = Rack::Request.new(env)
17
+
18
+ if @request.get?
19
+ get(env)
20
+ else
21
+ # expire all cached items for this request
22
+ expire(@request.fullpath.gsub(/\.(.*)$/, '')) # strip the format, if present
23
+
24
+ # pass along the call
25
+ @app.call(env)
26
+ end
27
+
28
+ end
29
+
30
+ def expire(path=nil)
31
+ if path
32
+ purge_path path
33
+ else
34
+ purge_all
35
+ end
36
+ end
37
+
38
+ def self.expire(path=nil)
39
+ if path
40
+ purge_path path
41
+ else
42
+ purge_all
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def get(env)
49
+
50
+ purge_expired
51
+
52
+ # build a request
53
+ key = @request.fullpath
54
+ key << @request.session[:session_id] unless @options[:global]
55
+
56
+ # return the cached result, if present
57
+ if @@cache.has_key? key
58
+ @@cache[key][:data]
59
+ else
60
+ # otherwise, cache it
61
+ @@cache[key] = { :expires_at => Time.now + @options[:expiration_seconds].seconds,
62
+ :data => @app.call(env) }
63
+ @@cache[key][:data]
64
+ end
65
+ end
66
+
67
+ def purge_path(path)
68
+ @@cache.each do |key, value|
69
+ @@cache.delete(key) if key.gsub(/\.(.*)$/, '') == path
70
+ end
71
+ end
72
+
73
+ def purge_expired
74
+ # Cleaning cache
75
+ now = Time.now
76
+
77
+ @@cache.each do |key, value|
78
+ @@cache.delete(key) if value[:expires_at] < now
79
+ end
80
+ end
81
+
82
+ def purge_all
83
+ @@cache = {}
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rest_cache/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rest_cache"
7
+ s.version = RestCache::VERSION
8
+ s.authors = ["Dave Barbalato"]
9
+ s.email = ["dbarbalato@gmail.com"]
10
+ s.homepage = ""
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 example 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
+
14
+ s.rubyforge_project = "RestCache"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rack"
23
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rest_cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dave Barbalato
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-05 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: &69360080 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *69360080
25
+ description: ! 'RestCache is Rack middleware that relies on HTTP verbs to cache requests
26
+ to your server application. Every GET request will be cached according to your initialization
27
+ parameters (see example for more information), and conversely, every PUT/POST/DELETE
28
+ request will clear your cache to ensure that updates are always reflected. RestCache
29
+ is designed to be a lightweight, simple, yet effective tool to enhance the performance
30
+ of your application. '
31
+ email:
32
+ - dbarbalato@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - Rakefile
40
+ - lib/rest_cache.rb
41
+ - lib/rest_cache/version.rb
42
+ - rest_cache.gemspec
43
+ homepage: ''
44
+ licenses: []
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project: RestCache
63
+ rubygems_version: 1.8.10
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Request caching for Rack-based web applications.
67
+ test_files: []