sinatra_fragment_cache 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/sinatra_fragment_cache.rb +1 -0
- data/lib/sinatra_fragment_cache/fragment_cache.rb +51 -0
- metadata +73 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8f785d8f8aa546740e4b2a458046fae6f2ac6e30
|
4
|
+
data.tar.gz: 1422f57e027fbfe29ea96eb8145cb8e44388022e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a96b4623a7301eb0bfa268373af5a29c6926ff61c0d18300889b38015d2657ab743d715ce59ab645527c538db12ececddf0846fdbfc293ad47c793cdfa530ed6
|
7
|
+
data.tar.gz: a647cc48910607fa935e2cc493c1e8c06030dc2f27272e0ef4e95cae9bfb89030127b4901f1a89351805cacf327ac51876dc4aef62071975ab432f1fba90fd76
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'sinatra_fragment_cache/fragment_cache'
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Sinatra
|
2
|
+
module FragmentCache
|
3
|
+
attr_accessor :path, :options, :cache_path
|
4
|
+
|
5
|
+
def fragment_cache(path, options = {}, &block)
|
6
|
+
@path = path
|
7
|
+
@options = options
|
8
|
+
file_name = options[:file_name] || 'index.cache'
|
9
|
+
@cache_path = "#{ settings.fragment_cache_output_dir }/#{ path }/#{ file_name }"
|
10
|
+
|
11
|
+
return block.call unless settings.fragment_cache_enabled
|
12
|
+
|
13
|
+
@_out_buf = []
|
14
|
+
if cache = read_fragment
|
15
|
+
@_out_buf << cache
|
16
|
+
else
|
17
|
+
pos = @_out_buf.length
|
18
|
+
tmp = block.call
|
19
|
+
write_fragment tmp[pos..-1]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def read_fragment
|
24
|
+
now = Time.now
|
25
|
+
if File.file?(cache_path)
|
26
|
+
if options[:expires_in]
|
27
|
+
(current_age = (now - File.mtime(cache_path)).to_i / 60)
|
28
|
+
return false if (current_age > options[:expires_in])
|
29
|
+
end
|
30
|
+
return File.read(cache_path)
|
31
|
+
end
|
32
|
+
false
|
33
|
+
end
|
34
|
+
|
35
|
+
def write_fragment(buffer)
|
36
|
+
FileUtils.mkdir_p "#{ settings.fragment_cache_output_dir }/#{ path }"
|
37
|
+
file = File.new(cache_path, 'w+')
|
38
|
+
file.write(buffer)
|
39
|
+
file.close
|
40
|
+
buffer
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.registered(app)
|
44
|
+
app.extend Sinatra
|
45
|
+
app.helpers FragmentCache
|
46
|
+
|
47
|
+
app.set :fragment_cache_enabled, true
|
48
|
+
app.set :fragment_cache_output_dir, lambda { "#{ app.root }/tmp/cache" }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra_fragment_cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adriano Tadao Sabadini Matsumoto
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sinatra
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.4'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.5'
|
41
|
+
description: Creating a fragment cache with sinatra!
|
42
|
+
email: 'drianotadao@gmail.com '
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- lib/sinatra_fragment_cache.rb
|
48
|
+
- lib/sinatra_fragment_cache/fragment_cache.rb
|
49
|
+
homepage: https://github.com/adrianotadao/sinatra-fragment-cache
|
50
|
+
licenses:
|
51
|
+
- MIT
|
52
|
+
metadata: {}
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
requirements: []
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 2.2.2
|
70
|
+
signing_key:
|
71
|
+
specification_version: 4
|
72
|
+
summary: Sinatra Fragment Cache!
|
73
|
+
test_files: []
|