zache 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 54dd40fa91ad83f55060c983e59e4e371064313a10954e10a616fb991b8d1f43
4
+ data.tar.gz: 3cbf51535b7b696e8d02fbb917739b3a46d9a39e22416e3b4de0b0527fc2eb75
5
+ SHA512:
6
+ metadata.gz: 1afff4f4fcf1f5250e291486a324a97bb8f78ce70eb042293c7f9d06bc745b043e1329c725e089f9b7579e389c405bad7469d085961dc3e909a00c0b46813d85
7
+ data.tar.gz: 2e88abe56d9447451253aa3a0829451ece1c8a6393cb2dd9cb1676bc0cc062722f5bd2bdf446a25cae8cd78cca3576a78f54553a408b5b6c5e67a0459559ce53
data/.0pdd.yml ADDED
@@ -0,0 +1,9 @@
1
+ errors:
2
+ - yegor256@gmail.com
3
+ # alerts:
4
+ # github:
5
+ # - yegor256
6
+
7
+ tags:
8
+ - pdd
9
+ - bug
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ Gemfile.lock
2
+ *.gem
3
+ .bundle/
4
+ .DS_Store
5
+ rdoc/
data/.pdd ADDED
@@ -0,0 +1,5 @@
1
+ --source=.
2
+ --verbose
3
+ --rule min-words:20
4
+ --rule min-estimate:15
5
+ --rule max-estimate:90
data/.rubocop.yml ADDED
@@ -0,0 +1,10 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+ TargetRubyVersion: 2.3.3
4
+
5
+ Layout/MultilineMethodCallIndentation:
6
+ Enabled: false
7
+ Metrics/AbcSize:
8
+ Max: 30
9
+ Metrics/MethodLength:
10
+ Max: 30
data/.rultor.yml ADDED
@@ -0,0 +1,23 @@
1
+ assets:
2
+ rubygems.yml: zerocracy/home#assets/rubygems.yml
3
+ install: |-
4
+ export GEM_HOME=~/.ruby
5
+ export GEM_PATH=$GEM_HOME:$GEM_PATH
6
+ release:
7
+ script: |-
8
+ bundle install
9
+ rake
10
+ rm -rf *.gem
11
+ sed -i "s/0\.0\.0/${tag}/g" zache.gemspec
12
+ git add zache.gemspec
13
+ git commit -m "Version set to ${tag}"
14
+ gem build zache.gemspec
15
+ chmod 0600 ../rubygems.yml
16
+ gem push *.gem --config-file ../rubygems.yml
17
+ commanders:
18
+ - yegor256
19
+ architect:
20
+ - yegor256
21
+ merge:
22
+ commanders: []
23
+ deploy: {}
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.3
4
+ branches:
5
+ only:
6
+ - master
7
+ install:
8
+ - bundle install
9
+ script:
10
+ - set -e
11
+ - rake
12
+
data/Gemfile ADDED
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (c) 2018 Yegor Bugayenko
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the 'Software'), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, 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,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ source 'https://rubygems.org'
24
+ gemspec
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ [![DevOps By Rultor.com](http://www.rultor.com/b/yegor256/zache)](http://www.rultor.com/p/yegor256/zache)
2
+ [![We recommend RubyMine](http://www.elegantobjects.org/rubymine.svg)](https://www.jetbrains.com/ruby/)
3
+
4
+ [![Build Status](https://travis-ci.org/yegor256/zache.svg)](https://travis-ci.org/yegor256/zache)
5
+ [![Gem Version](https://badge.fury.io/rb/zache.svg)](http://badge.fury.io/rb/zache)
6
+ [![Maintainability](https://api.codeclimate.com/v1/badges/c136afe340fa94f14696/maintainability)](https://codeclimate.com/github/yegor256/zache/maintainability)
7
+
8
+ It's a simple Ruby gem for in-memory cache.
9
+
10
+ First, install it:
11
+
12
+ ```bash
13
+ $ gem install zache
14
+ ```
15
+
16
+ Then, use it like this
17
+
18
+ ```ruby
19
+ require 'zache'
20
+ zache = Zache.new
21
+ # Expires in 5 minutes
22
+ v = zache.get(:count, lifetime: 5 * 60) { expensive() }
23
+ ```
24
+ That's it.
25
+
26
+ # How to contribute
27
+
28
+ Read [these guidelines](https://www.yegor256.com/2014/04/15/github-guidelines.html).
29
+ Make sure you build is green before you contribute
30
+ your pull request. You will need to have [Ruby](https://www.ruby-lang.org/en/) 2.3+ and
31
+ [Bundler](https://bundler.io/) installed. Then:
32
+
33
+ ```
34
+ $ bundle update
35
+ $ rake
36
+ ```
37
+
38
+ If it's clean and you don't see any error messages, submit your pull request.
39
+
40
+ # License
41
+
42
+ (The MIT License)
43
+
44
+ Copyright (c) 2018 Yegor Bugayenko
45
+
46
+ Permission is hereby granted, free of charge, to any person obtaining a copy
47
+ of this software and associated documentation files (the 'Software'), to deal
48
+ in the Software without restriction, including without limitation the rights
49
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
50
+ copies of the Software, and to permit persons to whom the Software is
51
+ furnished to do so, subject to the following conditions:
52
+
53
+ The above copyright notice and this permission notice shall be included in all
54
+ copies or substantial portions of the Software.
55
+
56
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
57
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
58
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
59
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
60
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
61
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
62
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (c) 2018 Yegor Bugayenko
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the 'Software'), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, 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,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ require 'rubygems'
24
+ require 'rake'
25
+ require 'rake/clean'
26
+
27
+ CLEAN = FileList['coverage']
28
+
29
+ def name
30
+ @name ||= File.basename(Dir['*.gemspec'].first, '.*')
31
+ end
32
+
33
+ def version
34
+ Gem::Specification.load(Dir['*.gemspec'].first).version
35
+ end
36
+
37
+ task default: %i[clean test rubocop]
38
+
39
+ require 'rake/testtask'
40
+ desc 'Run all unit tests'
41
+ Rake::TestTask.new(:test) do |test|
42
+ test.libs << 'lib' << 'test'
43
+ test.pattern = 'test/**/test_*.rb'
44
+ test.verbose = false
45
+ end
46
+
47
+ require 'rubocop/rake_task'
48
+ desc 'Run RuboCop on all directories'
49
+ RuboCop::RakeTask.new(:rubocop) do |task|
50
+ task.fail_on_error = true
51
+ task.requires << 'rubocop-rspec'
52
+ end
data/lib/zache.rb ADDED
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ # (The MIT License)
4
+ #
5
+ # Copyright (c) 2018 Yegor Bugayenko
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the 'Software'), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+
25
+ # Cache.
26
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
27
+ # Copyright:: Copyright (c) 2018 Yegor Bugayenko
28
+ # License:: MIT
29
+ class Zache
30
+ def initialize
31
+ @hash = {}
32
+ @mutex = Mutex.new
33
+ end
34
+
35
+ def get(key, lifetime: 60 * 60)
36
+ @mutex.synchronize do
37
+ rec = @hash[key]
38
+ rec = nil if !rec.nil? && rec[:start] < Time.now - rec[:lifetime]
39
+ if rec.nil?
40
+ @hash[key] = {
41
+ value: yield,
42
+ start: Time.now,
43
+ lifetime: lifetime
44
+ }
45
+ end
46
+ @hash[key][:value]
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ # (The MIT License)
4
+ #
5
+ # Copyright (c) 2018 Yegor Bugayenko
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the 'Software'), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+
25
+ require 'minitest/autorun'
26
+ require 'threads'
27
+ require_relative '../lib/zache'
28
+
29
+ # Cache test.
30
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
31
+ # Copyright:: Copyright (c) 2018 Yegor Bugayenko
32
+ # License:: MIT
33
+ class ZacheTest < Minitest::Test
34
+ def test_caches
35
+ cache = Zache.new
36
+ first = cache.get(:hey, lifetime: 5) { Random.rand }
37
+ second = cache.get(:hey) { Random.rand }
38
+ assert(first == second)
39
+ end
40
+
41
+ def test_caches_and_expires
42
+ cache = Zache.new
43
+ first = cache.get(:hey, lifetime: 0.01) { Random.rand }
44
+ sleep 0.1
45
+ second = cache.get(:hey) { Random.rand }
46
+ assert(first != second)
47
+ end
48
+
49
+ def test_caches_in_threads
50
+ cache = Zache.new
51
+ Threads.new(10).assert(100) do
52
+ cache.get(:hey, lifetime: 0.0001) { Random.rand }
53
+ end
54
+ end
55
+ end
data/zache.gemspec ADDED
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ # (The MIT License)
4
+ #
5
+ # Copyright (c) 2018 Yegor Bugayenko
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the 'Software'), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+
25
+ require 'English'
26
+ Gem::Specification.new do |s|
27
+ s.specification_version = 2 if s.respond_to? :specification_version=
28
+ if s.respond_to? :required_rubygems_version=
29
+ s.required_rubygems_version = Gem::Requirement.new('>= 0')
30
+ end
31
+ s.rubygems_version = '2.3.3'
32
+ s.required_ruby_version = '>=2.3'
33
+ s.name = 'zache'
34
+ s.version = '0.1.0'
35
+ s.license = 'MIT'
36
+ s.summary = 'In-memory Cache'
37
+ s.description = 'Zero-footprint in-memory thread-safe cache'
38
+ s.authors = ['Yegor Bugayenko']
39
+ s.email = 'yegor256@gmail.com'
40
+ s.homepage = 'http://github.com/yegor256/zache'
41
+ s.files = `git ls-files`.split($RS)
42
+ s.test_files = s.files.grep(%r{^(test)/})
43
+ s.rdoc_options = ['--charset=UTF-8']
44
+ s.extra_rdoc_files = ['README.md']
45
+ s.add_development_dependency 'minitest', '~>5'
46
+ s.add_development_dependency 'rake', '~>12'
47
+ s.add_development_dependency 'rdoc', '~>4'
48
+ s.add_development_dependency 'rubocop', '0.58.1'
49
+ s.add_development_dependency 'rubocop-rspec', '~>1'
50
+ s.add_development_dependency 'threads', '~>0'
51
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yegor Bugayenko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rdoc
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.58.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 0.58.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: threads
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Zero-footprint in-memory thread-safe cache
98
+ email: yegor256@gmail.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files:
102
+ - README.md
103
+ files:
104
+ - ".0pdd.yml"
105
+ - ".gitignore"
106
+ - ".pdd"
107
+ - ".rubocop.yml"
108
+ - ".rultor.yml"
109
+ - ".travis.yml"
110
+ - Gemfile
111
+ - README.md
112
+ - Rakefile
113
+ - lib/zache.rb
114
+ - test/test_zache.rb
115
+ - zache.gemspec
116
+ homepage: http://github.com/yegor256/zache
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options:
122
+ - "--charset=UTF-8"
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '2.3'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.7.6
138
+ signing_key:
139
+ specification_version: 2
140
+ summary: In-memory Cache
141
+ test_files:
142
+ - test/test_zache.rb