blunt-cache 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
+ SHA1:
3
+ metadata.gz: fa23a41b5b4323f62abb654de3248b0284d53f39
4
+ data.tar.gz: 39d07e022f2c23a94dc970140ce9caa009b41335
5
+ SHA512:
6
+ metadata.gz: a3446dc8ab676bcf121f8e6dae706f41dc815238711ddfaa3adf48941d6b96f57d25fd6bf72f48d8667deed52742f8a0dfd6566ea2d2a8bf513915c8be0685d2
7
+ data.tar.gz: da6241b12fd898a9feb20ac033e94100d85800c1dce0773aa6b785415c31f82eee3d81e6cbc22a04a405c83ec287a9d1f2bbfadfb9bcb03697286c283bd0d975
data/.gitignore ADDED
@@ -0,0 +1,35 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /vendor/bundle
26
+ /lib/bundler/man/
27
+
28
+ # for a library or gem, you might want to ignore these files since the code is
29
+ # intended to run in multiple environments; otherwise, check them in:
30
+ # Gemfile.lock
31
+ # .ruby-version
32
+ # .ruby-gemset
33
+
34
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aerials.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ blunt_cache (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.5)
10
+ rake (10.4.2)
11
+ rspec (3.3.0)
12
+ rspec-core (~> 3.3.0)
13
+ rspec-expectations (~> 3.3.0)
14
+ rspec-mocks (~> 3.3.0)
15
+ rspec-core (3.3.2)
16
+ rspec-support (~> 3.3.0)
17
+ rspec-expectations (3.3.1)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.3.0)
20
+ rspec-mocks (3.3.2)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.3.0)
23
+ rspec-support (3.3.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ blunt_cache!
30
+ bundler (~> 1.6)
31
+ rake
32
+ rspec (~> 3.1)
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Roman Exempliarov
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
+
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # BluntCache
2
+ Simple in-memory cache service for ruby.
3
+
4
+ ## Usage
5
+
6
+ # set/get by key
7
+ BluntCache.set "key", data
8
+ data = BluntCache.get "key"
9
+
10
+ # executes block if not set or expired
11
+ data = BluntCache.fetch "key" do
12
+ do_something
13
+ end
14
+
15
+ # time to live can be provided (dafault is 60 sec)
16
+ BluntCache.set "key", expire: 120 data
17
+ BluntCache.fetch "key", expire: 120 do
18
+ do_something
19
+ end
20
+
21
+ # inherit it for namespacing and extending
22
+ class MyCache < BluntCache
23
+ @expire_default = 30
24
+ end
25
+
26
+ MyCache.set "1", "2"
27
+ BluntCache.set "1", "3"
28
+ MyCache.get "1" #2
29
+
30
+ ## Why? When to use?
31
+
32
+ * It is fast.
33
+ * Use it when you don't want to execute seralization-deseralization cycle with real cache (Redis or Memcache).
34
+ * Use it when you not able (or don't want) to use external cache service (you or your admin are lazy, when using Heroku or other cloud services, etc).
35
+
36
+ ## Limitations
37
+
38
+ * Keep in mind that values are stored in memory even if they are expired. As for replaced values, let's just belive in Ruby GC abilities. So, Ruby workers can bloat.
39
+ * Keep in mind that Cache IS NOT shared between workers (e.g. Unicorn, Puma cluster workers) and IS shared between threads (Puma).
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'blunt_cache/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "blunt-cache"
8
+ spec.version = BluntCache::VERSION
9
+ spec.authors = ["Roman Exempliarov"]
10
+ spec.email = ["urvala@gmail.com"]
11
+ spec.summary = %q{Simple in-memory cache service for ruby.}
12
+ spec.description = %q{Simple in-memory cache service for ruby.}
13
+ spec.homepage = "https://github.com/appelsin/blunt_cache"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake", ">= 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.1"
24
+ end
@@ -0,0 +1,46 @@
1
+ # In-memory cache service.
2
+ class BluntCache
3
+ @expire_default = 60
4
+
5
+ # Store +data+ in cache by +key+ for +:expire+ seconds (default is 60 sec)
6
+ def self.set(key, data, expire: nil)
7
+ self.timestamp[key] = Time.now + (expire || self.expire_default)
8
+ self.data[key] = data
9
+ data
10
+ end
11
+
12
+ # Get +key+ from cache. Returns nil if not set or expired.
13
+ def self.get(key)
14
+ self.timestamp[key].is_a?(Time) && Time.now < self.timestamp[key] ? self.data[key] : nil
15
+ end
16
+
17
+ # Get +key+ from cache. Executes +block+, stores it's result and returns it if not set or expired.
18
+ def self.fetch(key, expire: nil, &block)
19
+ result = self.get key
20
+ if result.nil?
21
+ self.set key, block.call, expire: expire
22
+ else
23
+ result
24
+ end
25
+ end
26
+
27
+ # Clear cache
28
+ def self.flush
29
+ @data = {}
30
+ @timestamp = {}
31
+ end
32
+
33
+ protected
34
+
35
+ def self.data
36
+ @data||= {}
37
+ end
38
+
39
+ def self.timestamp
40
+ @timestamp||= {}
41
+ end
42
+
43
+ def self.expire_default
44
+ @expire_default||=60
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ class BluntCache
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+
3
+ describe BluntCache do
4
+
5
+ class MyCache < BluntCache
6
+ end
7
+
8
+ class ShortCache < BluntCache
9
+ @expire_default = 0.1
10
+ end
11
+
12
+ [BluntCache, MyCache, ShortCache].each do |c|
13
+ context '#{c.name}' do
14
+ it 'stores data' do
15
+ expect { c.set("1", "1_val") }.not_to raise_error
16
+ result = nil
17
+ expect { result = c.get("1") }.not_to raise_error
18
+ expect(result).to eq "1_val"
19
+ expect { c.set("1", "1_val_2") }.not_to raise_error
20
+ expect { result = c.get("1") }.not_to raise_error
21
+ expect(result).to eq "1_val_2"
22
+ end
23
+
24
+ it 'returns nil on no data' do
25
+ result = "value"
26
+ expect { result = c.get("2") }.not_to raise_error
27
+ expect(result).to eq nil
28
+ end
29
+
30
+ it 'returns value before expiration and nil after expiration' do
31
+ expect { c.set("3", "3_val", expire: 0.1) }.not_to raise_error
32
+ result = nil
33
+ expect { result = c.get("3") }.not_to raise_error
34
+ expect(result).to eq "3_val"
35
+ sleep(0.09)
36
+ expect { result = c.get("3") }.not_to raise_error
37
+ expect(result).to eq "3_val"
38
+ sleep(0.02)
39
+ expect { result = c.get("3") }.not_to raise_error
40
+ expect(result).to eq nil
41
+ end
42
+
43
+ it 'evals block in fetch if cache not set' do
44
+ expect(c.get("4")).to eq nil
45
+ result = nil
46
+ expect { result = c.fetch("4") do "4_val" end }.not_to raise_error
47
+ expect(result).to eq "4_val"
48
+ expect(c.get("4")).to eq "4_val"
49
+ expect { result = c.fetch("4") do "4_val_2" end }.not_to raise_error
50
+ expect(result).to eq "4_val"
51
+ end
52
+
53
+ it 'returns value before expiration and re-executes block after expiration (fetch)' do
54
+ result = nil
55
+ expect { result = c.fetch "6", expire: 0.1 do "6_val" end }.not_to raise_error
56
+ expect(result).to eq "6_val"
57
+ expect { result = c.fetch "6" do "6_val_2" end }.not_to raise_error
58
+ expect(result).to eq "6_val"
59
+ sleep(0.11)
60
+ expect { result = c.fetch "6" do "6_val_3" end }.not_to raise_error
61
+ expect(result).to eq "6_val_3"
62
+ end
63
+ end
64
+ end
65
+
66
+ it 'can be namespaced by inheritance' do
67
+ expect { result = MyCache.set "7", "7_val" }.not_to raise_error
68
+ expect(BluntCache.get "7").to eq nil
69
+ expect(ShortCache.get "7").to eq nil
70
+ expect(MyCache.get "7").to eq "7_val"
71
+ end
72
+
73
+ it 'uses overrided @expire_default when inherited' do
74
+ expect { result = ShortCache.set "8", "8_val" }.not_to raise_error
75
+ expect(ShortCache.get "8").to eq "8_val"
76
+ sleep(0.11)
77
+ expect(ShortCache.get "8").to eq nil
78
+ end
79
+
80
+ end
@@ -0,0 +1 @@
1
+ require 'blunt_cache'
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blunt-cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Roman Exempliarov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ description: Simple in-memory cache service for ruby.
56
+ email:
57
+ - urvala@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - LICENSE
66
+ - README.md
67
+ - blunt_cache.gemspec
68
+ - lib/blunt_cache.rb
69
+ - lib/blunt_cache/version.rb
70
+ - spec/blunt_cache_spec.rb
71
+ - spec/spec_helper.rb
72
+ homepage: https://github.com/appelsin/blunt_cache
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.4.7
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Simple in-memory cache service for ruby.
96
+ test_files:
97
+ - spec/blunt_cache_spec.rb
98
+ - spec/spec_helper.rb