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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 576f933f6320c8664f9a67d49b3698024c27029a
4
- data.tar.gz: cadea424ed9eabc1bc1f9769244cffda813c3335
3
+ metadata.gz: 66973e874b55d97a1c153fb4f320335064c95e62
4
+ data.tar.gz: 2247514996e98307c6a6621217ed84cbb1b332ef
5
5
  SHA512:
6
- metadata.gz: cb0d74c12d9b6ed68af4318ee1fd7f3a1da9932378e63192efb44c197280478ff90a723e768e1c3bf08980541826951328f2c57a8055c9442780a894eb83e5ed
7
- data.tar.gz: ca89b2abc33f7209fed03ffd46ec30817cb3ff239b1a2127724121b3ae48daf1015199cd4a97e110ac4fa7be1ffb122120de9fefff62f94636a4db7e7aa0fc71
6
+ metadata.gz: b80c9ae76dbf0cfa2f0a0c1572b2060bac5e6ce1f8d02cc9e12d9d6b8eee4ca5d3bcf75f03571f2a873c85f375931904060dc06a7b5d0e8706163ef20d6989e3
7
+ data.tar.gz: 826f423ddf305cc23b944c5f188cd7c46a05546788d6e24ff0c5aa3120efbeb633d4c88d3e9fef3377728a735ebc99f429465655530a53c6fd0de5d0e3004b6c
data/README.md CHANGED
@@ -1,10 +1,6 @@
1
1
  # SimpleCacher [![Build Status](https://travis-ci.org/zw963/simple_cacher.svg?branch=master)](https://travis-ci.org/zw963/simple_cacher) [![Gem Version](https://badge.fury.io/rb/simple_cacher.svg)](http://badge.fury.io/rb/simple_cacher)
2
2
 
3
- TODO: Write a gem description
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
- TODO: Write usage instructions here
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
- * MRI 1.9.3+
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 'Key not exist, import failed!'
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.setnx(key, data.to_json)
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 'Key alreday exist in cacher, export failed!'
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 use cache
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))
@@ -2,7 +2,7 @@
2
2
  # -*- coding: utf-8 -*-
3
3
 
4
4
  module SimpleCacher
5
- VERSION = [0, 0, 8]
5
+ VERSION = [0, 0, 9]
6
6
 
7
7
  class << VERSION
8
8
  def to_s
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.8
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-01 00:00:00.000000000 Z
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.10
72
+ rubygems_version: 2.6.12
73
73
  signing_key:
74
74
  specification_version: 4
75
75
  summary: ''