simple_cacher 0.0.8 → 0.0.9
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.
- checksums.yaml +4 -4
- data/README.md +30 -8
- data/lib/simple_cacher.rb +8 -15
- data/lib/simple_cacher/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 66973e874b55d97a1c153fb4f320335064c95e62
|
4
|
+
data.tar.gz: 2247514996e98307c6a6621217ed84cbb1b332ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b80c9ae76dbf0cfa2f0a0c1572b2060bac5e6ce1f8d02cc9e12d9d6b8eee4ca5d3bcf75f03571f2a873c85f375931904060dc06a7b5d0e8706163ef20d6989e3
|
7
|
+
data.tar.gz: 826f423ddf305cc23b944c5f188cd7c46a05546788d6e24ff0c5aa3120efbeb633d4c88d3e9fef3377728a735ebc99f429465655530a53c6fd0de5d0e3004b6c
|
data/README.md
CHANGED
@@ -1,10 +1,6 @@
|
|
1
1
|
# SimpleCacher [](https://travis-ci.org/zw963/simple_cacher) [](http://badge.fury.io/rb/simple_cacher)
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
## Philosophy
|
6
|
-
|
7
|
-
TODO: Write philosophy
|
3
|
+
A very simple use/implement but very useful cacher, use Redis database.
|
8
4
|
|
9
5
|
## Getting Started
|
10
6
|
|
@@ -20,12 +16,38 @@ Add to your Gemfile
|
|
20
16
|
|
21
17
|
## Usage
|
22
18
|
|
23
|
-
|
19
|
+
### Use as a home page cache, expired within 120 second.
|
20
|
+
|
21
|
+
```rb
|
22
|
+
# The first step is need specify a namespace, one string which should unique each other
|
23
|
+
# for different purpose, in this current case (one Rails controller action), `request.url'
|
24
|
+
# is a perfect candidate.
|
25
|
+
cacher = SimpleCacher.new(namespace: request.url)
|
26
|
+
|
27
|
+
if cacher.fresh?(key: 'items')
|
28
|
+
@items_hash = cacher.import(key: 'items')
|
29
|
+
else
|
30
|
+
# do something get correct data and assign to a variable items_hash
|
31
|
+
@items_hash = cacher.export(key: 'items', data: items_hash, expire: 120)
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
### Use as a counter, limit IP access during a time span.
|
36
|
+
|
37
|
+
```rb
|
38
|
+
counter = SimpleCacher.new(namespace: 'my_api:sms_log:user_ip')
|
39
|
+
|
40
|
+
# Sum IP 123.123.123.123 access count in one day,
|
41
|
+
if counter.reach_limit?(key: '123.123.123.123', limit: 5, expire: 86400)
|
42
|
+
# if exceed the limit, sent a warn info
|
43
|
+
else
|
44
|
+
# do some sutff
|
45
|
+
end
|
46
|
+
```
|
24
47
|
|
25
48
|
## Support
|
26
49
|
|
27
|
-
|
28
|
-
* Rubinius 2.2+
|
50
|
+
Still not test, should work in recent ruby version, the only dependency is [redis-rb](https://github.com/redis/redis-rb)
|
29
51
|
|
30
52
|
## Limitations
|
31
53
|
|
data/lib/simple_cacher.rb
CHANGED
@@ -16,44 +16,37 @@ class SimpleCacher
|
|
16
16
|
if redis.exists(key)
|
17
17
|
JSON.load(redis.get(key))
|
18
18
|
else
|
19
|
-
fail '
|
19
|
+
fail 'Import failed, key not exist!'
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
23
|
def export(key:, data: nil, expire: nil)
|
24
24
|
key = nskey(key)
|
25
|
+
nx = __callee__ == :export
|
25
26
|
expire = Integer(expire) unless expire.nil?
|
26
27
|
|
27
|
-
if redis.
|
28
|
-
redis.expire(key, expire) unless expire.nil?
|
28
|
+
if redis.set(key, data.to_json, nx: nx, ex: expire)
|
29
29
|
JSON.load(redis.get(key))
|
30
30
|
else
|
31
|
-
fail '
|
31
|
+
fail 'Export failed, key was exist!'
|
32
32
|
end
|
33
33
|
end
|
34
|
-
|
35
|
-
def expire!(key)
|
36
|
-
key = nskey(key)
|
37
|
-
|
38
|
-
redis.expire(key, -1)
|
39
|
-
end
|
34
|
+
alias export! export
|
40
35
|
|
41
36
|
def reach_limit?(key:, limit:, expire: nil)
|
42
37
|
key = nskey(key)
|
43
38
|
expire = Integer(expire) unless expire.nil?
|
44
|
-
limit = Integer(limit)
|
45
39
|
|
46
40
|
if redis.exists(key)
|
47
|
-
redis.incr(key) > limit ? true : false
|
41
|
+
redis.incr(key) > Integer(limit) ? true : false
|
48
42
|
else
|
49
|
-
redis.set(key, '1')
|
50
|
-
redis.expire(key, expire) unless expire.nil?
|
43
|
+
redis.set(key, '1', ex: expire)
|
51
44
|
false
|
52
45
|
end
|
53
46
|
end
|
54
47
|
|
55
48
|
def fresh?(key:)
|
56
|
-
# if in develpment mode, not
|
49
|
+
# if use with rails in develpment mode, not detect key existance.
|
57
50
|
return false if defined?(Rails) && Rails.env.development?
|
58
51
|
|
59
52
|
redis.exists(nskey(key))
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_cacher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Billy.Zheng(zw963)
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-06-
|
11
|
+
date: 2017-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|
@@ -69,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
69
|
version: '0'
|
70
70
|
requirements: []
|
71
71
|
rubyforge_project:
|
72
|
-
rubygems_version: 2.6.
|
72
|
+
rubygems_version: 2.6.12
|
73
73
|
signing_key:
|
74
74
|
specification_version: 4
|
75
75
|
summary: ''
|