simple_cacher 0.0.3

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a5a25ca07f2607cb76e6a5672a047442544ac493
4
+ data.tar.gz: 9a5839c75a6078430f326f33abd0140746a7f13b
5
+ SHA512:
6
+ metadata.gz: f224f2776aa6a22ce8e29bbb99cedeff760864cd3fdb6d553dee64b182b97e2bd010747024a8d4f3673ed4f0af8ea1f4ffd5d6e1ab40346827aac4b39948c0c9
7
+ data.tar.gz: b3b82a2144ab14e29e49da69c4642e5849aff153e813406d69bc4dbaea18f185dcf3bea59439b805f5bf4ddde99e92e411a3ca3d8f5cea43fb80fac664751eb6
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2017 Billy.Zheng
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
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
+
3
+ TODO: Write a gem description
4
+
5
+ ## Philosophy
6
+
7
+ TODO: Write philosophy
8
+
9
+ ## Getting Started
10
+
11
+ Install via Rubygems
12
+
13
+ $ gem install simple_cacher
14
+
15
+ OR ...
16
+
17
+ Add to your Gemfile
18
+
19
+ gem 'simple_cacher'
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Support
26
+
27
+ * MRI 1.9.3+
28
+ * Rubinius 2.2+
29
+
30
+ ## Limitations
31
+
32
+ No known limit.
33
+
34
+ ## History
35
+
36
+ See [CHANGELOG](https://github.com/zw963/simple_cacher/blob/master/CHANGELOG) for details.
37
+
38
+ ## Contributing
39
+
40
+ * [Bug reports](https://github.com/zw963/simple_cacher/issues)
41
+ * [Source](https://github.com/zw963/simple_cacher)
42
+ * Patches:
43
+ * Fork on Github.
44
+ * Run `gem install --dev simple_cacher` or `bundle`.
45
+ * Create your feature branch: `git checkout -b my-new-feature`.
46
+ * Commit your changes: `git commit -am 'Add some feature'`.
47
+ * Push to the branch: `git push origin my-new-feature`.
48
+ * Send a pull request :D.
49
+
50
+ ## license
51
+
52
+ Released under the MIT license, See [LICENSE](https://github.com/zw963/simple_cacher/blob/master/LICENSE) for details.
@@ -0,0 +1,51 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ class SimpleCacher
5
+ attr_reader :redis, :namespace
6
+
7
+ def initialize(namespace:)
8
+ # 默认使用最后一个 db, 避免冲突.
9
+ # TODO: 使得 db 可配置
10
+ @redis = Redis.new(url: 'redis://127.0.0.1:6379/15')
11
+ @namespace = Digest::MD5.new.update(namespace).to_s
12
+ end
13
+
14
+ def nskey(key)
15
+ "#{namespace}:#{key}"
16
+ end
17
+
18
+ def import(key:)
19
+ key = nskey(key)
20
+
21
+ redis.exists(key) ? JSON.parse(key) : nil
22
+ end
23
+
24
+ def export(key:, data: nil, expire: nil)
25
+ key = nskey(key)
26
+
27
+ hash = redis.set(key, data.to_json)
28
+ redis.expire(key, expire.to_i) unless expire.nil?
29
+ hash
30
+ end
31
+
32
+ def reach_limit?(key:, limit:, expire: nil)
33
+ key = nskey(key)
34
+
35
+ if redis.exists(key)
36
+ # 如果存在, 递增 1, 记录访问的次数.
37
+ redis.incr(key)
38
+ else
39
+ redis.set(key, '1')
40
+ redis.expire(key, expire.to_i) unless expire.nil?
41
+ end
42
+
43
+ redis.get(key).to_i > limit ? true : false
44
+ end
45
+
46
+ def fresh?(key:)
47
+ (Rails.env.production? or Rails.env.test?) &&
48
+ redis.exists(nskey(key))
49
+ end
50
+ alias exists? fresh?
51
+ end
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ module SimpleCacher
5
+ VERSION = [0, 0, 3]
6
+
7
+ class << VERSION
8
+ def to_s
9
+ join('.')
10
+ end
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_cacher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Billy.Zheng(zw963)
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ritual
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.4'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.4'
41
+ description: ''
42
+ email:
43
+ - zw963@163.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - LICENSE
49
+ - README.md
50
+ - lib/simple_cacher.rb
51
+ - lib/simple_cacher/version.rb
52
+ homepage: http://github.com/zw963/simple_cacher
53
+ licenses:
54
+ - MIT
55
+ metadata: {}
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 1.9.1
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 2.6.10
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: ''
76
+ test_files: []