sinatra-diskcache 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Gemfile +4 -0
- data/README.md +8 -0
- data/lib/sinatra/diskcache.rb +85 -0
- data/lib/sinatra/version.rb +5 -0
- data/sinatra-diskcache.gemspec +20 -0
- metadata +51 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.swp
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
module Sinatra
|
2
|
+
module DiskCache
|
3
|
+
module Helpers
|
4
|
+
def diskcache filename, &block
|
5
|
+
unless settings.diskcache_enabled
|
6
|
+
if block_given?
|
7
|
+
block.call path_to filename
|
8
|
+
return resulting_name filename
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
file = cache_get filename
|
13
|
+
|
14
|
+
if file
|
15
|
+
cache_log "get: #{filename}"
|
16
|
+
return resulting_name File.basename file.path
|
17
|
+
elsif block_given?
|
18
|
+
cache_log "block invoked for #{filename}"
|
19
|
+
block.call path_to filename# if block_given?
|
20
|
+
return resulting_name filename
|
21
|
+
end
|
22
|
+
rescue => e
|
23
|
+
throw e if settings.development? or settings.show_exceptions
|
24
|
+
|
25
|
+
if block_given?
|
26
|
+
block.call path_to filename
|
27
|
+
return resulting_name filename
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
def cache_log msg
|
33
|
+
puts "[sinatra-diskcache] #{msg}" if settings.diskcache_logging
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
def path_to filename
|
38
|
+
File.join settings.diskcache_path, filename
|
39
|
+
end
|
40
|
+
|
41
|
+
def resulting_name filename
|
42
|
+
return settings.diskcache_full_paths == true ? path_to( filename ) : filename
|
43
|
+
end
|
44
|
+
|
45
|
+
def cache_get filename
|
46
|
+
filepath = path_to filename
|
47
|
+
if File.exist? filepath
|
48
|
+
if ( File.ctime( filepath ) + settings.diskcache_expiry_period > Time.now ) or not settings.diskcache_expiry_enabled
|
49
|
+
begin
|
50
|
+
cache_log "found unexpirable file #{filepath}"
|
51
|
+
return File.new filepath, 'r'
|
52
|
+
rescue IOError => e
|
53
|
+
cache_log "can't open #{filename}, skipping"
|
54
|
+
end
|
55
|
+
else
|
56
|
+
cache_log "#{filename} expired, wiping"
|
57
|
+
File.delete filepath
|
58
|
+
nil
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.registered app
|
65
|
+
app.helpers DiskCache::Helpers
|
66
|
+
|
67
|
+
# defaults
|
68
|
+
app.set :diskcache_enabled, true
|
69
|
+
app.set :diskcache_logging, true if app.development?
|
70
|
+
app.set :diskcache_expiry_enabled, true
|
71
|
+
app.set :diskcache_expiry_period, 3600
|
72
|
+
app.set :diskcache_full_paths, true
|
73
|
+
app.set :diskcache_path, "./diskcache"
|
74
|
+
|
75
|
+
begin
|
76
|
+
Dir.mkdir app.settings.diskcache_path unless File.exist? app.settings.diskcache_path
|
77
|
+
rescue SystemCallError => e
|
78
|
+
cache_log 'cant create cache dir, disabling cache'
|
79
|
+
app.set :diskcache_enabled, false
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
register DiskCache
|
85
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "sinatra/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "sinatra-diskcache"
|
7
|
+
s.version = Sinatra::DiskCache::VERSION
|
8
|
+
s.authors = ["Ilia Zhirov"]
|
9
|
+
s.email = ["izhirov@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Sinatra disk cache}
|
12
|
+
s.description = %q{Allows to cache heavy-load query results on the disk}
|
13
|
+
|
14
|
+
s.rubyforge_project = "sinatra-diskcache"
|
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
|
+
end
|
metadata
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-diskcache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ilia Zhirov
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-29 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Allows to cache heavy-load query results on the disk
|
15
|
+
email:
|
16
|
+
- izhirov@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- README.md
|
24
|
+
- lib/sinatra/diskcache.rb
|
25
|
+
- lib/sinatra/version.rb
|
26
|
+
- sinatra-diskcache.gemspec
|
27
|
+
homepage: ''
|
28
|
+
licenses: []
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
requirements: []
|
46
|
+
rubyforge_project: sinatra-diskcache
|
47
|
+
rubygems_version: 1.8.17
|
48
|
+
signing_key:
|
49
|
+
specification_version: 3
|
50
|
+
summary: Sinatra disk cache
|
51
|
+
test_files: []
|