sinatra-dalli 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,72 @@
1
+ = Sinatra-Dalli
2
+
3
+ == Install
4
+
5
+ gem install sinatra-dalli
6
+
7
+ == Dependencies
8
+
9
+ dalli
10
+ zlib
11
+
12
+ == Usage:
13
+
14
+ require 'rubygems'
15
+ require 'sinatra'
16
+ require 'sinatra/dalli'
17
+
18
+ # cache
19
+ get '/cache1' do
20
+ cache 'cache1' do
21
+ sleep(5)
22
+ 'Hello Cache1'
23
+ end
24
+ end
25
+
26
+ # args
27
+ get '/cache2' do
28
+ cache 'cache2', :expiry => 10, :compress => true do
29
+ sleep(3)
30
+ 'Hello Cache2'
31
+ end
32
+ end
33
+
34
+ # cache object
35
+ get '/obj' do
36
+ hash = cache 'obj' do
37
+ sleep(2)
38
+ { :a => 'Hello Object' }
39
+ end
40
+ hash[:a]
41
+ end
42
+
43
+ # expire
44
+ get '/expire' do
45
+ expire 'cache1'
46
+ expire /^cache/
47
+ expire //
48
+ 'Hello Expire'
49
+ end
50
+
51
+ # default options
52
+ set :cache_server, "localhost:11211"
53
+ set :cache_namespace, "sinatra-dalli"
54
+ set :cache_enable, true
55
+ set :cache_logging, true
56
+ set :cache_default_expiry, 3600
57
+ set :cache_default_compress, false
58
+
59
+ == Authors & Contributors
60
+
61
+ Kazuki Uchida, gioext@gmail.com
62
+
63
+ Casey Manion, casey@manion.com
64
+
65
+ == License
66
+
67
+ Copyright (c) 2009 Kazuki UCHIDA
68
+
69
+ Licensed under the MIT License:
70
+
71
+ http://www.opensource.org/licenses/mit-license.php
72
+
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'spec/rake/spectask'
3
+
4
+ task :default => :spec
5
+
6
+ desc "Run all specs in spec directory"
7
+ Spec::Rake::SpecTask.new(:spec) do |t|
8
+ t.spec_opts = ['--color', '--format specdoc']
9
+ t.spec_files = FileList['spec/**/*_spec.rb']
10
+ end
@@ -0,0 +1,101 @@
1
+ # Based on https://github.com/JGailor/sinatra-memcache/blob/master/lib/sinatra/memcache.rb
2
+ # Modified to use dalli instead of memcache-client
3
+
4
+ require 'dalli'
5
+ require 'zlib'
6
+
7
+ module Sinatra
8
+ module Dalli
9
+ module Helpers
10
+
11
+ #
12
+ #
13
+ #
14
+ def cache(key, params = {}, &block)
15
+ return block.call unless settings.cache_enable
16
+
17
+ opts = {
18
+ :expiry => settings.cache_default_expiry,
19
+ :compress => settings.cache_default_compress
20
+ }.merge(params)
21
+
22
+ value = get(key, opts)
23
+ return value unless block_given?
24
+
25
+ if value
26
+ log "Get: #{key}"
27
+ value
28
+ else
29
+ log "Set: #{key}"
30
+ set(key, block.call, opts)
31
+ end
32
+ rescue => e
33
+ throw e if settings.development? || settings.show_exceptions
34
+ block.call
35
+ end
36
+
37
+ #
38
+ #
39
+ #
40
+ def expire(p)
41
+ return unless settings.cache_enable
42
+
43
+ case p
44
+ when String
45
+ expire_key(p)
46
+ when Regexp
47
+ expire_regexp(p)
48
+ end
49
+ true
50
+ rescue => e
51
+ throw e if settings.development? or settings.show_exceptions
52
+ false
53
+ end
54
+
55
+ private
56
+
57
+ def client
58
+ settings.cache_client ||= ::Dalli::Client.new settings.cache_server,
59
+ :namespace => settings.cache_namespace
60
+ end
61
+
62
+ def log(msg)
63
+ puts "[sinatra-dalli] #{msg}" if settings.cache_logging
64
+ end
65
+
66
+ def get(key, opts)
67
+ v = client.get(key, :raw => true)
68
+ return v unless v
69
+
70
+ v = Zlib::Inflate.inflate(v) if opts[:compress]
71
+ Marshal.load(v)
72
+ end
73
+
74
+ def set(key, value, opts)
75
+ v = Marshal.dump(value)
76
+ v = Zlib::Deflate.deflate(v) if opts[:compress]
77
+ client.set(key, v, opts[:expiry], :raw => true)
78
+ value
79
+ end
80
+
81
+ def expire_key(key)
82
+ client.delete(key)
83
+ log "Expire: #{key}"
84
+ end
85
+ end
86
+
87
+ def self.registered(app)
88
+ app.helpers Dalli::Helpers
89
+
90
+ app.set :cache_client, nil
91
+ app.set :cache_server, 'localhost:11211'
92
+ app.set :cache_namespace, 'sinatra-dalli'
93
+ app.set :cache_enable, true
94
+ app.set :cache_logging, true
95
+ app.set :cache_default_expiry, 3600
96
+ app.set :cache_default_compress, false
97
+ end
98
+ end
99
+
100
+ register Dalli
101
+ end
@@ -0,0 +1,51 @@
1
+
2
+ get '/' do
3
+ "Hello World"
4
+ end
5
+
6
+ get '/cache' do
7
+ cache "cache" do
8
+ "Hello World"
9
+ end
10
+ end
11
+
12
+ get '/cache2' do
13
+ cache "cache2", :expiry => 1 do
14
+ "Hello World"
15
+ end
16
+ end
17
+
18
+ get '/read' do
19
+ cache 'cache'
20
+ end
21
+
22
+ get '/compress' do
23
+ cache "compress", :compress => true do
24
+ "Hello Compress"
25
+ end
26
+ end
27
+
28
+ get '/object' do
29
+ hash = cache "object" do
30
+ { :a => 'hello a', :b => 'hello b' }
31
+ end
32
+ hash[:a] + ' ' + hash[:b]
33
+ end
34
+
35
+ get '/expire' do
36
+ expire "cache"
37
+ end
38
+
39
+ get '/drop' do
40
+ end
41
+
42
+ get '/expire_all' do
43
+ expire "cache"
44
+ expire "cache2"
45
+ expire "compress"
46
+ end
47
+
48
+ configure do
49
+ set :cache_namespace, "test"
50
+ set :cache_logging, false
51
+ end
@@ -0,0 +1,77 @@
1
+ require 'rubygems'
2
+ require 'sinatra'
3
+ require 'rack/test'
4
+
5
+ require File.dirname(__FILE__) + '/../lib/sinatra/dalli'
6
+ require File.dirname(__FILE__) + '/fixture'
7
+
8
+ include Rack::Test::Methods
9
+ def app
10
+ Sinatra::Application
11
+ end
12
+
13
+ describe 'Sinatra-Dalli' do
14
+ before do
15
+ @client = Dalli::Client.new 'localhost:11211', :namespace => "test"
16
+ end
17
+
18
+ it "/index" do
19
+ get '/'
20
+ last_response.ok?.should be_true
21
+ last_response.body.should == "Hello World"
22
+ end
23
+
24
+ it "/cache" do
25
+ get '/cache'
26
+ last_response.ok?.should be_true
27
+ last_response.body.should == "Hello World"
28
+ end
29
+
30
+ it "/cache - marshal" do
31
+ get '/cache'
32
+ Marshal.load(@client.get('cache', true)).should == "Hello World"
33
+ end
34
+
35
+ it "/cache, /read" do
36
+ get '/cache'
37
+ get '/read'
38
+ last_response.ok?.should be_true
39
+ last_response.body.should == "Hello World"
40
+ end
41
+
42
+ it "/cache, /expire - nil" do
43
+ get '/cache'
44
+ get '/expire'
45
+ @client.get('cache').should be_nil
46
+ end
47
+
48
+ it "/cache2 - nil" do
49
+ get '/cache2'
50
+ sleep(1)
51
+ @client.get('cache2').should be_nil
52
+ end
53
+
54
+ it "/compress" do
55
+ get '/compress'
56
+ last_response.ok?.should be_true
57
+ last_response.body.should == "Hello Compress"
58
+ @client.get('compress', true).should == Zlib::Deflate.deflate(Marshal.dump('Hello Compress'))
59
+ end
60
+
61
+ it "/object" do
62
+ get '/object'
63
+ last_response.ok?.should be_true
64
+ last_response.body.should == "hello a hello b"
65
+ Marshal.load(@client.get('object', true)).should == { :a => 'hello a', :b => 'hello b' }
66
+ end
67
+
68
+ it "/cache, /cache2, /compress, /expire_all - nil " do
69
+ get '/cache'
70
+ get '/cache2'
71
+ get '/compress'
72
+ get '/expire_all'
73
+ @client.get('cache').should be_nil
74
+ @client.get('cache2').should be_nil
75
+ @client.get('compress').should be_nil
76
+ end
77
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-dalli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Casey Manion
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-04-30 00:00:00.000000000 +00:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sinatra
17
+ requirement: &11569220 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *11569220
26
+ - !ruby/object:Gem::Dependency
27
+ name: dalli
28
+ requirement: &11568780 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *11568780
37
+ description: Cache extension on Sinatra
38
+ email: casey@manion.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - Rakefile
44
+ - README.rdoc
45
+ - lib/sinatra/dalli.rb
46
+ - spec/sinatra-dalli_spec.rb
47
+ - spec/fixture.rb
48
+ has_rdoc: true
49
+ homepage: https://github.com/caseman72/sinatra-dalli
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 1.6.2
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Cache extension on Sinatra
73
+ test_files: []