cachoo 0.1.0
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/.gems +1 -0
- data/README.md +45 -0
- data/Rakefile +9 -0
- data/cachoo.gemspec +14 -0
- data/lib/cachoo.rb +48 -0
- data/test/cachoo_test.rb +76 -0
- metadata +67 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 363a5740d37f68aacc6a737155e89ec172ef18f1
|
4
|
+
data.tar.gz: 1cceef15686c26aa24324b5e0c9cb76c65a07f9c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ada8a34dfdbfce575a21423ce0977083eaeb375298629c638d4e70977ba6c36f4c78f2036836acef3057e3c4767362b4c9ceb23f1dec5a8ec8d2007b2da2c305
|
7
|
+
data.tar.gz: a7202e0e598bf4a805210a6af363467117dc8f609a884310b24bc683844e35a34ebcdb1b1e2ec1bd2993b4cad3f0f5ac5a8e49f87676c1e82741066f116d01d4
|
data/.gems
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
cutest:1.2.1
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# Cachoo
|
2
|
+
|
3
|
+
Expirable memoization of methods
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```bash
|
8
|
+
gem install cachoo
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
class Cache
|
15
|
+
extend Cachoo
|
16
|
+
|
17
|
+
def now
|
18
|
+
Time.now.utc
|
19
|
+
end
|
20
|
+
cachoo :now # :now gets cached for 5 seconds by default
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
class Cache
|
26
|
+
extend Cachoo
|
27
|
+
|
28
|
+
def now
|
29
|
+
Time.now.utc
|
30
|
+
end
|
31
|
+
cachoo :now, for: 60*60 # :now get cached for 1 hour
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
You can also ahange the time globally:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
Cachoo.for = 60*60*24 # 1 day cache
|
39
|
+
```
|
40
|
+
|
41
|
+
## Why?
|
42
|
+
I hate manually expiring memoization
|
43
|
+
|
44
|
+
## Name
|
45
|
+
It's an invented word betweeh `cache` and `achoo` :)
|
data/Rakefile
ADDED
data/cachoo.gemspec
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "cachoo"
|
3
|
+
s.version = "0.1.0"
|
4
|
+
s.summary = "Expirable method memoization"
|
5
|
+
s.description = "A quick and dirty way to expire memoization"
|
6
|
+
s.authors = ["elcuervo"]
|
7
|
+
s.licenses = ["MIT", "HUGWARE"]
|
8
|
+
s.email = ["yo@brunoaguirre.com"]
|
9
|
+
s.homepage = "http://github.com/elcuervo/cachoo"
|
10
|
+
s.files = `git ls-files`.split("\n")
|
11
|
+
s.test_files = `git ls-files test`.split("\n")
|
12
|
+
|
13
|
+
s.add_development_dependency("cutest", "~> 1.2.1")
|
14
|
+
end
|
data/lib/cachoo.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
module Cachoo
|
2
|
+
MUTEX = Mutex.new
|
3
|
+
CACHE = Hash.new do |h,k|
|
4
|
+
h[k] = Hash.new { |h,k| h[k] = nil }
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.for=(seconds)
|
8
|
+
@_cache_time = seconds
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.sync(&block)
|
12
|
+
MUTEX.synchronize(&block)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.for
|
16
|
+
@_cache_time ||= 5
|
17
|
+
end
|
18
|
+
|
19
|
+
def cachoo(*method_names)
|
20
|
+
options = method_names.find { |i| i.is_a?(Hash) && method_names.delete(i) } || {}
|
21
|
+
expires_in = options.fetch(:for, Cachoo.for)
|
22
|
+
|
23
|
+
Array(method_names).each do |method_name|
|
24
|
+
method_sym = :"__uncached_#{method_name}"
|
25
|
+
|
26
|
+
alias_method method_sym, method_name
|
27
|
+
remove_method method_name
|
28
|
+
|
29
|
+
define_method(method_name) do |*args|
|
30
|
+
Thread.new do
|
31
|
+
sleep expires_in
|
32
|
+
Cachoo.sync { CACHE.delete(method_name) }
|
33
|
+
end
|
34
|
+
|
35
|
+
sig = args.map(&:hash)
|
36
|
+
c = CACHE[method_name]
|
37
|
+
|
38
|
+
return Cachoo.sync { CACHE[method_name][:return] } if c[:sig] == sig
|
39
|
+
|
40
|
+
Cachoo.sync do
|
41
|
+
ret = method(method_sym).call(*args)
|
42
|
+
CACHE[method_name] = { sig: sig, return: ret }
|
43
|
+
ret
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/test/cachoo_test.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'cutest'
|
2
|
+
require 'cachoo'
|
3
|
+
|
4
|
+
setup do
|
5
|
+
Cachoo.for = 1
|
6
|
+
|
7
|
+
class Test
|
8
|
+
extend Cachoo
|
9
|
+
|
10
|
+
def now
|
11
|
+
Time.now.utc
|
12
|
+
end
|
13
|
+
|
14
|
+
def greet(name)
|
15
|
+
"Hello #{name}"
|
16
|
+
end
|
17
|
+
cachoo :now, :greet
|
18
|
+
end
|
19
|
+
|
20
|
+
class Other
|
21
|
+
extend Cachoo
|
22
|
+
|
23
|
+
def now
|
24
|
+
Time.now.utc
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class OtherTime
|
29
|
+
extend Cachoo
|
30
|
+
|
31
|
+
def kuak
|
32
|
+
Time.now.utc
|
33
|
+
end
|
34
|
+
cachoo :kuak, for: 2
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
test "should be included" do
|
39
|
+
assert Test.methods.include?(:cachoo)
|
40
|
+
end
|
41
|
+
|
42
|
+
test "should respect method arguments" do
|
43
|
+
klass = Test.new
|
44
|
+
assert klass.greet("Bob") != klass.greet("Dave")
|
45
|
+
end
|
46
|
+
|
47
|
+
test "should cache a method call for 1 second" do
|
48
|
+
klass = Test.new
|
49
|
+
now = klass.now
|
50
|
+
|
51
|
+
assert_equal klass.now, now
|
52
|
+
|
53
|
+
sleep 1
|
54
|
+
|
55
|
+
assert klass.now != now
|
56
|
+
end
|
57
|
+
|
58
|
+
test "cache different classes" do
|
59
|
+
k1 = Test.new
|
60
|
+
k2 = Other.new
|
61
|
+
|
62
|
+
now = k1.now
|
63
|
+
sleep 1
|
64
|
+
assert now != k2.now
|
65
|
+
end
|
66
|
+
|
67
|
+
test "cache for different timespans" do
|
68
|
+
time = OtherTime.new
|
69
|
+
kuak = time.kuak
|
70
|
+
|
71
|
+
assert_equal kuak, time.kuak
|
72
|
+
sleep 1
|
73
|
+
assert_equal kuak, time.kuak
|
74
|
+
sleep 1
|
75
|
+
assert kuak != time.kuak
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cachoo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- elcuervo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: cutest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.2.1
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.2.1
|
27
|
+
description: A quick and dirty way to expire memoization
|
28
|
+
email:
|
29
|
+
- yo@brunoaguirre.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- .gems
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- cachoo.gemspec
|
38
|
+
- lib/cachoo.rb
|
39
|
+
- test/cachoo_test.rb
|
40
|
+
homepage: http://github.com/elcuervo/cachoo
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
- HUGWARE
|
44
|
+
metadata: {}
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 2.0.14
|
62
|
+
signing_key:
|
63
|
+
specification_version: 4
|
64
|
+
summary: Expirable method memoization
|
65
|
+
test_files:
|
66
|
+
- test/cachoo_test.rb
|
67
|
+
has_rdoc:
|