sinatra-simplecache 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +28 -2
- data/lib/sinatra/simplecache/version.rb +1 -1
- data/lib/sinatra/simplecache.rb +10 -6
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b78abec2742d26290b181e67e53a2a65976f6cae
|
4
|
+
data.tar.gz: edfa96bed9423298cf47f195273f494dacfdd39e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4ef941d40b97062c4dad36e8aedb35f8a35b7c0c19321d2b655c292b36067e6ff7fbb4cd2146668c195efcd879a4dec9b8e162f856717668461ddfce87d7caf2
|
7
|
+
data.tar.gz: 02cde8e9a7247e50f968796bbefcbe62c4ddb3443d28362bae92aa2f26f0d235ca09150dcca27373e6f64b085f2b365d9e2a2da13d23e6f55d1795035865aec6
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Sinatra::Simplecache
|
2
2
|
|
3
|
-
|
3
|
+
Simple cache helpers for Sinatra.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -18,7 +18,33 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
Modular Application:
|
22
|
+
|
23
|
+
``` ruby
|
24
|
+
require 'sinatra/base'
|
25
|
+
require 'sinatra/simplecache'
|
26
|
+
require 'erubis'
|
27
|
+
|
28
|
+
class TestApp < Sinatra::Base
|
29
|
+
helpers Sinatra::SimpleCache
|
30
|
+
set :erb, escape_html: true
|
31
|
+
|
32
|
+
get '/' do
|
33
|
+
cache(key: "index", expire: 10) { erb :index, locals: { _time: Time.now }}
|
34
|
+
end
|
35
|
+
|
36
|
+
run! if app_file == $0
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
`sinatra-simplecache` provide `cache` helper method.
|
41
|
+
This helper method take two args,
|
42
|
+
|
43
|
+
* options
|
44
|
+
- key: cache key
|
45
|
+
- expire: set expire time(Time or Numeric)
|
46
|
+
* block
|
47
|
+
- evaluate Template, RDB/KVS Query etc.
|
22
48
|
|
23
49
|
## Contributing
|
24
50
|
|
data/lib/sinatra/simplecache.rb
CHANGED
@@ -3,8 +3,7 @@ require "sinatra/base"
|
|
3
3
|
|
4
4
|
module Sinatra
|
5
5
|
module SimpleCache
|
6
|
-
#
|
7
|
-
#
|
6
|
+
# @example
|
8
7
|
# For operation cache:
|
9
8
|
# @@users = MySQL.query("...").to_a
|
10
9
|
# //=> @@users = cache(key: "users", expire: 0.6) { MySQL.query("...").to_a }
|
@@ -17,19 +16,24 @@ module Sinatra
|
|
17
16
|
# @@sidebar = cache(key: "sidebar", expire: 1) { slim: sidebar }
|
18
17
|
# slim :detail, :locales = { :sidebar => @@sidebar }
|
19
18
|
#
|
19
|
+
# @param [Hash] opts Must specified :key and :expire
|
20
|
+
# @option opts [String] :key Cache key
|
21
|
+
# @option opts [Numeric] :expire(Infinity) Expire time
|
22
|
+
# @yield Evaluate template, RDB/KVS query etc
|
23
|
+
# @return [String] Block evaluated value, or cached value.
|
20
24
|
def cache(opts={}, &block)
|
21
|
-
@@
|
25
|
+
@@__entries__ ||= {}
|
22
26
|
|
23
27
|
now = Time.now.to_f
|
24
|
-
expire = opts[:expire] ? (
|
28
|
+
(expire = opts[:expire]) ? (expire + now) : (@@inf ||= 1/0.0)
|
25
29
|
key = opts[:key] || (defined?(Sinatra) && request.path) || abort("set key")
|
26
30
|
|
27
|
-
if (e = @@
|
31
|
+
if (e = @@__entries__[key]) && (e[:expire] > now)
|
28
32
|
return e[:value]
|
29
33
|
end
|
30
34
|
|
31
35
|
value = block.call
|
32
|
-
@@
|
36
|
+
@@__entries__[key] = {
|
33
37
|
expire: expire,
|
34
38
|
value: value
|
35
39
|
}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-simplecache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- yoppi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sinatra
|
@@ -92,3 +92,4 @@ signing_key:
|
|
92
92
|
specification_version: 4
|
93
93
|
summary: Simple cache helper for Sinatra
|
94
94
|
test_files: []
|
95
|
+
has_rdoc:
|