salli 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in salli.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 daryl
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.
@@ -0,0 +1,104 @@
1
+ # Salli
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 'salli'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install salli
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 Salli.
26
+
27
+ ~~~ruby
28
+ class CatsAreAwesome < Sinatra::Base
29
+ register Sinatra::Salli
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 "salli"
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 server
53
+ app.set :cache_server, 'localhost:11211'
54
+ # Cache on/off
55
+ app.set :cache, true
56
+ # Expiration (seconds)
57
+ app.set :cache_expires, 600
58
+ # Cache logging (development)
59
+ app.set :cache_logging, true
60
+ # Default cache environment
61
+ app.set :cache_environment, :development
62
+ ~~~
63
+
64
+ ## Fragment caching
65
+
66
+ Fragment caching is fairly straight forward, you can also set a custom expiry.
67
+
68
+ ~~~erb
69
+ <% cache 'bar', expires: 300 do %>
70
+ ...
71
+ <% end %>
72
+ ~~~
73
+
74
+ ## Key value caching
75
+
76
+ Store an item simple, there are 3 parameters: __key__, __value__ & __expires__
77
+
78
+ ~~~ruby
79
+ cache_set(:key, 'your value', 300)
80
+ ~~~
81
+
82
+ To retretive an item, you just specify the key:
83
+
84
+ ~~~ruby
85
+ cache_get(:key)
86
+ ~~~
87
+
88
+ ## Delete cache
89
+
90
+ Delete an item by key:
91
+
92
+ ~~~ruby
93
+ cache_delete(:key)
94
+ ~~~
95
+
96
+ Flush all cache:
97
+
98
+ ~~~ruby
99
+ cache_flush
100
+ ~~~
101
+
102
+ ## License
103
+
104
+ MIT License
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,30 @@
1
+ require "salli/version"
2
+ require "salli/helpers"
3
+ # Salvador
4
+ require 'dalli'
5
+
6
+ module Sinatra
7
+ module Salli
8
+ def self.registered(app)
9
+ app.helpers Salli::Helpers
10
+ # Default server
11
+ app.set :cache_server, 'localhost:11211'
12
+ # Cache on/off
13
+ app.set :cache, true
14
+ # Expiration (seconds)
15
+ app.set :cache_expires, 600
16
+ # Cache logging (development)
17
+ app.set :cache_logging, true
18
+ # Default cache environment
19
+ app.set :cache_environment, :development
20
+ end
21
+
22
+ # Development logging
23
+ def self.log(meth, key)
24
+ puts (meth =~ /g/ ? 'Served: ' : 'Stored: ') << key
25
+ end
26
+ end
27
+
28
+ # Classic
29
+ register Salli
30
+ end
@@ -0,0 +1,102 @@
1
+ module Sinatra
2
+ module Salli
3
+ module Helpers
4
+ ##
5
+ # Fragment cache
6
+ # @api public
7
+ def cache(nade, expires=nil, &maybe)
8
+ raise ArgumentError, 'No block was used' unless block_given?
9
+ return call_me(maybe) unless all_systems_go?
10
+ # Push to output buffer
11
+ if value = cache_get(nade)
12
+ buff? ? @_out_buf << value : value
13
+ else
14
+ cache_set(nade, call_me(maybe), expires)
15
+ end
16
+ end
17
+
18
+ ##
19
+ # Key / value
20
+ # @api public
21
+ def cache_set(key, value, expires=nil)
22
+ return value unless all_systems_go?
23
+ key = keygen(key.to_s)
24
+ expires ||= settings.cache_expires
25
+ client.set(key, Marshal.dump(value), expires)
26
+ Salli.log('set', key) if settings.cache_logging
27
+ value
28
+ end
29
+
30
+ ##
31
+ # Fetch
32
+ # @api public
33
+ def cache_get(key)
34
+ return nil unless all_systems_go?
35
+ key = keygen(key.to_s)
36
+ if value = client.get(key)
37
+ Salli.log('get', key) if settings.cache_logging
38
+ Marshal.load(value)
39
+ end
40
+ end
41
+
42
+ ##
43
+ # Delete by key
44
+ # @api public
45
+ def cache_delete(key)
46
+ client.delete(keygen(key.to_s)) if all_systems_go?
47
+ end
48
+
49
+ ##
50
+ # Wipe out
51
+ # @api public
52
+ def cache_flush
53
+ client.flush if all_systems_go?
54
+ end
55
+
56
+ private
57
+
58
+ ##
59
+ # Can we cache?
60
+ # @api private
61
+ def all_systems_go?
62
+ @all_systems_go ||= settings.cache && correct_nature? && client
63
+ end
64
+
65
+ ##
66
+ # Environment check
67
+ # @api private
68
+ def correct_nature?
69
+ @correct_nature ||= (settings.environment == settings.cache_environment)
70
+ end
71
+
72
+ ##
73
+ # Dalli client
74
+ # @api private
75
+ def client
76
+ @client ||= Dalli::Client.new(settings.cache_server)
77
+ end
78
+
79
+ ##
80
+ # Keygen
81
+ # @api private
82
+ def keygen(key)
83
+ 'salli:' << request.host << ':' << key
84
+ end
85
+
86
+ ##
87
+ # Buffer?
88
+ # @api private
89
+ def buff?
90
+ @_out_buf
91
+ end
92
+
93
+ ##
94
+ # Call block
95
+ # @api private
96
+ def call_me(maybe)
97
+ tmp = maybe.call
98
+ buff? ? tmp[@_out_buf.length..-1] : tmp
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,3 @@
1
+ module Salli
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'salli/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "salli"
8
+ gem.version = Salli::VERSION
9
+ gem.authors = ["Daryl Ginn"]
10
+ gem.email = ["me@daryl.im"]
11
+ gem.description = %q{A Sinatra Dalli Gem for page, fragment and key value caching.}
12
+ gem.summary = %q{A Sinatra Dalli Gem for page, fragment and key value caching.}
13
+ gem.homepage = "https://github.com/daryl/Salli"
14
+
15
+ gem.add_dependency "dalli"
16
+
17
+ gem.files = `git ls-files`.split($/)
18
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
+ gem.require_paths = ["lib"]
21
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: salli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Daryl Ginn
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-05 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/salli.rb
43
+ - lib/salli/helpers.rb
44
+ - lib/salli/version.rb
45
+ - salli.gemspec
46
+ homepage: https://github.com/daryl/Salli
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.23
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: A Sinatra Dalli Gem for page, fragment and key value caching.
70
+ test_files: []