sinatralli 0.0.2

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/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sinatralli.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Daryl Ginn
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,124 @@
1
+ # Sinatralli
2
+
3
+ A Sinatra Dalli Gem for page, fragment and key value caching.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sinatralli'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sinatralli
18
+
19
+ ## Dependencies
20
+
21
+ This gem uses [Dalli](https://github.com/mperham/dalli), a high performance memcached client for Ruby.
22
+
23
+ ## Getting started
24
+
25
+ If you're using a modern structure, you'll have to register Sinatralli.
26
+
27
+ ~~~ruby
28
+ class CatsAreAwesome < Sinatra::Base
29
+ register Sinatra::Sinatralli
30
+
31
+ get '/' do
32
+ erb :index
33
+ end
34
+ end
35
+ ~~~
36
+
37
+ If you're using the classic structure, you'll just have to require it.
38
+
39
+ ~~~ruby
40
+ require "sinatralli"
41
+
42
+ get '/' do
43
+ erb :index
44
+ end
45
+ ~~~
46
+
47
+ ## Default settings
48
+
49
+ There are a few settings
50
+
51
+ ~~~ruby
52
+ # Default cache server
53
+ set :cache_server, 'localhost:11211'
54
+ # Enable cache
55
+ set :cache_enabled, true
56
+ # Default environment
57
+ set :cache_environment, :production
58
+ # Default expires
59
+ set :cache_expires, 600
60
+ # Minify the page output
61
+ set :cache_minify_pages, true
62
+ # Cache pages
63
+ set :cache_pages, true
64
+ # Cache logging
65
+ set :cache_logging, true
66
+ ~~~
67
+
68
+ ## Page caching
69
+
70
+ Page caching is enabled by default, you can turn off all page caching by setting `:cache_pages` to `false`.
71
+
72
+ Cache key: `_sd_page_sitename.com/path_bar`
73
+
74
+ You can also overide default settings for a specific page.
75
+
76
+ ~~~ruby
77
+ get '/foo' do
78
+ erb :bar, cache: false, expires: 10, minify: false
79
+ end
80
+ ~~~
81
+
82
+ ## Fragment caching
83
+
84
+ Fragment caching is fairly straight forward, you can also set a custom expiry.
85
+
86
+ Cache key: `_sd_frag_sitename.com/path_bar`
87
+
88
+ ~~~erb
89
+ <% cache 'bar', expires: 300 do %>
90
+ ...
91
+ <% end %>
92
+ ~~~
93
+
94
+ ## Key value caching
95
+
96
+ Store an item simple, there are 3 parameters: __key__, __value__ & __expires__
97
+
98
+ ~~~ruby
99
+ cache_set('key', 'your value', 300)
100
+ ~~~
101
+
102
+ To retretive an item, you just specify the key:
103
+
104
+ ~~~ruby
105
+ cache_get('key')
106
+ ~~~
107
+
108
+ ## Delete cache
109
+
110
+ Delete an item by key:
111
+
112
+ ~~~ruby
113
+ cache_delete('key')
114
+ ~~~
115
+
116
+ Flush all cache:
117
+
118
+ ~~~ruby
119
+ cache_flush
120
+ ~~~
121
+
122
+ ## License
123
+
124
+ MIT License
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/.DS_Store ADDED
Binary file
Binary file
@@ -0,0 +1,81 @@
1
+ module Sinatra
2
+ module Templates
3
+ def render(engine, data, options={}, locals={}, &block)
4
+ # DRY
5
+ def get_setting(key, default)
6
+ settings.respond_to?(key) ? settings.send(key) : default
7
+ end
8
+
9
+ # Minification
10
+ def minify_output(content)
11
+ content.gsub(/^\s*\/\/.+$|<!--(.*?)-->|\t|\n|\r/, '')
12
+ end
13
+
14
+ # Cache output
15
+ def cache_output(key, content, opts)
16
+ content = minify_output(content) if settings.cache_minify_pages && opts[:minify]
17
+ expires = opts[:expires] ? opts[:expires] : settings.cache_expires
18
+ puts key
19
+ output = cache_set(key, content, expires)
20
+ end
21
+
22
+ # merge app-level options
23
+ engine_options = get_setting(engine, {})
24
+ options = engine_options.merge(options)
25
+
26
+ # extract generic options
27
+ locals = options.delete(:locals) || locals || {}
28
+ views = options.delete(:views) || settings.views || "./views"
29
+ layout = options.delete(:layout)
30
+ eat_errors = layout.nil?
31
+ layout = engine_options[:layout] if layout.nil? or layout == true
32
+ layout = @default_layout if layout.nil? or layout == true
33
+ content_type = options.delete(:content_type) || options.delete(:default_content_type)
34
+ layout_engine = options.delete(:layout_engine) || engine
35
+ scope = options.delete(:scope) || self
36
+
37
+ # set some defaults
38
+ options[:outvar] ||= '@_out_buf'
39
+ options[:default_encoding] ||= settings.default_encoding
40
+
41
+ # Cache defaults
42
+ options =
43
+ {
44
+ cache: true,
45
+ minify: true,
46
+ expires: false
47
+ }.merge!(options)
48
+
49
+ sd_do_cache = settings.cache_enabled && (settings.environment == settings.cache_environment)
50
+ sd_do_cache = sd_do_cache && settings.cache_pages && options[:cache]
51
+ sd_page_key = "_sinatralli_page_#{request.host + request.path_info.gsub(/^\//, '')}_#{data.to_s}"
52
+
53
+ # compile and render template
54
+ begin
55
+ layout_was = @default_layout
56
+ @default_layout = false
57
+ template = compile_template(engine, data, options, views)
58
+ output = template.render(scope, locals, &block)
59
+ ensure
60
+ @default_layout = layout_was
61
+ end
62
+
63
+ # render layout
64
+ if layout
65
+ # Fetch the copy
66
+ if sd_do_cache
67
+ cached = cache_get(sd_page_key)
68
+ return cached unless cached.nil?
69
+ end
70
+ options = options.merge(:views => views, :layout => false, :eat_errors => eat_errors, :scope => scope)
71
+ catch(:layout_missing) {
72
+ output = render(layout_engine, layout, options, locals) { output }
73
+ return sd_do_cache ? cache_output(sd_page_key, output, options) : output
74
+ }
75
+ end
76
+
77
+ output.extend(ContentTyped).content_type = content_type if content_type
78
+ sd_do_cache ? cache_output(sd_page_key, output, options) : output
79
+ end
80
+ end
81
+ end
data/lib/sinatralli.rb ADDED
@@ -0,0 +1,79 @@
1
+ require 'sinatra/templates'
2
+ require 'dalli'
3
+
4
+ module Sinatra
5
+ module Sinatralli
6
+ module Helpers
7
+ def cache(key, opts={}, &block)
8
+ return call_fragment(block) unless cache_enabled
9
+ key = "_sinatralli_frag_#{request.host + request.path_info.gsub(/^\//, '')}_#{key.to_s}"
10
+ if cache = cache_get(key)
11
+ @_out_buf << cache
12
+ else
13
+ cache_set(key, call_fragment(block), opts[:expires])
14
+ end
15
+ end
16
+
17
+ def cache_set(key, value, expires=nil)
18
+ return value unless cache_enabled
19
+ expires ||= settings.cache_expires
20
+ dalli.set(key, Marshal.dump(value), expires)
21
+ puts "SET: #{key}" if settings.cache_logging
22
+ value
23
+ end
24
+
25
+ def cache_get(key)
26
+ return nil unless cache_enabled
27
+ cache = dalli.get(key)
28
+ if cache
29
+ puts "GET: #{key}" if settings.cache_logging
30
+ return Marshal.load(cache)
31
+ end
32
+ end
33
+
34
+ def cache_delete(key)
35
+ dalli.delete(key) if cache_enabled
36
+ end
37
+
38
+ def cache_flush
39
+ dalli.flush if cache_enabled
40
+ end
41
+
42
+ private
43
+
44
+ def dalli
45
+ @_sd_dalli ||= Dalli::Client.new(settings.cache_server)
46
+ end
47
+
48
+ def cache_enabled
49
+ @_sd_cache_enabled ||= settings.cache_enabled && (settings.environment == settings.cache_environment)
50
+ end
51
+
52
+ def call_fragment(block)
53
+ obl = @_out_buf.length
54
+ tmp = block.call
55
+ tmp[obl..-1]
56
+ end
57
+ end
58
+
59
+ def self.registered(app)
60
+ app.helpers Sinatralli::Helpers
61
+
62
+ # Default server
63
+ app.set :cache_server, 'localhost:11211'
64
+ # Enable cache
65
+ app.set :cache_enabled, true
66
+ # Default environment
67
+ app.set :cache_environment, :production
68
+ # Default expires
69
+ app.set :cache_expires, 600
70
+ # Minify the page output
71
+ app.set :cache_minify_pages, true
72
+ # Cache pages
73
+ app.set :cache_pages, true
74
+ # Cache logging
75
+ app.set :cache_logging, true
76
+ end
77
+ end
78
+ register Sinatralli
79
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = "sinatralli"
5
+ gem.version = "0.0.2"
6
+ gem.authors = ["Daryl Ginn"]
7
+ gem.email = ["me@daryl.im"]
8
+ gem.description = %q{A Sinatra Dalli Gem for page, fragment and key value caching.}
9
+ gem.summary = %q{A Sinatra Dalli Gem for page, fragment and key value caching.}
10
+ gem.homepage = "https://github.com/daryl/Sinatralli"
11
+
12
+ gem.add_dependency 'dalli'
13
+
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = %w{lib}
18
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatralli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Daryl Ginn
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: dalli
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: A Sinatra Dalli Gem for page, fragment and key value caching.
31
+ email:
32
+ - me@daryl.im
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/.DS_Store
43
+ - lib/sinatra/.DS_Store
44
+ - lib/sinatra/templates.rb
45
+ - lib/sinatralli.rb
46
+ - sinatralli.gemspec
47
+ homepage: https://github.com/daryl/Sinatralli
48
+ licenses: []
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 1.8.23
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: A Sinatra Dalli Gem for page, fragment and key value caching.
71
+ test_files: []