sinatra-redis-cache 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +20 -0
  3. data/README.md +44 -0
  4. data/Rakefile +13 -0
  5. data/lib/sinatra/redis-cache.rb +143 -0
  6. metadata +89 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8fddb41084d855236180b8f318398cacf0ca4339
4
+ data.tar.gz: 011f08ff6d8b377e71f13d2c26b867fb8840bebe
5
+ SHA512:
6
+ metadata.gz: 9f187012a94865f91273d809d57badadb5bd47f0476d79e91e66989212f2eab63e74cf2d61f25bdde0c74180b8f576d7b8dcf0cce0f074ab719a7f90da35bded
7
+ data.tar.gz: 6f4ee2343b063daa6077301382ee00bbc2e2adaeca390939f6be444e06c65b64befa8bcd36b0bb6eac0da66ed02502013d8a9988c0b5c856d80be204b67c3248
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (C) 2014 Warren Guy <warren@guy.net.au>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # sinatra/redis-cache
2
+
3
+ A simple redis backed cache for Sinatra applications.
4
+
5
+ ## Installing
6
+
7
+ * Add the gem to your Gemfile
8
+
9
+ ```ruby
10
+ gem 'sinatra-redis-cache', git: 'https://github.com/warrenguy/sinatra-redis-cache.git'
11
+ ```
12
+
13
+ * Require it in your app after including Sinatra
14
+
15
+ ```ruby
16
+ require 'sinatra/redis-cache'
17
+ ```
18
+
19
+ ## Using in your app
20
+
21
+ Use `cache_do` anywhere you want to cache the output of a block of code.
22
+
23
+ The following will be cached for 60 seconds with the key `test-key`.
24
+
25
+ ```ruby
26
+ cache_do('test-key', 60) do
27
+ 'do something here'
28
+ end
29
+ ```
30
+
31
+ If there is an unexpired object in the cache with the same key, it will be returned. If not, the code will be executed, returned, and stored in the cache.
32
+
33
+ ## Configuration
34
+
35
+ Include the following block in your app after including `sinatra/redis-cache` to configure. Defaults are shown.
36
+
37
+ ```ruby
38
+ Sinatra::RedisCache::Config.config do
39
+ redis_conn Redis.new
40
+ namespace 'sinatra_cache'
41
+ default_expires 3600
42
+ environments [:production]
43
+ end
44
+ ```
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ namespace :sinatra_cache do
2
+ desc 'Flush the cache'
3
+ task :flush do
4
+ include Sinatra::RedisCache
5
+ cache_flush
6
+ end
7
+
8
+ desc 'Print configured namespace'
9
+ task :namespace do
10
+ include Sinatra::RedisCache
11
+ Sinatra::RedisCache.config.namespace
12
+ end
13
+ end
@@ -0,0 +1,143 @@
1
+ require 'sinatra/base'
2
+ require 'redis'
3
+
4
+ module Sinatra
5
+
6
+ module RedisCache
7
+
8
+ module Config
9
+ extend self
10
+
11
+ def parameter(*names)
12
+ names.each do |name|
13
+ attr_accessor name
14
+
15
+ define_method name do |*values|
16
+ value = values.first
17
+ if value
18
+ self.send("#{name}=", value)
19
+ else
20
+ instance_variable_get("@#{name}")
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ def config(&block)
27
+ instance_eval &block
28
+ end
29
+ end
30
+
31
+ class RedisCache
32
+ def do(key, expires, params={}, block)
33
+ key = key_with_namespace(key)
34
+ if Sinatra::RedisCache::Config.environments.include? Sinatra::Base.settings.environment
35
+ object = get(key)
36
+ if object
37
+ object
38
+ else
39
+ object = block.call
40
+ store(key, object, expires)
41
+ object
42
+ end
43
+ else
44
+ # Just run the block without cache if we're not in an allowed environment
45
+ block.call
46
+ end
47
+ end
48
+
49
+ def get(key, params={})
50
+ key = key_with_namespace(key)
51
+ redis.get(key)
52
+ end
53
+
54
+ def store(key, value, expires, params={})
55
+ key = key_with_namespace(key)
56
+ expires = expires || config.default_expires
57
+ redis.set(key, value)
58
+ redis.expire(key, expires)
59
+ end
60
+
61
+ def flush
62
+ redis.del(all_keys)
63
+ end
64
+
65
+ private
66
+
67
+ def config
68
+ Sinatra::RedisCache::Config
69
+ end
70
+
71
+ def redis
72
+ config.redis_conn
73
+ end
74
+
75
+ def namespace
76
+ config.namespace
77
+ end
78
+
79
+ def key_with_namespace(key)
80
+ if key.start_with? namespace
81
+ key
82
+ else
83
+ "#{namespace}:#{key}"
84
+ end
85
+ end
86
+
87
+ def key_without_namespace(key)
88
+ if key.start_with? namespace
89
+ key.gsub(/^#{namespace}:/,'')
90
+ else
91
+ key
92
+ end
93
+ end
94
+
95
+ def all_keys
96
+ redis.keys(namespace + '*')
97
+ end
98
+ end
99
+
100
+ def cache_do(key, expires=nil, params={}, &block)
101
+ cache = RedisCache.new
102
+ cache.do(key, expires, params, block)
103
+ end
104
+
105
+ def cache_get(key, params={})
106
+ cache = RedisCache.new
107
+ cache.get(key, params)
108
+ end
109
+
110
+ def cache_store(key, value, expires, params={})
111
+ cache = RedisCache.new
112
+ cache.store(key, value, expires, params)
113
+ end
114
+
115
+ def cache_flush
116
+ cache = RedisCache.new
117
+ cache.flush
118
+ end
119
+
120
+ end
121
+
122
+ helpers RedisCache
123
+ end
124
+
125
+ Sinatra::RedisCache::Config.config do
126
+ # Set up configurable values
127
+ parameter :redis_conn
128
+ parameter :namespace
129
+ parameter :default_expires
130
+ parameter :environments
131
+ end
132
+
133
+ Sinatra::RedisCache::Config.config do
134
+ # Default values
135
+ redis_conn Redis.new
136
+ namespace 'sinatra_cache'
137
+ default_expires 3600
138
+ environments [:production]
139
+ end
140
+
141
+ unless defined?(Rake).nil?
142
+ Rake.load_rakefile("#{File.dirname(__FILE__)}/../../Rakefile")
143
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-redis-cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Warren Guy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-10 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.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: redis
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: A simple redis backed cache for Sinatra
56
+ email: warren@guy.net.au
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - LICENSE
62
+ - README.md
63
+ - Rakefile
64
+ - lib/sinatra/redis-cache.rb
65
+ homepage: https://github.com/warrenguy/sinatra-redis-cache
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.2.2
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: A simple redis backed cache for Sinatra
89
+ test_files: []