lock_and_cache 0.0.2
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 +7 -0
- data/.gitignore +15 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/CHANGELOG +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +36 -0
- data/Rakefile +7 -0
- data/lib/lock_and_cache.rb +27 -0
- data/lib/lock_and_cache/version.rb +3 -0
- data/lock_and_cache.gemspec +32 -0
- data/spec/lock_and_cache_spec.rb +91 -0
- data/spec/spec_helper.rb +7 -0
- metadata +201 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4f956903ed9cadae938803806da303032e5770a3
|
4
|
+
data.tar.gz: bcb67b572971fbc37311f14d998c3b97b29d3317
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5f3788ca8c067748e1dd8f3e75fca7d5ba0d669c09eea78a703fdf4cb6314be2d0f30e40d9898a765678f683b935956d9072fe2f8478a3b59dbf6c00abef8c9e
|
7
|
+
data.tar.gz: 54b68c613423afc0b56e7d17a98091c10ebd18b5614aa462489b14e4f573cc6abdf2fcd65aa8f5349b39b91ba6eb8f0d64ca91611f2f33e8c5b462dde5cd65bc
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Seamus Abshere
|
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,36 @@
|
|
1
|
+
# LockAndCache
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Wishlist
|
6
|
+
|
7
|
+
* no dep on activerecord
|
8
|
+
* no dep on cache_method
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'lock_and_cache'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install lock_and_cache
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
TODO: Write usage instructions here
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
|
32
|
+
1. Fork it ( https://github.com/[my-github-username]/lock_and_cache/fork )
|
33
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
34
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
35
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
36
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'lock_and_cache/version'
|
2
|
+
|
3
|
+
require 'hash_digest'
|
4
|
+
require 'cache_method'
|
5
|
+
require 'active_record'
|
6
|
+
require 'with_advisory_lock'
|
7
|
+
|
8
|
+
module LockAndCache
|
9
|
+
# only for instance methods
|
10
|
+
def lock_and_cache(method_id)
|
11
|
+
unlocked_method_id = "_lock_and_cache_unlocked_#{method_id}"
|
12
|
+
alias_method unlocked_method_id, method_id
|
13
|
+
|
14
|
+
define_method method_id do |*args|
|
15
|
+
lock_key = [self.class.name, method_id, HashDigest.digest3([as_cache_key]+args)].join('/')
|
16
|
+
ActiveRecord::Base.with_advisory_lock(lock_key) do
|
17
|
+
if cache_method_cached?(method_id, args)
|
18
|
+
send method_id, *args # which will be the cached version
|
19
|
+
else
|
20
|
+
send unlocked_method_id
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
cache_method method_id
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'lock_and_cache/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "lock_and_cache"
|
8
|
+
spec.version = LockAndCache::VERSION
|
9
|
+
spec.authors = ["Seamus Abshere"]
|
10
|
+
spec.email = ["seamus@abshere.net"]
|
11
|
+
spec.summary = %q{Lock and cache methods.}
|
12
|
+
spec.description = %q{Lock and cache methods, in case things should only be calculated once across processes.}
|
13
|
+
spec.homepage = "https://github.com/seamusabshere/lock_and_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_runtime_dependency 'activerecord'
|
22
|
+
spec.add_runtime_dependency 'cache_method'
|
23
|
+
spec.add_runtime_dependency 'hash_digest'
|
24
|
+
spec.add_runtime_dependency 'with_advisory_lock'
|
25
|
+
|
26
|
+
spec.add_development_dependency 'pry'
|
27
|
+
spec.add_development_dependency 'activesupport'
|
28
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
29
|
+
spec.add_development_dependency 'pg'
|
30
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
31
|
+
spec.add_development_dependency 'rspec'
|
32
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Foo
|
4
|
+
def initialize(id)
|
5
|
+
@id = id
|
6
|
+
@count = 0
|
7
|
+
end
|
8
|
+
def click
|
9
|
+
@count += 1
|
10
|
+
end
|
11
|
+
|
12
|
+
extend LockAndCache
|
13
|
+
lock_and_cache :click
|
14
|
+
def as_cache_key
|
15
|
+
@id
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'set'
|
20
|
+
$clicking = Set.new
|
21
|
+
class Bar
|
22
|
+
def initialize(id)
|
23
|
+
@id = id
|
24
|
+
@count = 0
|
25
|
+
end
|
26
|
+
def unsafe_click
|
27
|
+
Thread.exclusive do
|
28
|
+
# puts "clicking bar #{@id} - #{$clicking.to_a} - #{$clicking.include?(@id)} - #{@id == $clicking.to_a[0]}"
|
29
|
+
raise "somebody already clicking Bar #{@id}" if $clicking.include?(@id)
|
30
|
+
$clicking << @id
|
31
|
+
end
|
32
|
+
sleep 1
|
33
|
+
@count += 1
|
34
|
+
Thread.exclusive do
|
35
|
+
$clicking.delete @id
|
36
|
+
end
|
37
|
+
@count
|
38
|
+
end
|
39
|
+
def click
|
40
|
+
unsafe_click
|
41
|
+
end
|
42
|
+
|
43
|
+
extend LockAndCache
|
44
|
+
lock_and_cache :click
|
45
|
+
def as_cache_key
|
46
|
+
@id
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe LockAndCache do
|
51
|
+
it 'has a version number' do
|
52
|
+
expect(LockAndCache::VERSION).not_to be nil
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "caching" do
|
56
|
+
let(:foo) { Foo.new(rand.to_s) }
|
57
|
+
it "works" do
|
58
|
+
expect(foo.click).to eq(1)
|
59
|
+
expect(foo.click).to eq(1)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "locking" do
|
64
|
+
let(:bar) { Bar.new(rand.to_s) }
|
65
|
+
it "it blows up normally" do
|
66
|
+
a = Thread.new do
|
67
|
+
bar.unsafe_click
|
68
|
+
end
|
69
|
+
b = Thread.new do
|
70
|
+
bar.unsafe_click
|
71
|
+
end
|
72
|
+
expect do
|
73
|
+
a.join
|
74
|
+
b.join
|
75
|
+
end.to raise_error(/somebody/)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "doesn't blow up if you lock it" do
|
79
|
+
a = Thread.new do
|
80
|
+
bar.click
|
81
|
+
end
|
82
|
+
b = Thread.new do
|
83
|
+
bar.click
|
84
|
+
end
|
85
|
+
a.join
|
86
|
+
b.join
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lock_and_cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Seamus Abshere
|
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: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: cache_method
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: hash_digest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: with_advisory_lock
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activesupport
|
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
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: bundler
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '1.7'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.7'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: pg
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rake
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '10.0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '10.0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: rspec
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
description: Lock and cache methods, in case things should only be calculated once
|
154
|
+
across processes.
|
155
|
+
email:
|
156
|
+
- seamus@abshere.net
|
157
|
+
executables: []
|
158
|
+
extensions: []
|
159
|
+
extra_rdoc_files: []
|
160
|
+
files:
|
161
|
+
- ".gitignore"
|
162
|
+
- ".rspec"
|
163
|
+
- ".travis.yml"
|
164
|
+
- CHANGELOG
|
165
|
+
- Gemfile
|
166
|
+
- LICENSE.txt
|
167
|
+
- README.md
|
168
|
+
- Rakefile
|
169
|
+
- lib/lock_and_cache.rb
|
170
|
+
- lib/lock_and_cache/version.rb
|
171
|
+
- lock_and_cache.gemspec
|
172
|
+
- spec/lock_and_cache_spec.rb
|
173
|
+
- spec/spec_helper.rb
|
174
|
+
homepage: https://github.com/seamusabshere/lock_and_cache
|
175
|
+
licenses:
|
176
|
+
- MIT
|
177
|
+
metadata: {}
|
178
|
+
post_install_message:
|
179
|
+
rdoc_options: []
|
180
|
+
require_paths:
|
181
|
+
- lib
|
182
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
183
|
+
requirements:
|
184
|
+
- - ">="
|
185
|
+
- !ruby/object:Gem::Version
|
186
|
+
version: '0'
|
187
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
188
|
+
requirements:
|
189
|
+
- - ">="
|
190
|
+
- !ruby/object:Gem::Version
|
191
|
+
version: '0'
|
192
|
+
requirements: []
|
193
|
+
rubyforge_project:
|
194
|
+
rubygems_version: 2.2.2
|
195
|
+
signing_key:
|
196
|
+
specification_version: 4
|
197
|
+
summary: Lock and cache methods.
|
198
|
+
test_files:
|
199
|
+
- spec/lock_and_cache_spec.rb
|
200
|
+
- spec/spec_helper.rb
|
201
|
+
has_rdoc:
|