cache_hash 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +93 -0
- data/Rakefile +2 -0
- data/bin/test.rb +93 -0
- data/cache_hash.gemspec +28 -0
- data/lib/cache_hash.rb +85 -0
- data/lib/cache_hash/version.rb +3 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ff056e231d380da9b9835a65116f1294cac072cd
|
4
|
+
data.tar.gz: a2c6a788d6b00896aff970f80ba4106858608e66
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 06f006651e1ea46ec1e6bc9f79e44e34e93975b5458557b30457021b31bed99612a7dbc4026dd94983b9c3bced61a053f4d6ce1ca1ad91160158e5ab2a86fd4d
|
7
|
+
data.tar.gz: 4b9d620a16629cdb601130fad5bd428c5171fa8964c6a7e148e98881ca9b3b694d5a5ad486e27af7f4234d8bf370e2eac8dd365e75467f5e0a305f8927a7268f
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Doug Everly
|
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,93 @@
|
|
1
|
+
# CacheHash
|
2
|
+
|
3
|
+
Simple hash with expiring values.
|
4
|
+
|
5
|
+
* Simple - Returns value or nil.
|
6
|
+
* Default - Returns value or default
|
7
|
+
* Block - Returns value, or refreshes the value by yielding to long running job.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'cache_hash'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install cache_hash
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### Simple
|
28
|
+
|
29
|
+
Returns value or nil.
|
30
|
+
|
31
|
+
c = CacheHash.new(ttl: 3)
|
32
|
+
c[:a] = "its here"
|
33
|
+
puts c[:a] # => "its here" or nil if called after 3 seconds
|
34
|
+
|
35
|
+
### Default Value
|
36
|
+
|
37
|
+
Returns value or default.
|
38
|
+
|
39
|
+
x = 0
|
40
|
+
c[:foo] = "HAZ VALUE"
|
41
|
+
c.fetch(:foo, "NO VALUE") # => "HAZ VALUE"
|
42
|
+
# wait 3 seconds
|
43
|
+
c.fetch(:foo, "NO VALUE") # => "NO VALUE"
|
44
|
+
|
45
|
+
|
46
|
+
### Block
|
47
|
+
|
48
|
+
Returns value, or refreshes the value by yielding to long running job.
|
49
|
+
|
50
|
+
c = CacheHash.new(ttl: 5)
|
51
|
+
users = ['smith', 'jones']
|
52
|
+
|
53
|
+
# first run loads cache
|
54
|
+
|
55
|
+
users.each do |user|
|
56
|
+
row = c.fetch(user) {
|
57
|
+
# long running job
|
58
|
+
get_user(user)
|
59
|
+
}
|
60
|
+
end
|
61
|
+
|
62
|
+
# second run hits cache
|
63
|
+
|
64
|
+
users.each do |user|
|
65
|
+
row = c.fetch(user) {
|
66
|
+
# long running job
|
67
|
+
get_user(user)
|
68
|
+
}
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
# third run refreshes cache
|
73
|
+
|
74
|
+
sleep 6
|
75
|
+
users.each do |user|
|
76
|
+
row = c.fetch(user) {
|
77
|
+
# long running job
|
78
|
+
get_user(user)
|
79
|
+
}
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
c = CacheHash.new(ttl: 2, gc_interval: 1)
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
## Contributing
|
88
|
+
|
89
|
+
1. Fork it ( https://github.com/[my-github-username]/cache_hash/fork )
|
90
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
91
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
92
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
93
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/test.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'cache_hash'
|
5
|
+
|
6
|
+
def test1
|
7
|
+
puts "Testing fetch"
|
8
|
+
c = CacheHash.new(ttl: 3, gc_interval: 1)
|
9
|
+
|
10
|
+
c[:a] = "its here"
|
11
|
+
|
12
|
+
sleep 1
|
13
|
+
puts c.fetch(:a, "its gone!")
|
14
|
+
|
15
|
+
sleep 1
|
16
|
+
puts c.fetch(:a, "its gone!")
|
17
|
+
|
18
|
+
sleep 1
|
19
|
+
puts c.fetch(:a, "its gone!")
|
20
|
+
|
21
|
+
sleep 1
|
22
|
+
puts c.fetch(:a, "its gone!")
|
23
|
+
|
24
|
+
sleep 1
|
25
|
+
puts c.fetch(:a, "its gone!")
|
26
|
+
end
|
27
|
+
|
28
|
+
def test2
|
29
|
+
puts "Testing array syntax"
|
30
|
+
c = CacheHash.new(ttl: 3, gc_interval: 1)
|
31
|
+
|
32
|
+
c[:a] = "its here"
|
33
|
+
|
34
|
+
sleep 1
|
35
|
+
puts c[:a] || "its gone!"
|
36
|
+
|
37
|
+
sleep 1
|
38
|
+
puts c[:a] || "its gone!"
|
39
|
+
|
40
|
+
sleep 1
|
41
|
+
puts c[:a] || "its gone!"
|
42
|
+
|
43
|
+
sleep 1
|
44
|
+
puts c[:a] || "its gone!"
|
45
|
+
|
46
|
+
sleep 1
|
47
|
+
puts c[:a] || "its gone!"
|
48
|
+
|
49
|
+
sleep 1
|
50
|
+
puts c[:a] || "its gone!"
|
51
|
+
|
52
|
+
sleep 1
|
53
|
+
puts c[:a] || "its gone!"
|
54
|
+
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
def test3
|
59
|
+
puts "Testing block"
|
60
|
+
c = CacheHash.new(ttl: 3, gc_interval: 1)
|
61
|
+
|
62
|
+
x = 0
|
63
|
+
10.times do
|
64
|
+
r = c.fetch(:foo) {
|
65
|
+
puts "expired cache"
|
66
|
+
x
|
67
|
+
}
|
68
|
+
x = x + 1
|
69
|
+
puts r
|
70
|
+
sleep 2
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
def test4
|
76
|
+
puts "Testing default"
|
77
|
+
c = CacheHash.new(ttl: 2, gc_interval: 1)
|
78
|
+
|
79
|
+
x = 0
|
80
|
+
c[:foo] = "HAZ VALUE"
|
81
|
+
4.times do
|
82
|
+
r = c.fetch(:foo, "NO VALUE")
|
83
|
+
x = x + 1
|
84
|
+
puts r
|
85
|
+
sleep 1
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
test2
|
91
|
+
test1
|
92
|
+
test3
|
93
|
+
test4
|
data/cache_hash.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cache_hash/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "cache_hash"
|
8
|
+
spec.version = CacheHash::VERSION
|
9
|
+
spec.authors = ["Doug Everly"]
|
10
|
+
spec.email = ["Doug@Everly.org"]
|
11
|
+
spec.summary = %q{Simple hash with expiring values.}
|
12
|
+
spec.description = %q{Simple hash with expiring values.
|
13
|
+
|
14
|
+
* Simple - Returns value or nil.
|
15
|
+
* Default - Returns value or default
|
16
|
+
* Block - Returns value, or refreshes the value by yielding to long running job.}
|
17
|
+
|
18
|
+
spec.homepage = "https://github.com/DougEverly/CacheHash"
|
19
|
+
spec.license = "MIT"
|
20
|
+
|
21
|
+
spec.files = `git ls-files -z`.split("\x0")
|
22
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
23
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
end
|
data/lib/cache_hash.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require "cache_hash/version"
|
2
|
+
|
3
|
+
class CacheHash
|
4
|
+
# CacheHash is a simple hash implementation where the
|
5
|
+
# value is purged after TTL seconds.
|
6
|
+
|
7
|
+
attr_reader :ttl, :gc_interval
|
8
|
+
|
9
|
+
# Creates CacheHash and starts garbage collection
|
10
|
+
# @param [seconds] ttl Number to seconds to retain value
|
11
|
+
# @param [seconds] gc_interval Seconds to wait between GC
|
12
|
+
def initialize(ttl: 60, gc_interval: 10)
|
13
|
+
@data = Hash.new
|
14
|
+
@mutex = Mutex.new
|
15
|
+
@gc_thread = nil
|
16
|
+
|
17
|
+
@ttl = ttl
|
18
|
+
@gc_interval = gc_interval # seconds
|
19
|
+
start_gc
|
20
|
+
end
|
21
|
+
|
22
|
+
# Sets a value for a key and returns the value
|
23
|
+
# @param [Object] key
|
24
|
+
# @param [Object] value
|
25
|
+
# @return [Object] value
|
26
|
+
def []=(key, value)
|
27
|
+
@mutex.synchronize {
|
28
|
+
@data[key] = {
|
29
|
+
value: value,
|
30
|
+
expires_at: Time.now.to_i + @ttl
|
31
|
+
}
|
32
|
+
}
|
33
|
+
value
|
34
|
+
end
|
35
|
+
|
36
|
+
# Gets value for key, or nil if expired
|
37
|
+
# @param [Object] key
|
38
|
+
# @return [Object, nil]
|
39
|
+
def [](key)
|
40
|
+
@mutex.synchronize {
|
41
|
+
if @data.has_key?(key)
|
42
|
+
if Time.now.to_i > @data[key][:expires_at]
|
43
|
+
# @data.delete(key)
|
44
|
+
return nil
|
45
|
+
else
|
46
|
+
return @data[key][:value]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
}
|
50
|
+
return nil
|
51
|
+
end
|
52
|
+
|
53
|
+
# Gets value for key, or default_value if expired
|
54
|
+
# @param [Object] key
|
55
|
+
# @param [Object] default_value
|
56
|
+
# @param [Block]
|
57
|
+
# @return [Object, nil]
|
58
|
+
def fetch(key, default_value = nil)
|
59
|
+
raise ArgumentError, "Block not given" if (block_given? && default_value)
|
60
|
+
self[key] || default_value || (self[key] = yield) # (*args))
|
61
|
+
end
|
62
|
+
|
63
|
+
# Kick off garbage collection in a thread
|
64
|
+
def start_gc
|
65
|
+
@gc_thread = Thread.new do
|
66
|
+
loop do
|
67
|
+
sleep @gc_interval
|
68
|
+
gc
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Delete keys with expired values
|
74
|
+
def gc
|
75
|
+
now = Time.now.to_i
|
76
|
+
@mutex.synchronize {
|
77
|
+
@data.each_pair do |key, value|
|
78
|
+
if now > @data[key][:expires_at]
|
79
|
+
@data.delete key
|
80
|
+
end
|
81
|
+
end
|
82
|
+
}
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cache_hash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Doug Everly
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-11 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
|
+
description: |-
|
42
|
+
Simple hash with expiring values.
|
43
|
+
|
44
|
+
* Simple - Returns value or nil.
|
45
|
+
* Default - Returns value or default
|
46
|
+
* Block - Returns value, or refreshes the value by yielding to long running job.
|
47
|
+
email:
|
48
|
+
- Doug@Everly.org
|
49
|
+
executables:
|
50
|
+
- test.rb
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- ".gitignore"
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- bin/test.rb
|
60
|
+
- cache_hash.gemspec
|
61
|
+
- lib/cache_hash.rb
|
62
|
+
- lib/cache_hash/version.rb
|
63
|
+
homepage: https://github.com/DougEverly/CacheHash
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata: {}
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 2.2.0
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: Simple hash with expiring values.
|
87
|
+
test_files: []
|